You should be able to define and explain the following terms:
- Object-oriented programming, including
- methods
- an object's instance variables
- the constructor, or __init__ method
- the string, or __str__, method
- access methods, getters
- update methods, setters
- self
You should understand and be able to use the following Python concepts:
- How to define a Python class, including its constructor, string method, and the instance variables.
- How to use a Python class to create objects and call its methods.
Practice problems:
- Write a Student class that stores information for a Swarthmore student. The class should include the following instance variables:
- id, an integer identifier for the student
- firstName, a string
- lastName, a string
- courses, a list of strings, the course names that the student is enrolled in
- hobbies, a list of strings, the student's hobbies
Write the following methods for your Student class:
- A constructor that, given an id, a first name, and a last name, creates a Student with those values and empty lists of courses and hobbies.
- A string method that returns a string representing the student.
- Getter methods for each of the instance variables in the class.
- An addCourse method that, given a course name, adds that course to the student's course list.
- An addHobby method that, given a hobby name, adds that hobby to the student's hobby list.
- A getCredits method that returns the number of courses a student is enrolled in. You can assume that each course is worth 1 credit.
- A getFreeTime method that returns the value 55 - 12*number-of-courses - 5*number-of-hobbies. (Alternatively, this method could just return 0.)
- Test your student class by creating a main program with several
student bojects. Make sure to test that each method works as
expected.
- According to the textbook: "Objects know stuff and can do stuff." Explain what this means. How are objects different from functions?
include("../style/footer.php"); ?>