Lesson15 Individual Assignment

Individual means that you do it yourself. You won't learn to code if you don't struggle for yourself and write your own code. Remember that while you can discuss the general (algorithmic) way to solve a problem, you should not even be looking at anyone else's code or showing anyone else your code for an individual assignment.
Review the Group Work guidelines on Cavas and/or ask an instructor if you have any questions.

Programming Practice

Be sure to spell all function names correctly - misspelled functions will lose points (and often break anyway since no one is sure what to type to call it). If you prefer showing your earlier, scratch work as you figure out what you are doing, please be sure that you make a final, complete, correct last function in its own cell that you then call several times to test. In other words, separate your thought process/working versions from the final one (a comment that tells us which is the final version would be lovely).

Every function should have at least a docstring at the start that states what it does (see Lesson3 Team Notebook if you need a reminder). Make other comments as necessary.

Make sure that you are running test cases (plural) for everything and commenting on the results in markdown. Your comments should discuss how you know that the test case results are correct.

part 1: Instance Variables and Get Methods

While you should do steps A, B, and C incrementally, you cannot really test each step until the end of this part, except to check that your code runs without an error in a Python cell (shift-enter). Therefore you can just provide the entire class definition at the end of this part, along with your code that tests the methods. We do not need to see your intermediate work for parts A and B.

A note about names:
It is programming convention that classes have capitalized names and objects (and variables in general) have lowercase names. For example, if we had a very minimal Customer class as shown below:

class Customer:

    def __init__(self, name, balance=0.0):
        self.name = name
        self.balance = balance

We would instantiate objects of that class with lowercase identifiers:

jane = ('Jane', 1000.0)

Notice the distinction between the lowercase identifier - jane - and the capitalized string - 'Jane' - that is passed in for the name argument. This situation will also hold true below with the Planet class - the name of the planet is Venus (and can be passed as 'Venus') but the name of the object should be venus.

A. Define a new class called Planet. (remember the case conventions!) Write a constructor for the class that takes four parameters: self, name (e.g., Mars), radius (of the planet), and mass (of the planet). All these parameters should be required. Be sure to include a docstring that gives the expected units for each parameter.

B. Add a fifth parameter to the constructor: the number of moons. This last parameter should be optional, and if not given, the default number of moons should be zero.

C. Add four get methods to the Planet class: get_name, get_radius, get_mass, and get_moons. Be sure to test each of these methods to make sure they work! They should all RETURN values (not print the values).


In [1]:
# Planet class definition at the end of Part 1

Test your code to make sure that the class definition worked.


In [2]:
# code to make sure constructors and get methods all work

part 2: Remaining Methods

In this part, you should test your class after each step. Each of the methods should RETURN, not print, values.

You should NOT need to add any new instance variables to your class in this part. You should be able to write the methods using local variables and parameters only.

D. Add one set method to the Planet class: set_moons. Be sure to check for invalid values, if any exist. Test the method to make sure it works, before proceeding to the next step!

E. Add a calculate_volume method to the Planet class that returns the volume of the Planet. You may assume a planet is a perfect sphere. Assuming r is the sphere’s radius, the formula for volume (V) is: $V = \frac{4}{3}\pi r^3$. Test the method to make sure it works, before proceeding to the next step!

F. Add a calculate_surface_area method that returns the surface area of the Planet, using the formula: $A = 4 \pi r^2$. Test the method to make sure it works, before proceeding to the last step!

G. Add a calculate_density method that returns the density of the Planet, using the formula: $density = \frac{mass}{volume}$. Take advantage of the methods you have already defined in the class. Don't forget to test everything before continuing.

part 3: Final Class Definition

Copy and paste your Planet class here:


In [ ]:

Write code that defines a Planet object called mars. Use the constructor to set its name, radius, and mass (see http://solarsystem.nasa.gov/planets/mars/facts for the correct values). Then print its volume, surface area, and density, by calling the appropriate methods.


In [ ]: