workshop_9

.pdf

School

University of Rochester *

*We aren’t endorsed by this school

Course

161

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

2

Uploaded by anonymousracoon1

Workshop 9 Classes 1. Write code to declare a class Person. A person has a name and a phone number. What is the best way to represent a phone number? Include accessor (functions that get the values), mutator (methods that let you change the values), and a constructor method. 2. Assume that you have the Person class defined from the previous question. Write the code to declare a class RockBand. A rock band has four people (a singer, a drummer, a guitar player, and a bass player). Include accessor, mutator, and constructor methods. Assume that you have the two classes defined in the previous questions. Write a main function that builds two different rock bands. 3. Modify the Student class, below, by adding a mutator method that records a grade for the student. Here is the specification of the new method: addGrade(self, gradePoint, credits) where gradePoint is a float value that represents a grade (e.g., A = 4.0, A- = 3.7, B+ = 3.3, etc.), and credits is a float value indicating the number of credit hours for the class. Modify the student class by adding this grade information. class Student: def __init__(self, name, hours, qpoints): self.name = name self.hours = float(hours) self.qpoints = float(qpoints) def getName(self): return self.name def getHours(self): return self.hours def getQPoints(self): return self.qpoints def gpa(self): return self.qpoints/self.hours Use the updated class to implement a simple program for calculating GPA. Your pro- gram should create a new student object that has 0 credits and 0 quality points (the name is irrelevant). Then prompt the user to enter course information (gradePoint and credits) and then print out the final GPA achieved. 1
4. Extend the previous exercise by implementing an addLetterGrade method. This is similar to addGrade except that it accepts a letter grade as a string (instead of a grade point). Use the updated class to improve the GPA calculator by allowing the entry of letter grades. 5. Write a class to represent the geometric solid sphere. Your class should implement the following methods: __init__(self, radius) # Creates a sphere given the radius. getRadius(self) # Returns the radius of this sphere. surfaceArea(self) # Returns the surface area of the sphere. volume(self) # Returns the volume of the sphere. Use this class to write a program that calculates the surface and the volume of a sphere given its radius as input from the user. 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help