Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python The aim of this first assignment is to further strengthen your Python abilities by applying basic OO concepts, and to start thinking about

In python

The aim of this first assignment is to further strengthen your Python abilities by applying basic OO concepts, and to start thinking about algorithm complexity.

The requirements of the assignment are to implement code that demonstrates the following concepts, using the code below as a starting point.

  1. Inheritance: Define a parent class (e.g. Mammal), and two (or more) child classes (e.g. Elephant, Hippo) which are derived from the parent
  2. Polymorphism: Implement methods in each of the children that override a method in the parent, and that can still be used without needing to know what type of child it is
  3. Accessor: Implement an accessor method and demonstrate its functionality
  4. Operator override: Override an operator to allow for children classes to be evaluated/compared (e.g. "==")
  5. Modify runtime: Modify the 'eat' method so that it runs in O(log n) time, even though this may not be physically possible (note: log should be base = 2)

What to hand in?

A single *.py file should be submitte

What needs to be done exactly?

Begin with the starter code shown here and add all of the necessary logic and code to complete the requirements described above. Note that you will be mostly be adding new code to what is below. The only exceptions are a) modifying the current eat() method so that it runs in O(log n) time, and b) deleting the comments in ALL CAPS and replacing them with what it is asking for.

import unittest import time class Mammal: """ A Mammal class to further populate our animal kingdom """ def __init__(self, species): """ mammal constructor can initialize class attributes """ self.species = species self.name = None def eat(self, food): """ a method that will 'eat' in O(n) time """ i = food print(self.name, "the", self.species, "is about to eat") while i >= 1: time.sleep(0.1) i -= 1 print(" ", self.name, "is done eating!") def makeNoise(self): """ a method that should be implemented by children classes """ raise NotImplementedError("this method should be implemented by child class") # ADD ANY OTHER BASE CLASS METHODS YOU NEED/WANT TO HERE # THE FOLLOWING TWO CLASSES NEED TO BE COMPLETED, AND YOU # NEED TO REPLACE/DELETE ALL OF THE ELLIPSES SHOWN BELOW class Hippo ... ... ... class Elephant ... ... ... class TestMammals(unittest.TestCase): """ a class that is derived from TestCase to allow for unit tests to run """ def testInheritance(self): """ confirm that Elephant and Hippo are children classes of Mammal """ self.assertTrue(issubclass(Elephant, Mammal) and issubclass(Hippo, Mammal)) # ADD YOUR OTHER TESTS HERE, AT LEAST 2 MORE TO FULFILL TESTING RUBRIC CRITERION def main(): """ a 'main' function to keep program clean and organized """ print("-------------------- start main --------------------") # create instances of child classes e = Elephant("Ellie") h = Hippo("Henry") # compare classes with overriden == operator, and call accessor method if(e == h): print(e.getName(), "and", h.getName(), "are of the same species") else: print(e.getName(), "and", h.getName(), "are *not* of the same species") # a function to help demonstrate polymorphism def listenToMammal(Mammal): print(Mammal.makeNoise()) # polymorphism in action: treating different classes in the same way listenToMammal(e) listenToMammal(h) # feed Ellie 10 bites of food (and see how long it takes!) e.eat(100) print("--------------------- end main ---------------------") # this will run when the file is called as a standalone program (ex: python assign1.py) if __name__ == "__main__": main() unittest.main() 

How should it work?

Once you have implemented the classes above, then all of the code in 'main' should run correctly. An example of the output when this is run correctly is here:

Misc Tips and Hints

  • see the rubric below for exact scoring of the assignment, and note that the 5 requirements above are divided up evenly for each rubric criteria (e.g. each requirement above is 1/5 of 30 = 6 points for the Program Execution/Output criterion)
  • be sure to implement two additional unit tests to test some of the other assignment requirements (e.g. test '==' operator, accessor method, etc.)
  • be sure to delete and replace the comments that are in ALL CAPS; if these are not deleted/replaced then some points will be deducted from the Coding Standards/Documentation criterion

Rubric

Programming Assignment Rubric

Programming Assignment Rubric

Criteria Ratings Pts This criterion is linked to a Learning OutcomeProgram Execution/Output 30 to >25.0 pts Good Program executes with no syntax or runtime errors and produces nearly all of the appropriate output when run through the test script (26pts - 30pts). 25 to >10.0 pts Fair Program executes and produces some of the desired/correct output (11pts - 25pts). 10 to >0 pts Poor Program does not execute without some modification and/or produces none or very little of the desired output in the test script (0pts - 10 pts). 30 pts This criterion is linked to a Learning OutcomeCode Logic/Design 40 to >30.0 pts Good Program has all required classes, variables, and methods defined, and the logic is (mostly) correct (31pts - 40pts). 30 to >10.0 pts Fair Program has some but not all requisite classes, methods, or variables and/or the logic of how they are implemented is incorrect (11pts - 30pts). 10 to >0 pts Poor Program has few or none of the classes, variables, or methods that were required and/or it is evident that their logic is incorrect (0pts - 10pts). 40 pts This criterion is linked to a Learning OutcomeCoding Standards/Documentation 20 to >15.0 pts Good Program has documentation for each class, method, function and this can be displayed when using Python's help function, "help()". Code is consistently formatted (e.g. spacing, indenting), and methods/variables are named using a consistent style. If there is starter code supplied, then all comments in CAPS have been removed/replaced (16pts - 20pts). 15 to >5.0 pts Fair Program has some documentation and is somewhat consistent in formatting, variable/method naming, etc. (6pts - 15pts). 5 to >0 pts Poor Program has little to no documentation, is not structured/formatted well, and does follow a consistent approach towards naming variables/method (0pts - 5pts). 20 pts This criterion is linked to a Learning OutcomeTesting 10 to >6.0 pts Good Program has at least two (2) unit tests implemented using unittest and/or there are rigorous checks for valid data types/ranges/etc. (7pts - 10pts). 6 to >2.0 pts Fair Program has one (1) test using unittest and/or checks possible edge cases manually (e.g. with if statements in methods) (3pts - 6pts). 2 to >0 pts Poor Program does not use unittest and does not have any manual checks for valid data types/ranges/etc. (0pts - 2pts). 10 pts Total Points: 100

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions