Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For simple Python Inheritance: Create a class called Thing for representing physical objects in the game. It should have: Two public attributes: name (a string

For simple Python Inheritance: Create a class called Thing for representing physical objects in the game. It should have: Two public attributes: name (a string used to identify the object) and location (a string used to identify the location of the object). An appropriate constructor that takes two extra arguments: a string to be used as the nameand a string to be used as the location. A method called .description() that returns a string that describes the object. For simple Things, the description should be of the form "The name is location."

Create a subclass of Thing called Openable for representing those physical objects that can be opened. In addition to the methods and attributes it automatically inherits from Thing, it should have: A public boolean attribute called is_open that represents whether or not the object is open. An overridden constructor that takes two strings and an optional boolean value (representing whether the object starts off open or not). If it's not specified, the object should start out closed. Your version of .__init__() for Openable must use the version of .__init__()from the superclass Thing to handle the name and location. After doing that, then it should set is_open separately. You should be able to create instances of Openable with commands like Openable("small window", "in the north wall", True) (returning an object that starts out open) or Openable("Necronomicon", "on the book shelf")(returning an object that starts out closed). To access the the version of .__init__() for the superclass, use super().__init(...) instead of self.__init(...)

An overridden version of .description(). For an Openable, the method should return a string of the form "The name location is open." or "The name location is closed."depending on whether it's open or closed. It would not be appropriate to use the version of .description() for the superclass Thing here. A method called .try_open() that takes no extra arguments and attempts to open the object. For simple Openable objects, if the object is closed to begin with, then this method will succeed (meaning that the object is changed to be open) and it should return True to indicate that the attempt was successful. If the object is already open when .try_open() is called, then the method will fail (nothing is changed) and it should return False to indicate that the attempt was not successful.

Create a subclass of Openable called Lockable for representing physical objects that can be unlocked and opened. In addition to the methods and attributes it automatically inherits from Openable and Thing, it should have: A public boolean attribute called is_locked that represents whether or not the object is locked. A public attribute called key which is a Thing object that represents the item that can be used as akey to unlock the Lockable object. An overridden constructor that takes two strings (the name and the location), a Thing (the key), and two optional boolean values (first, whether the object starts off open and second, whether it starts out locked). If neither of the optional boolean values are given, then the object should default to being closed but not locked. If only the first of the optional arguments (the starting "openness" state) is given, then the object should start out unlocked.

Your version of .__init__() for Lockable must use the version of .__init__() from the superclass Openable to handle the name, location, and "openness" state. Setting the is_locked and key attributes are the only ones you need to handle separately. An overridden version of .description(). For a Lockable, the method should return a string of the form "The name location is open." or "The name location is closed but unlocked." or "The name location is locked." depending on whether it's open, closed and unlocked, or closed and locked.

An overridden version of .try_open() that acts just like .try_open() for Openables when the object is unlocked, but fails to open the object if it is locked. As before: .try_open() should return True and change the obect to an open state if the attempt to open the object is successful (that is, if it started out closed but not locked) and False if the attempt fails (that is, if it was originally locked or if it was already open). A method called .try_unlock_with() that takes one extra argument: a Thingrepresenting the object that is being used as a key. This method should change the Lockable's status to unlocked (and return True) if and only if the Lockable object was closed and locked and the right key was being used (i.e., it matches the Thing designated as the Lockable's .key attribute). If the attempt fails, then nothing should be changed, and .try_unlock_with() should return False. To be clear, there are two reasons why it might fail: the object was already unlocked, or the Thing being used as a key doesn't match the .key of the Lockable.

Write some code to test your class. As in previous assignments, for each test, your tester should display a message describing what it is about to do before carrying out each test command. If the command is supposed to change something, the tester should also display the state of the object before and after carrying out the test command. And finally, if the command is supposed to return something, the test command should hang on to that returned value, and the tester should display that returned value after the command is run. Here are the minimum requirements for your test code:

Create at least 5 test objects using the appropriate constructors: a simple Thing, two Openable objects (one created using two arguments and one created using three), and three Lockable objects (one created using three arguments, one using four, and one using five).

Display the descriptions of objects at least 6 times. You need to display the description of an ordinary Thing, of an Openable that is open, of an Openable that is closed, of a Lockable that is open, of a Lockable that is closed and unlocked, and of a Lockable that is closed and locked. Note that since you can use the .description() method as a way of checking the state of the object before and after you change it, you'll probably end up doing this anyway as part of the test code for the other parts.

Try to open Openable objects at least 5 times. You need to try to open an Openable that is already open, an Openable that is closed, a Lockable that is already open, a Lockablethat is closed and unlocked, and a Lockable that is closed and locked. Remember to display the status (I recommend using .description()) before and after trying to open, and also to display whether the returned value was True or False.

Try to unlock Lockable objects at least 4 times. You need to try to open a Lockable that is already open (using the correct key), a Lockable that is closed and unlocked (using the correct key), a Lockable that is closed and locked (using the correct key), and a Lockablethat is closed and locked (using the wrong key). Remember to display the status (I recommend using .description()) before and after trying to open, and also to display whether the returned value was True or False.

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

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

4. I can tell when team members dont mean what they say.

Answered: 1 week ago