Question

In: Computer Science

Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...

Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where instances of the class are created, and the methods are tested by giving the required details and printing the details for each class

Solutions

Expert Solution


import math

class Shape:

def __init__(self, side, color='black', fill=False):
self.__color = color
self.__fill = fill
self.__side = side

def get_color(self):
return self.__color

def set_color(self, color):
self.__color = color

def get_fill(self):
return self.__fill

def set_fill(self, fill):
self.__fill = fill

def get_side(self):
return self.__side

def set_side(self,side):
self.__side = side


class Rectangle(Shape):

def __init__(self, length, breadth,side):
super().__init__(side)
self.__length = length
self.__breadth = breadth

def get_length(self):
return self.__length

def set_length(self, length):
self.__length = length

def get_breadth(self):
return self.__breadth

def set_breadth(self, breadth):
self.__breadth = breadth

def get_area(self):
return self.__length * self.__breadth

def get_perimeter(self):
return 2 * (self.__length + self.__breadth)

class Circle(Shape):
def __init__(self, radius,side):
super().__init__(side)
self.__radius = radius

def get_radius(self):
return self.__radius

def set_radius(self, radius):
self.__radius = radius

def get_area(self):
return math.pi * self.__radius ** 2

def get_perimeter(self):
return 2 * math.pi * self.__radius


r1 = Rectangle(10.5, 2.5,4)

print("Number of sides in rectangle r1:",r1.get_side())
print("Area of rectangle r1:", r1.get_area())
print("Perimeter of rectangle r1:", r1.get_perimeter())
print("Color of rectangle r1:", r1.get_color())
print("Is rectangle r1 filled ? ", r1.get_fill())
r1.set_fill(True)
print("Is rectangle r1 filled ? ", r1.get_fill())
r1.set_color("orange")
print("Color of rectangle r1:", r1.get_color())

c1 = Circle(12,0)

print("\nNumber of sides in circle c1:",c1.get_side())
print("\Area of circle c1:", format(c1.get_area(), "0.2f"))
print("Perimeter of circle c1:", format(c1.get_perimeter(), "0.2f"))
print("Color of circle c1:", c1.get_color())
print("Is circle c1 filled ? ", c1.get_fill())
c1.set_fill(True)
print("Is circle c1 filled ? ", c1.get_fill())
c1.set_color("blue")
print("Color of circle c1:", c1.get_color())

Output:

Number of sides in rectangle r1: 4
Area of rectangle r1: 26.25
Perimeter of rectangle r1: 26.0
Color of rectangle r1: black
Is rectangle r1 filled ?  False
Is rectangle r1 filled ?  True
Color of rectangle r1: orange

Number of sides in circle c1: 0
\Area of circle c1: 452.39
Perimeter of circle c1: 75.40
Color of circle c1: black
Is circle c1 filled ?  False
Is circle c1 filled ?  True
Color of circle c1: blue

Related Solutions

Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line...
Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line using nested loops. Here is what you need to know: 1- Copy this and don't change the inputs in the provided code: # Get the size and drawing character from the user size = input('Please enter the size: ') # Validate the input, exit if bad if size.isdigit(): size = int(size) else: print("Exiting, you didn't enter a number:", size) exit(1) # Input the drawing...
Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
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) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
Write a Python graphics program that draws the following shapes: • window size: 250 x 250...
Write a Python graphics program that draws the following shapes: • window size: 250 x 250 pixels with window title with your name • big circle, 50 pixels radius with center at (125, 125) • two green circles, 10 pixels radius; first one centered at (113, 113) and second centered at (137, 113) • one red line, from (100, 150) to (150, 150) Then answer this, what do you see? (make this a comment in your code)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT