Question

In: Computer Science

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

Python Program:

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

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.

Sample Output (Your output should be similar or can be the same 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.

Solutions

Expert Solution

class Rectangle:
def __init__(self,length,width):
self.length=length
self.width=width
def area(self):
return self.length*self.width
def perimeter(self):
return 2*self.length+2*self.width

class Square(Rectangle):
def __init__(self,length):
super().__init__(length,length)

if __name__== "__main__":
print("Rectangle Calculator")
while True:
c=input("Rectangle or Square? (r/s): ")
if c=='r':
l=int(input("Heigth: "))
b=int(input("Width: "))
rect=Rectangle(l,b)
print("Perimeter: ",rect.perimeter())
print("Area: ",rect.area())
if c=='s':
l=int(input("Length: "))
squa=Square(l)
print("Perimeter: ",squa.perimeter())
print("Area is: ",squa.area())
s=input("Continue? (y/n): ")
if s=='n':
break
print("Thank you for using my app")

Output:


Related Solutions

This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
IN JAVA Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Python: Using Inheritance to Create a Derived Class in Python In this lab, you create a...
Python: Using Inheritance to Create a Derived Class in Python In this lab, you create a derived class from a base class, and then use the derived class in a Python program. The program should create two Motorcycle objects, and then set the Motorcycle’s speed, accelerate the Motorcycle object, and check its sidecar status. Instructions Open the file named Motorcycle.py. Create the Motorcycle class by deriving it from the Vehicle class. Use a public derivation. In the Motorcycle class, create...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer information in the list and to search for a customer by specifying the customer’s ID. Sample Run Customer Information Management System --------------------------------------------------------------- CUSTOMER DATA ENTRY: Full Name (First, Last): Jenny Ha Company: Convergent Laser Technologies Street: 1000 International Ave City: Oakland State: CA Zip Code: 94506 ID: 100 Continue Your Data Entry? (y/n): y Full Name (First, Last): Bill Martinez Company: Cisco Systems Street:...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
write a python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT