Quiz 3 OOP Practice


Hi all! This is the first wave of practice problems for your upcoming quiz Tuesday, November 10th. These problems are great practice for OOP concepts, including classes, methods, constructors, and more! Please try to attempt these on your own before checking the answer key. Good luck!

The solutions to these problems can be found here:

The video going over these solutions can be found here

Exercise 1: Insta User

In this exercise you will write the classes InstaUser and Admin. These classes will model a simplified version of an instagram account. In this scenario, an InstaUser can only change their password with the help of an Admin. Here are the following properties and specifications for each class.

InstaUser (class)

An InstaUser class has the following properties:

  • username (str)
  • password (str)
  • followers (List[str])
  • following (List[str])

You should initialize all of these properites in the constructor (it is especially important you set the Lists as [] inside the constructor so all the accounts don’t reference the same object on the heap!)

The InstaUser class has the following methods:

  • getFollowerCount -> returns an int representing # of followers

  • getFollowingCount -> return an int representing # of accounts the user is following

  • getMutuals -> return a List[str] with all the usernames of accounts that a) follow this user account and b) are also followed by this user account (aka they follow each other). Tip: make use of indexes and try/except to check for these mutuals

  • addFollower -> return None; this method accepts an InstaUser object called person. It adds person’s username to the current object’s followers List.

  • removeFollower -> return None; this method accepts an InstaUser object called person. It removes person’s username from the current object’s followers List, if it exists on the List (use the in operator here!)

  • follow -> return None; this method accepts an InstaUser object called person. It adds person’s username to the current object’s following List and adds the current object’s username to person’s followers List using person’s addFollower method.

  • unfollow -> return None; this method accepts an InstaUser object called person. It removes person’s username from the current object’s following List and removes the current object’s username from person’s followers List using person’s removeFollower method.

  • changePassword -> return bool; this method accepts an Admin object called admin and a str called password. It calls admin’s method, changePassword by passing a reference to self (the current object) and the string password. It returns the result of admin’s changePassword method, which should be either True or False.

Hint! You will need the following import statment at the top of your file.

The first import statement is most likely unfamiliar. The purpose of this statement is to allow the use of a class as a type inside of itself.

Admin (class)

An Admin class has the following properties:

  • username (str)

You should initialize this properity in the constructor.

The Admin class has the following method:

  • changePassword -> returns an bool; it accepts an InstaUser object called user and a str called newPassword. This method should return False if:
    • newPassword is the same as user’s current password. Print the following string: “Error: Password must be new”
    • newPassword is empty or longer than 12 characters. Print the following string: “Error: Password length out of bounds”

Otherwise, this method should change user’s password to newPassword and return True.

Exercise 2: Classception

What better way to learn about classes in Python than to work with real classes! In this exercise, you will model the course registration period by creating a Student class with methods that give a Student the capability of adding/dropping courses as well as a method to compare schedules with another Student.

  1. Write a class with the following specifications:
    1. Each Student should have a str attribute called name, a Dict[str, str] called schedule, an int attribute called credit_hours, and a bool attribute called full_time.
    2. Your Student class should have a constructor that takes in name and full_time. The constructor should use these arguments to initialize the name and full_time attributes. Your constructor should also initialize the schedule attribute to be an empty dictionary and the credit_hours attribute to be 0.
    3. Your class should also have 3 methods: add_course, drop_course, and is_classmate. The functionality of these methods should be as described below in the Requirements section.
  2. Use the __name__ is "__main__" idiom to test the implementation of your function with some sample calls to it whose return values are printed (documentation: https://docs.python.org/3/library/__main__.html).

Requirements

  1. add_course:
    1. add_course should take 2 str values and an int value as its arguments and return None.
    2. The first string will be a course code (ex: "COMP110"), the second string will be the time that the course starts (ex: "4:45PM"), and the integer will be the number of credit hours that the course is worth.
    3. add_course should add the course to the schedule attribute as long as certain conditions are met. The schedule attribute is a Dict[str, str] that has course codes as its keys and the corresponding start time of the course as its values (ex: {"COMP110": "4:45PM", "COMP211": "12:00PM"}). If the course is successfully added, then the credit_hour attribute should be increased by the number of credit hours that the added course is worth.
    4. There are several cases that need to be considered, each with a different outcome:
    5. If the full_time attribute is True, then the student may not exceed 18 credit hours worth of classes. So, if adding the course will make the credit_hours attribute greater than 18, the course cannot be added and you should print the message Add unsuccessful: too many credit hours. If adding the course does not make the credit_hours attribute exceed 18, then the course can be successfully added. If the course is already in the schedule, simply modify the start time of that course. Otherwise, add the course to the schedule (make the course a key in the schedule dictionary and make the course’s start time its corresponding value in the dictionary).
    6. If the full_time attribute is False, then the student is a part-time student and may not exceed 8 credit hours worth of classes. So, if adding the course will make the credit_hours attribute greater than 8, the course cannot be added and you should print the message Add unsuccessful: too many credit hours. If adding the course does not make the credit_hours attribute exceed 8, then the course can be successfully added. If the course is already in the schedule, simply modify the start time of that course. Otherwise, add the course to the schedule.
    7. Make sure add_course is fully functional before moving on to the other methods since the grader uses your add_course method to construct Student objects when testing the other methods.
  2. drop_course:
    1. drop_course should take a str value and an int value as its arguments and return None.
    2. The string will be a course code and the integer will be the number of credit hours that the course is worth.
    3. drop_course should drop the course from the schedule attribute as long as certain conditions are met. If the course is successfully dropped, then the credit_hour attribute should be decreased by the number of credit hours that the dropped course is worth.
    4. There are several cases that need to be considered, each with a different outcome:
    5. If the full_time attribute is True, then the student may not take fewer than 12 credit hours worth of classes. So, if dropping the course will make the credit_hours attribute less than 12, the course cannot be dropped and you should print the message Drop unsuccessful: must take at least 12 credit hours to maintain full time status. If dropping the course does not make the credit_hours attribute less than 12, then the course can be successfully dropped. If the course is already in the schedule, use the pop operation to remove the course from the schedule. Otherwise, if the course wasn’t in the schedule to begin with, no modifications should be made.
    6. If the full_time attribute is False, then the student is a part-time student and must be taking at least 1 course. So, if dropping the course will make the credit_hours attribute equal to 0, the course cannot be dropped and you should print the message Drop unsuccessful: must be enrolled in a class to maintain student status. If dropping the course does not make the credit_hours attribute equal to 0, then the course can be successfully dropped. If the course is already in the schedule, use the pop operation to remove the course from the schedule. Otherwise, if the course wasn’t in the schedule to begin with, no modifications should be made.
  3. is_classmate:
    1. is_classmate should take a Student value as its only argument and return bool.
    2. is_classmate should look through both student’s schedule attribute and create a list that stores all of the common courses that the students are in at the same time
    3. There are 2 cases that need to be considered, each with a different outcome:
    4. If there are no courses that the students are in at the same time, you should print "Hi <name attribute of the other student>, my name is <name attribute of self>! on one line, then print "Unfortunately, we have no classes together" on the next line, and then return False. See below for example:
  4. If the students are in any of the same courses at the same time, you should print "Hi <name attribute of the other student>, my name is <name attribute of self>! on one line, then print "This is a list of classes that I have with you: <list of common_classes>" on the next line, and then return True. See below for example:

  5. To gain full credit for these print statements, you may not use concatenation. Instead, you must make use of f-strings.
  6. Use correct static type annotations for function’s parameters and return type.