Question

In: Computer Science

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

  1. 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


I did Already half of the question can you please complete on it cause this is what is required:(The Circle Test Code didn't work please redo the Circle and complete the question requirements:

class Shape:

    def __init__(self,color,fn,no_of_sides):
        self.color=color
        self.filledornot=fn
        self.no_of_sides=no_of_sides
        self.sides = []

    def inputSides(self):
        for i in range(self.no_of_sides):
            self.sides.append(float(input("Enter side " +str(i+1)+" : ")))


    def display(self):
        print("color: " , self.color)
        print("Filled: ", self.filledornot)
        for i in range(self.no_of_sides):
            print("Side ",i+1,"is", self.sides[i])

    def area(self):
        print("Area")

class Rectangle(Shape):

    def __init__(self):
         Shape.__init__(self,"Red",True,2)

    def area(self):
        l , w = self.sides
        calarea=l*w
        print("Area=" , calarea , "cm**2")


class Circle(Shape):
    '''Class to represent the circle shape'''

    # constructor
    def _init_(self):
        Shape.__init__(self, "White", True, 0)

    # area function for the circle
    def area(self):
        radius = self.sides[0]
        area_circle = 3.14*radius*radius
        print("Area= ", area_circle)

#Test Code
print('Rectangle')
r = Rectangle()
r.inputSides()
print('-'*30)
r.display()
r.area()
print('-'*30)

print('Circle')
c = Circle()
c.inputSides()
print('-'*30)
c.display()
c.area()
print('-'*30)

Solutions

Expert Solution

First error in the cirlce class was that the contructor must be __init__ > double underscore , but you used a single underscrore,
Second was the number of side , circle must sedn one side(for radius)

Code

class Shape:

    def __init__(self, color, fn, no_of_sides):

        self.color = color

        self.filledornot = fn

        self.no_of_sides = no_of_sides

        self.sides = []

    def inputSides(self):

        for i in range(self.no_of_sides):

            self.sides.append(float(input("Enter side " + str(i+1)+" : ")))

    def display(self):

        print("color: ", self.color)

        print("Filled: ", self.filledornot)

        for i in range(self.no_of_sides):

            print("Side ", i+1, "is", self.sides[i])

    def area(self):

        print("Area")


class Rectangle(Shape):

    def __init__(self):

        Shape.__init__(self, "Red", True, 2)

    def area(self):

        l, w = self.sides

        calarea = l*w

        print("Area=", calarea, "cm**2")


class Circle(Shape):

    '''Class to represent the circle shape'''

    # constructor

    def __init__(self):

        Shape.__init__(self, "White", True, 1)

    # area function for the circle

    def area(self):

        radius = self.sides[0]

        area_circle = 3.14*radius*radius

        print("Area= ", area_circle, "cm**2")


# Test Code

print('Rectangle')

r = Rectangle()

r.inputSides()

print('-'*30)

r.display()

r.area()

print('-'*30)

print('Circle')

c = Circle()

c.inputSides()

print('-'*30)

c.display()

c.area()

print('-'*30)

Screenshot

Output


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...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT