Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 1 2 A: Having a seat Sample output: You are about to create a chair. How many legs does your chair have: 8 Is

Lab12A: Having a seat Sample output:
You are about to create a chair.
How many legs does your chair have: 8
Is your chair rolling (true/false) : true
What is your chair made of: plastic
Your chair has 8 legs, is rolling, and is made of plastic.
This program is going to change that.
Your chair has 4 legs, is not rolling, and is made of wood. Lab12B: My dog can do tricks
For this lab we are going to create a Dog class.
Dog objects have a few attributes, but this time unlike chair objects they can also do some cool things
too. Each action is represented by a method. Therefore, for any action our Dog can do or we do to the
Dog, we are going to create a method. For example, if you want my Dog to bark, you can create a
method to do that in the Dog class and call that method outside of the class (once you have created
an object).
Dog class:
Variables (Attributes):
Behaviors (Methods):
0
0
bark
# prints "Woof! Woof!"
rename
0
eat
# take a string and change the name of the dog
take a float and add that number to weight
Keep in mind that methods inside of a class will always take the "self" parameter. This parameter is
always automatically passed whenever you call a method from an object. However, some methods
must take in more than just the "self" parameter; in this case, these extra parameters must be passed
before the method can be called.
Outside the Dog class, create a new Dog object and prompt the user to input the attributes describing
this Dog. Once done, print out all the details about the Dog, as per the sample output.
Next, use the methods you created in the Dog class to have it bark, change the name (using the
rename method, not the dot operator), and feed it.
Finally print out all the details about the Dog, the object should have changed because of your calls to
the various methods.
When learning about objects, you may hear the example "a chair is an object". Well, for this lab, we are
going to implement this example. Create a class called "Chair", which can create "Chair" objects.
Chair class:
Using these attributes you can describe most chairs. For example, you might have a wooden chair with 4
legs that does not roll, or you may have a rolling chair made of leather with 5 legs. The code above will
allow you to create Chair objects, each with their own copies of the attributes available to a Chair, as
below:
Another way for you to create a Chair class is by initializing the fields inside a constructor. Whenever
an object is created, the constructor is the first piece of code which runs, and it is used to initialize the
fields of the object of a "valid" state. In Python, the constructor is called () and looks like any
other method, with the exception that it always has at least one parameter, called "self". Its syntax can
be found below:
For the purposes of this lab, either of the two ways above to create a Chair class will work. However,
they can lead to different behavior, which we will see in the next lab.
This class isn't going to have any methods, so unless you are adding the constructor, your class
should only have the three attributes above.
Outside of the class body, create a Chair object. Then, take user input for the attributes for the Chair
object and assign them to the object. Once you have done that, print out the information about your
chair, as per the sample output below. Finally, inform the user you will be changing the information,
change the attributes of the Chair object to have 4 legs, to not be rolling, and to be made of wood, and
print the Chair's information one last time.
Tip: When dealing with rolling/not rolling you may want to use an IF statement with different print
statements.
See Sample Output in the next page. The user input is indicated in bold.
image text in transcribed

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

More Books

Students also viewed these Databases questions