13.12. Project: Classes and Objects

Now its time to design a new class of robots. These objects will all be able to clean our school building, but we want to pick the best ones.

Let’s create a class to handle new cleaning crew candidates!

13.12.1. Before You Start

If your teacher added you to a repl.it classroom or a Trinket course, login to your account to access the starter code for this project.

If you are NOT enrolled in a repl.it classroom or Trinket course, use one of the following links to copy the starter code.

  1. Trinket code (Remix before getting started).
  2. Repl.it code (Fork before getting started).

13.12.2. Part 1: Add Class Properties and __str__ Method

  1. Declare a class called CrewCandidate with an __init__ that takes three parameters: name, mass, and scores. Note that scores will be a list of test results for the candidate’s speed and accuracy.

  2. In main(), create objects for the following candidates:

    1. iClean has a mass of 13.5 kg and test scores of 88, 85, and 90.
    2. Shiny has a mass of 1.5 kg and test scores of 93, 88, and 97.
    3. DustVac has a mass of 22.5 kg and test scores of 75, 78, and 62.
  3. Define a __str__ method to display the properties of each candidate. The output should look something like:

    **Candidate Information**
    Name: Shiny
    Mass: 1.5 kg
    Scores: [93, 88, 97]
    
  4. Print each object to check to see if your class correctly assigns and displays the property values. (You can remove or comment out the print statement after your tests pass).

13.12.3. Part 2: Add Another Class Method

As our candidates complete more tests, we need to be able to add the new scores to their records.

  1. Create an add_score method in CrewCandidate. The function must take a new score as a parameter in addition to self.

  2. When passed a score, the function adds the value to self.scores with the append method.

    Tip

    Inside the class, the name of the list is self.scores instead of scores. Thus, self.scores.append() is the proper syntax.

  3. In main(), test your new method by adding a score of 83 to iClean’s record. Print out the new score list with object_name.scores.

13.12.4. Part 3: Add More Methods

Now that we can add scores to our candidates’ records, we need to evaluate their fitness for our cleaning program. Let’s add two more methods to CrewCandidate. One will average the test scores, and the other will decide if the candidate should be added to the cleaning team.

13.12.4.1. Calculating the Test Average

  1. Define an average() method. It only needs the self parameter.
  2. To find the average, add up the entries from self.scores, then divide the sum by the number of scores in the list.
  3. To make the average easier to look at, round it to 1 decimal place, then return the result from the method.

Check your code by evaluating and printing Shiny’s average test score (92.7).

13.12.4.2. Determining Candidate Status

Candidates with averages at or above 90% are automatically added to our new cleaning crew. Backup robots average between 80 - 89%, while robots with averages between 70 - 79% get sent out for repairs. Averages below 70% lead to a short trip to the recycling bin.

  1. Add a status() method to CrewCandidate. The method returns a string ('Accepted', 'Backup', 'Maintenance', or 'Scrapped') depending on a candidate’s average.

  2. The status method requires the average test score. Fortunately, methods can call other methods inside a class! Just remember to use self.method_name().

  3. Once status has a candidate’s average score, return the proper string based on that value.

  4. In main(), test the status method on each of the three candidates. Print the result with the format, '___ scored an average of ___% (___).'

    iClean scored an average of 87.7% (Backup).
    Shiny scored an average of 92.7% (Accepted).
    DustVac scored an average of 71.7% (Maintenance).
    

13.12.5. Part 4: Play a Bit

Use the class methods to boost DustVac’s status to Backup or higher. How many good tests will it take to reach Backup status? How many to reach Accepted?

Note that scores cannot exceed 100%.

Tip

Rather than adding one score at a time, could you use a loop?