1. Engineering
  2. Computer Science
  3. implement a class score that is a subclass of the...

Question: implement a class score that is a subclass of the...

Question details

Implement a class Score (that is a subclass of the object class) representing a score (e.g. for homework, a game, etc.), that is a non-negative integer, and supporting eight methods:

  1. __init__() which either takes no parameters or takes a single parameter representing an initial value for the score. If no parameter is provided or the parameter provided is not an integer, then the constructor sets the number associated with the score to 0. If the parameter provided is >=0 and an integer, then the constructor sets the number associated with the score to the parameter.
  2. __repr__() which returns a string "Score(number)" where number is the current value of the score. You can see several examples below.
  3. __str__() which returns a string containing the current value of the score. You can see several examples below.
  4. update() which prompts the user to update the score. It repeatedly re-prompts the user until he/she enters a non-negative integer value, at which point the method resets the value of the score to the number obtained from the user. The method must recover from illegal input of all types, and several examples are shown below. It does not return any values.
  5. incr() which increases the value of the score by 1. It does not return any values.
  6. __lt__() which takes a second Score as a parameter and returns True if the calling Score is less than the other score and False otherwise.
  7. __add__() which takes a second Score as a parameter and returns a new Score that is the sum of the values stored in the two Score objects.
  8. __sub__() which takes a second Score as a parameter and returns a new Score. The value of the new Score object is the calling Score minus the parameter Score, assuming that the result is not negative. If the parameter Score is larger than the calling Score, the method returns a Score with value 0.

The following shows how the Score class and its methods could be used:

Python 3.7.2 Shell File Edit She Debug Options Window Help >>> s1 = Score() >>> s1 Score (0) >>> print (s1) 0 >>> 31.incr () >>> s1 Score (1) >>1. update () The current score is 1 Enter a new score (> 0): -1 Scores must be non-negative. The current score is 1 Enter a new score (> 0): five Please enter a whole number using digits. The current score is 1 Enter a new score (0): 3.5 Please enter a whole number using digits. The current score is 1 Enter a new score (>0): 3 >>> s1 Score (3) >>> s2 = Score (-2) >>S2 Score (0) >>S2.update () The current score is 0 Enter a new score (> 0): >>S2 Score (2) >>>s1 32 False >>>2 31 True 33- 31 - 32 >> 33 Score (1) >>> s4 = s2 - s1 >>34 Score (0) >>> s1 Score (3) >>S2 Score (2) >>> 35Score (3.5) 35 Score (0) Ln: 52 Col: 4The following shows some sample interactions with the completed Student class:

  1. Write a class Student that represents a student in a course. The class supports eight methods:

    1. __init__() which takes an optional parameter representing the student's name. It sets the student's name to the parameter, using 'Gertrude' as the default if no name is provided. The constructor also creates an empty list inside the object which will be used to hold scores.

    2. __repr__() which returns a string "Student(name, hwLst)" where name is the student's name and hwLst is the list of homework scores for the student. You can see several examples below.

    3. __str__() which returns a string "name has homework scores: hwLst" where name is the name of the student and hwLst is the list of homework scores for the student. You can see several examples below.

    4. addScore() which adds a new Score to the homework list for the student. The method takes a parameter representing the value of the new score, creates a new Score object (using the class developed for the first question), and adds the new Score object into the homework list for the student. The method does not return any values.

    5. getMax() which returns the largest Score object stored in the list of homework scores. If the list of homework scores is non-empty, the method must return a Score object. If the list of homework scores is empty, the method returns None (the type, not a string).

    6. getMin() which returns the smallest Score object stored in the list of homework scores. If the list of homework scores is non-empty, the method must return a Score object. If the list of homework scores is empty, the method returns None (the type, not a string).

    7. getTotal() which returns a Score holding the total of all the homework Scores for the student. The method must return a Score object.

    8. __lt__() which returns True if the calling Student has a name that appears in the alphabet before the Student provided as a parameter. If two students have the same name, then the method returns True if the calling Student has a total score that is less than the parameter Student. Otherwise the method returns False. The total score must be computed via a call to the getTotal() method.

Python 3.7.2 Shell File Edit She Debug Options Window Help >>> 31 - Student () >>> s1 Student (Gertrude, []) >>> print (s1) Gertrude has homework scores [] >>> val -s1.getMax () >>> type (val) <class NoneType >>> val -s1.getMin() >>> type (val) <class NoneType> >31.addScore (85) >>> s1 Student (Gertrude, [Score (85)]) >> 31.addScore (-5) >>> s1 Student (Gertrude, [Score (85Score (0)]) >> 31.addScore (78) >>> s1 Student (Gertrude, [Score (85), Score (O), Score (78)]) >>> val -s1.getMax () >>val Score (85) S1 Student (Gertrude, [Score (85), Score (O), Score (78)]) >>> val = s 1 . ge t Min ( ) >>val Score (0) S1 Student (Gertrude, [Score (85), Score (O), Score (78)]) >>> val - s1.getTotal () >>val Score (163) >>> type (val) clasmain .Score> >>> s2 = Student (Prudence) >>S2 Student (Prudence, []) >32.addScore (83) >>S2 Student (Prudence, [Score (83)]) >>> 33Student () >> 33 Student (Gertrude, []) >>> s1 Student (Gertrude, [Score (85), Score (O), Score (78)]) S1 < 32 True ans 31 < 33 >>>ans False 32 < 33 False Ln: 94 Col: 18

dont use return sum([x.val for x in self.lst]) for getTotal it's not allowed

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution