lab02 : Class Inheritance

num ready? description assigned due
lab02 true Class Inheritance Sun 10/10 11:59PM Sun 10/17 11:59PM

In this lab, we’ll utilize some inheritance functionality and define various 2D shapes and its properties. You’ll have the opportunity to practice:

Note: In general, it is always important to work on labs and reading early so you can gain the proper context and utilize our office hours to seek assistance / ask clarifying questions during the week before the deadline if needed!

It may be a good idea to read up on some tools we’ll be using in this lab before you get started, specifically Chapter 1.4.6.2 (Inheritance).

In this lab, we will create a new Shape2D base class as well as defining specific classes for a couple of 2D shapes that will inherit from the Shape2D class.

In addition to defining classes for various shapes, you should test your code for correctness by unit testing various scenarios using pytest.

Instructions

You will need to create four files:

There will be no starter code for this assignment, but rather the class descriptions and required methods are defined in the specification below.

It’s recommended that you organize your lab work in its own directory. This way all files for a lab are located in a single folder. Also, this will be easy to import various files into your code using the import / from technique shown in lecture.

Shape2D.py class

The Shape2D.py file will contain the definition of what a general 2D Shape is.

We will define this class’ attributes as follows:

You should write a constructor that allows the user to construct a Shape2D object by passing in values for all the fields. You may assume calls to the constructor will always contain a str representing the 2D Shape’s color.

In addition to your constructor, your class definition should also support “setter” and “getter” methods that can update and retrieve the state of the Shape2D objects:

Each 2D Shape object should be able to call a method getShapeProperties(self) that you will implement, which returns a str with minimal 2D Shape information. Since a 2D Shape can be many things, the following output represents what will happen if we call the getShapeProperties method after constructing a Shape2D object:

s1 = Shape2D("blue")
print(s1.getShapeProperties())

Output:

Shape: N/A, Color: blue

Note: The s1.getShapeProperties() return value in the example above does not contain a newline character (\n) at the end.

Circle.py

The Circle.py file will contain the definition of what a 2D Circle will have. Since a Circle IS-A 2D Shape, we will inherit the values we defined in the Shape2D class. Since this lab focuses on inheritance, it is important that you only provide the method definitions below (you will not receive full credit if you implement other methods in the Circle class definition, even if Gradescope’s tests pass).

Your Circle class definition should support the following constructor / methods:

c1 = Circle("blue", 2.5)
print(c1.getShapeProperties())

Output:

Shape: CIRCLE, Color: blue, Radius: 2.5, Area: 19.6349375, Perimeter: 15.70795

Note: The c1.getShapeProperties() return value in the example above does not contain a newline character (\n) at the end.

Square.py

The Square.py file will contain the definition of what a 2D Square will have. Since a Square IS-A 2D Shape, we will inherit the values we defined in the Shape2D class. Since this lab focuses on inheritance, it is important that you only provide the method definitions below (you will not receive full credit if you implement other methods in the Square class definition, even if Gradescope’s tests pass).

Your Square class definition should support the following constructor / methods:

s1 = Square("blue", 2.5)
print(s1.getShapeProperties())

Output:

Shape: SQUARE, Color: blue, Side: 2.5, Area: 6.25, Perimeter: 10.0

Note: The s1.getShapeProperties() return value in the example above does not contain a newline character (\n) at the end.

testFile.py pytests

This file will contain unit tests using pytest to test if your functionality is correct. Think of various scenarios and method calls to be certain that the state of your objects and return values are correct (provide enough tests such that all method calls in Shape2D, Circle, and Square are covered). Even though Gradescope will not use this file when running the automated tests, it is important to provide this file with various test cases (testing is important!!). We will manually grade your testFile.py to make sure your unit tests cover the methods in Shape2D, Circle, and Square.

Pytest will need to be installed on your computer since it does not come with Python by default. Some links for you to use when installing pytest are:

Submission

Once you’re done with writing your class definition and tests, Submit your Shape2D.py, Circle.py, Square.py, and testFile.py files to the Lab02 assignment on Gradescope. There will be various unit tests Gradescope will run to ensure your code is working correctly based on the specifications given in this lab.

If the tests don’t pass, you may get some error message that may or may not be obvious at this point. Don’t worry - if the tests didn’t pass, take a minute to think about what may have caused the error. Try to think of your pytests and see if you can write a test to help you debug the error (if you haven’t already). If you’re still not sure why you’re getting the error, feel free to ask your TAs or Learning Assistants.