class Student(object): '''A student with a name, age, major, and set of enrolled courses.''' def __init__(self, name, age, major='undeclared'): '''Initializes the student object.''' self.name = name self.age = age self.major = major self.courses = set() def add_course(self, course): '''Enroll the student in a course, as long as the student does not have a full class load already.''' if len(self.courses) < 4: self.courses.add(course) else: print('Schedule full, can\'t enroll') def birthyear(self): '''Return the student's year of birth.''' return 2016 - self.age def switch_major(self, new_major): '''Changes the student's major to new_major''' self.major = new_major def __str__(self): '''Returns a string representation of the student object.''' return self.name # return, not print! class Course: '''A class described by a name, an enrollment limit, and a set of enrolled students.''' def __init__(self, name, limit=30): '''Creates a new course with the specified name and limit and no initial students.''' self.name = name self.limit = limit self.students = set() def __str__(self): '''Return a textual version of the course.''' return '{0}, limit {1}, enrollment {2}'.format(self.name, self.limit, len(self.students)) def has_space(self): '''Return whether the course has space for another student.''' return len(self.students) < self.limit def add_student(self, student): '''Adds the specified student to the course''' if self.has_space(): self.students.add(student) # now, add this course to the student's list of courses student.add_course(self) print('Enrolled', student) else: print('No room for', student) # some sample Students and Courses alice = Student('Alice', 18, 'comp sci') bob = Student('Bob', 19, 'english') charlie = Student('Charlie', 20) course1 = Course('Biology 423: Evolution of Squirrels', 10) course2 = Course('History 130: Computers of the 12th Century', 20) course3 = Course('Government 250: Galactic Empires', 1) ######################## import math class Point: '''Represent an (x, y) coordinate pair.''' def __init__(self, new_x, new_y): '''Set up a new Point object with an x and y value.''' self.x = new_x self.y = new_y def __str__(self): '''Return a string representation of the point object.''' return '({0}, {1})'.format(self.x, self.y) def distance_to(self, other_point): '''Return the distance from this point to other_point.''' x1 = self.x y1 = self.y x2 = other_point.x y2 = other_point.y return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) def distance_origin(self): '''Return the distance from this point (aka self) to the origin.''' origin = Point(0, 0) return self.distance_to(origin) class LineSegment: '''A line segment from one point to another point.''' def __init__(self, start_point, end_point): '''Initializes the line segment by storing the start and endpoints.''' self.start_point = start_point self.end_point = end_point def __str__(self): '''Return a string version of the line segment.''' return 'Line from ' + str(self.start_point) + ' to ' + str(self.end_point) def slope(self): '''Return the slope of the line segment.''' change_y = self.end_point.y - self.start_point.y change_x = self.end_point.x - self.start_point.x return change_y / change_x def length(self): '''Return the length of the line segment.''' return self.start_point.distance_to(self.end_point)