Question

Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

Description
Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square.

Sample Output (Your output should be similar to the text in the following box)

Rectangle Calculator
Rectangle or square? (r/s): r
Height: 5
Width: 10
Perimeter: 30
Area: 50

Continue? (y/n): y

Rectangle or square? (r/s): s
Length: 5
Perimeter: 20
Area: 25

Continue? (y/n): n

Thank you for using my app

Specifications

  • Use a Rectangle class that provides attributes to store the height and width of a rectangle. This class should also provide methods that calculate the perimeter and area of the rectangle.
  • Use a Square class that inherits the Rectangle class. This class should set the height and the width of the Rectangle superclass to the length that’s set in the Square subclass.
  • The program should determine whether the user wants to enter a rectangle or a square.
  • For a rectangle, the program should get the height and width from the user.
  • For a square, the program should get the length of the square from the user.

Submission Instructions

  1. Write the code to create the program described above.
  2. Save your Python source code.
  3. Compile, run, and submit the following items (DO NOT submit a zip file) on Canvas:
    • Source code file: Main.py
    • Output screenshot file: Output.jpg or Output.png on IDLE or Thonny.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Python code for above problem

# parent class
class Shape( object ):
   def __init__(self, height, width):       # constructor that initialises the object with values
       self.height = height
       self.width = width
   def area(self):                           # method that prints the area
       print("Area:",self.height*self.width)
   def perimeter(self):                   # method that prints the perimeter
       print("Perimeter:",2*(self.height+self.width))

# child class
class Rectangle( Shape ):          
   def __init__(self, height, width):
       Shape.__init__(self, height, width)

class Square( Shape ):          
    def __init__(self, length):
       Shape.__init__(self, length, length)

def main():
   while(True):
       shape=input("Rectangle or Square? (r/s): ")
       if(shape[0]=='r'):
            height=int(input("Height: "))
            width=int(input("Width: "))
            rectangle=Rectangle(height, width)
            rectangle.perimeter()
            rectangle.area()
       elif(shape[0]=='s'):
            length=int(input("Length: "))
            square=Square(length)
            square.perimeter()
            square.area()
       else:
            print("Invalid option")
       choice=input("Continue? (y/n): ")
       if(choice[0]!='y'):
            break
   print("Thank you for using my app")
main()

Sample output:

Rectangle or Square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or Square? (r/s): s Len

# Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...

    Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

  • Use DevC++ to implement a program uses objected oriented programming to calculate the area of a...

    Use DevC++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • Use C++ to implement a program uses objected oriented programming to calculate the area of a...

    Use C++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • ASAP Please. Python Program Description Create an object-oriented program that allows you to enter data for...

    ASAP Please. Python Program Description Create an object-oriented program that allows you to enter data for employees. Specifications Create an abstract Employee class that provides private attributes for the first name, last name, email address, and social security number. This class should provide functions that set and return the employee’s first name, last name, email address, and social security number. This class has a function: get_net_income which returns 0. Create a Manager class that inherits the Employee class. This class...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • Go to the Java API and review the Rectangle class briefly. As directed below, create a...

    Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...

  • C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle...

    C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle Class Derive a Square Class from the Rectangle Class Derive a Right Triangle Class from the Rectangle Class Create a Circle Class Create a Sphere Class Create a Prism Class Define any other classes necessary to implement a solution Define a Program Driver Class for Demonstration a) Create a Container to hold objects of the types described above b) Create and Add two(2) objects...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT