5. (15 pts) Please answer the following questions about Object-Oriented Programming in python. (A) A "Rectangle" class is defined on the right. Please: (1) generate an instance "r" of class Rectangle, with x=y=0, and width = 4, height = 2 (2) print the width (3) change the width to 6 Your code: class Rectangle: def __init__(self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h @property def width(self): return self.w @width.setter def width(self, w): if w > 0: self.w = w (B) Given the code for class A, please answer: (1) what is the meaning of @classmethod? (2) what is the output of the following code: >>> a = A() >>> A.set_class_b() >>> a.b (Your answer: ) >>> A.b (Your answer: ) >>> a_new = A() >>> a_new.b (Your answer: ) (C) Please use "Rectangle" as superclass in the class A: b = 6 def set_b(self): self.b = 3 @classmethod def set_class_b(cls): cls.b = 7 class Square(Rectangle): def __init__(self, x, y, s):
Added by James S.
Close
Step 1
Step 1: Define the superclass Rectangle with the __init__ method that takes in the parameters x and y for the coordinates of the top-left corner of the rectangle, and w and h for the width and height of the rectangle. Show more…
Show all steps
Your feedback will help us improve your experience
Shelayah Robinson and 61 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
1.) Rectangle Area The area of a rectangle is calculated according to the following formula: Area= Width * Length Design a function that accepts a rectangle's width and length as arguments and returns the rectangle's area. Use the function in a program that prompts the user to enter the rectangle's width and length, and then displays the rectangle's area.
Shelayah R.
Question 11A (12 points) Write a definition line for a class named State and a one-line docstring that describes what a State is. Write definitions for the following methods (functions) in the code for the class: 1. An initialization method. The initialization method should: - Take a string parameter, name, and assign it as the variable name of the state being created. - Create a list variable, universities, for the state being created and initialize it to the empty list. 2. A method named addUniversity. This method should take the name of a university as a string parameter and add it to the list of universities for that state. 3. A method named is_home_of. This method should take the name of a university as a string parameter. If the university is in the state's list of universities, it should return True; otherwise, it should return False.
Akash M.
Given that you have a function f that accepts a kwargs argument and does not have a return. You also have a variable d that has been assigned a dictionary, write a line of code that passes variable d to function f. 2a) Complete the following code to build the states dictionary. The USPresidents.txt file has a number of lines of data, with each line containing two pieces of data: a two-letter state abbreviation and the name of a president born in that state. The dictionary keys should be the state abbreviation and the dictionary values should be a count of how many presidents were born in that state (this is just like one of your lab exercises). Only build the one dictionary – don't build any lists, etc. states = {} file = open("USPresidents.txt", "r") for line in file: 2b) Iterate through the states dictionary to print the key and value for each item in the dictionary. 3a) Write a complete Square class. It should have an __init__ method and a getArea method. The __init__ method should accept side (the length of the side of the square) as a parameter. 3b) Write a complete Cube class. It should inherit from the Square class and should have an __init__ method and a getVolume method. The __init__ method should accept side as a parameter. 3c) Initialize variable s with a square object with a side of 3. 3d) Print the area of square s. 3e) Initialize variable c with a cube object with a side of 4. 3f) Print the volume of cube c. 4a) Given that you have a Liquid class that can be initialized with any amount of gallons, liters, ounces, etc, write the first line of the method that will allow you to add together two Liquid objects using operator overloading. Again, write ONLY the first line (method definition) – you do NOT have to write the entire method. 4b) Given that you have L1 and L2 which are instances of the Liquid class, write a line of code that uses operator overloading to add them together and assign the result to L3.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD