# Initialize Otter
import otter
grader = otter.Notebook("hw9-classes.ipynb")

Homework 9#

This assignment includes three problems on the topic of classes.

Entering Your Information for Credit#

To receive credit for assignments it is important we can identify your work from others. To do this we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

# Please provide your first name, last name, Drexel ID, and Drexel email. Make sure these are provided as strings. "STRINGS ARE TEXT ENCLOSED IN QUOTATION MARKS."

# In the assignments you will see sections of code that you need to fill in that are marked with ... (three dots). Replace the ... with your code.
first_name = ...
last_name = ...
drexel_id = ...
drexel_email = ...
grader.check("q0-Checking-Your-Name")

Entering Your Information for Credit#

To receive credit for assignments, it is important we can identify your work from others. To do this, we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

Question 1: Nutritional Information

Your task is to write a class called FoodItem. The class constructor should initialize a new food item given a name and amounts of fat, carbs, and protein (in grams). The class should also have a method called get_calories which returns the estimated amount of calories in a serving of the food item.

To estimate calories \(x\) given the grams of fat \(f\), carbs \(c\), and protein \(p\), you should use the following approximation:

\[x = 9f + 4c + 4p\]

Write python code to do the following:

  • Define a class called FoodItem.

  • Define the class constructor to accept as arguments a string name and floats fat, carbs, and protein. The values from these argument should be stored as data members, so that they can be used in other methods.

  • Define a method within FoodItem called get_calories, which accepts no arguments (other than self) and returns the estimated calories, as a float.

Your code replaces the prompt: ...

...


# This creates a FoodItem object so you can test your class
snack = FoodItem("M&Ms", 10.0, 34.0, 2.0)
# This calls the get_calories method on the newly created FoodItem object
print(snack.get_calories())
grader.check("q1-nutrition")

Question 2: Area and Perimeter of a Circle

Your task is to write a class called Circle. The class constructor should take as input a radius that defaults to a value of 1.0. The class should have methods called area and perimeter which return as floats the area and perimeter of the circle, respectively.

Be sure to reference math.pi in your calculations.

Write python code to do the following:

  • Define a class called Circle.

  • Define the class constructor to accept as argument a float radius with a default value of 1.0. This value should be stored as a data member, so that it can be used in other methods.

  • Define a method within Circle called area, which accepts no arguments (other than self) and returns the area, as a float.

  • Define a method within Circle called perimeter, which accepts no arguments (other than self) and returns the perimeter, as a float.

Your code replaces the prompt: ...

...


# This creates a Circle object so you can test your class
c = Circle(8.0)
# These call the area and perimeter methods on the newly created Circle object
print(c.area())
print(c.perimeter())
grader.check("q2-area-perimeter-circle")

Question 3: 3D Point Class

Your task is to create a class called Point3D that implements a point in 3-D space. Objects of this class are constructed with three arguments x,y,and z, which all default to zero.

Inside the class you will implement a number of β€œmagic” methods in python, the methods with names surrounded by double underscores. Specifically, you will implement the proper methods to allow addition, subtraction and equality checking between two Point3D instances. You will also implement the proper method to control how a Point3D object is printed.

Specifics regarding how these should be implemented follow.

Write python code to do the following:

  • Define a class called Point3D.

  • Define the class constructor to accept as arguments 3 numbers x, y, and z which all default to 0. These values should be stored as data members (called x, y, and z), so that they can be used in the other methods.

  • Addition: define a method such that, given Point3D objects a and b, a + b returns a new Point3D object whose x, y, and z values are the sums of the x, y and z values of points a and b. (Hint: the built in function for addition is __add__())

  • Subtraction: define a method such that, given Point3D objects a and b, a - b returns a float representing the Euclidean distance between points a and b.

  • Equality: define a method such that, given Point3D objects a and b, a == b returns True if and only if the x, y and z values of point a and b are equal.

  • Printing: define a method such that, given a Point3D object a, print(a) prints in the format <a.x, b.y, c.z>. (Hint: the built in print function will call the __str__() method on the input to convert the input into a string before printing.)

After your class has been implemented, it should be able to be used as follows:

Your code replaces the prompt: ...

...


# You can manipulate these points (and make others!) to test your class 
p1 = Point3D(1,1,1)
p2 = Point3D(2,2,2)
grader.check("q3-point3d")

Submitting Your Assignment#

To submit your assignment please use the following link the assignment on GitHub classroom.

Use this link to navigate to the assignment on GitHub classroom.

If you need further instructions on submitting your assignment please look at Lab 1.

Viewing your score#

It is your responsibility to ensure that your grade report shows correctly. We can only provide corrections to grades if a grading error is determined. If you do not receive a grade report your grade has not been recorded. It is your responsibility either resubmit the assignment correctly or contact the instructors before the assignment due date.

Each .ipynb file you have uploaded will have a file with the name of your file + Grade_Report.md. You can view this file by clicking on the file name. This will show you the results of the autograder.

We have both public and hidden tests. You will be able to see the score of both tests, but not the specific details of why the test passed or failed.

Note

In python and particularly jupyter notebooks it is common that during testing you run cells in a different order, or run cells and modify them. This can cause there to be local variables needed for your solution that would not be recreated on running your code again from scratch. Your assignment will be graded based on running your code from scratch. This means before you submit your assignment you should restart the kernel and run all cells. You can do this by clicking Kernel and selecting Restart and Run All. If you code does not run as expected after restarting the kernel and running all cells it means you have an error in your code.

Fin#