Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in python please Lab 1 2 A: Having a seat When learning about objects, you may hear the example a chair is an object .

in python please
Lab12A: Having a seat
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:
class Chair:
# attributes of a chair
numOfLegs =4 # how many legs are on the chair
rolling = False # does it roll or not
material =plastic # what is the chair made of
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:
c1= Chair() # creates a Chair
print(c1.numOfLegs) # prints the numOfLegs attribute of c1(4)
c1.rolling = True # c1s rolling attribute is now True
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 __init__() 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:
class Chair:
def __init__(self, numOfLegs =4, rolling = False, material =plastic):
self.numOfLegs = numOfLegs
self.rolling = rolling
self.material = material
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 isnt 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 Chairs 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.
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.

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

Students also viewed these Databases questions

Question

Explain the Neolithic age compared to the paleolithic age ?

Answered: 1 week ago

Question

What is loss of bone density and strength as ?

Answered: 1 week ago