Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Part 1 - OOP with Animals This first part is a quick problem to get comfortable with OOP structuring. It is autograded, only worth

Pythonimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Part 1 - OOP with Animals This first part is a quick problem to get comfortable with OOP structuring. It is autograded, only worth 10% of the assignment, and should take you 20 minutes to complete. Create classes to represent the following hierarchy using inheritance in a module called Animals.py. Figure 1: Taxonomy to implement Implement the following functionality: a) Every class should be initialized with a name - call this instance variable name. Because all objects share this behavior, you can put it in the Animal constructor __init_.. b) Every class except ComputerScientist should define its own speak() method, which returns a string giving the name and an appropriate sound for that class. We only test the actual return string for Cat.speak, but we do test that the other objects all implement thier own speak method. >c1=Cat(Babs)>c1.speak()BabssaysMeow! c) Do not implement speak in the Computerscientist subclass - it should default to whatever method you define for Primate. d) The Animal superclass should define a method called reply(), which just calls the relevant speak: >c1=Cat( 'Babs') > c1.speak() \# should call Cat.speak() 'Babs says Meow!' > c1.reply() \# should call Animal.reply 'Babs says Meow!' Card games are a classic application of OOP. They let us use composition (decks of cards contain several card objects) and inheritance (a hand of cards can be treated as a specialized deck). The diagram below shows the inheritance model and the specific instance variables and bound methods we'll implement here. 1) \begin{tabular}{|l|} \hline \multicolumn{1}{|c|}{ Hand } \\ \hline . card_list \\ \hline .init() \\ . len() \\ .sort() \\ .repr() \\ -play (card) \\ \hline \end{tabular} Figure 2: (a) Class diagrams and (b) attributes for each class. Inherited attributes are in blue. Use TDD to create the classes above in a file called Cards.py, writing unittests as you go in a file called TestCards.py. TestCards.py Some cool OOP effects you'll encounter: - Composition - Decks are comprised of (a list of) Card objects - Inheritance - Some Hand are directly inherited from Deck and do not need to be coded at all We explicitly define the expected input/ouput of each method (the interface) below, but most of them do what you'd intuit. Before you start implementing code, review these guidelines: - Use TDD. Write a unittest first, then implement functionality. - This includes any exceptions you should raise. See the unittest basic example for an illustration of how to test that an error is raised (link) - Your final TestCards.py class should include 3 classes - one for each class you are trying to test: from Cards import Card, Deck, Hand import unittest class TestCard (unittest. TestCase): "Test cases specific to the Card class" def test_init(self): "Add a docstring here" class TestDeck(unittest. TestCase): \# your tests here class TestHand(unittest.TestCase): \# your tests here unittest.main() \# Runs all tests above - Structure your code based on OOP principles. Hand should inherit from Deck, and should not overload any methods unless it needs to. - Every method (including your unittests) should have a docstring. - When writing tests, create your own examples. Do not use any of the examples shown below. Card - init - initialize a new card based on the specified parameters: - value - the value of a card (i.e. the 3 in 3 of hearts) - suit - the suit of a card (i.e. the hearts in 3 of hearts) - repr - return something like 'Card (3 of hearts)' - lt - Implement as a magic method (__lt__) so it can be called with the standard operator (c1=Card(3, 'hearts' ) > repr(c1) 'Card (3 of hearts) ' >c2=card(3, 'spades' ) >c1c3 len(h_clubs) 4 5 > h_clubs.play (Card (1, 'clubs')) "Card (1 of clubs)" > h_clubs.play (Card(1, 'clubs')) Traceback (most recent call last): RuntimeError: Attempt to play Card(1 of clubs) that is not in Hand: [Card(5 of clubs),.. Submitting At a minimum, submit the following files: - Animals.py - Cards.py - TestCards.py

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

What do you plan on doing upon receiving your graduate degree?

Answered: 1 week ago