147k views
0 votes
Develop a program that creates three classes – Cube (actually a Cuboid), Circle, and

Square. For each class, provide methods accessor and mutator, method findArea() that
will calculate the area of the shape, and method display() that will print the class name
(Cuboid, Circle, or Square). Use private attributes. The constructor of each class should
set a default value for each shape. For class circle, set the radius to 4.3 cm, for class
Square set the side to 4.0 cm, and for class Cuboid set the length, width, and height to
your choices. Note – for the Cuboid shape, please calculate the surface area instead of the
volume.
Using an array/list and the loop concept, create ten objects of the given shapes above.
Which object to create and load into a list depends on your random number generator
between 0 to 2. If the random number is 0, then using the default constructor create and
load a Circle object into the array/list. If the random number is 1, then using the default
constructor create and load a Square object into the array/list. If the random number is 2,
then using the default constructor create and load a Cube object into the array/list.
Using a loop reiterates through your list of objects and call methods display() and
findArea() to print the class name and its area. Your program should offer the user three
options to print the results:
1. Save the results into a file. The program should ask the user to input a file name.
2. Print the results on the screen
3. Print the results inside GUI Windows

make sure the output looks like this.

welcome to my shape generator!
Please select which option to run the program:
1. Save the results into a file. The program will ask you to input
2. Print the results on screen.
3. Print the results inside out windows
4. Exit
Enter: 1
Enter name of file to save your result: datafinal
Please select which option to run the program:
1. Save the results into a file. The program will ask you to irout a file name.
2. Print the results on screen.
3. Print the results inside Guldas
Enter: 2
Shape: Cube, Area: 12.00
Shape: Cube, Area: 52.00
Shape: Ctrele, area: 18.09
Shape: Cube, Ares: $2.00
Shape: Cibe, Area: 52.00
Shape Square, Ares: 16.00
Shape: Circle, Area: 58.09
Shaper Cube, Area $2.00
Shape: Square, Area: 16.00
Shape: Cube, Area $2.00
Please select which option to run the program
1. Save the results into a file. The progree dll ask you to input a file name.
2. Print the results on screen.
3. Print the results inside out windows
Enter 3
file name.
Results
D
Shape Circle Area $8.09
Shape Square, Area 16.00
Shape Square, Area 16.00
Shaper Square, Ares 16.00
Shape Circle Area 38.09
Shape Circle, A 18.09
Shape Ciecle, Aves: 58.09
Shape Square, Ares 16.00
Shape Circle, Area 58.09
Shape Cercle Area 18.09

1 Answer

3 votes

Below is a program that creates the three classes – Cube (actually a Cuboid), Circle, and Square.

Python

class Shape:

def __init__(self, name):

self.name = name

def findArea(self):

raise NotImplementedError("Subclass must implement the findArea method")

def display(self):

print("Shape:", self.name)

class Cube(Shape):

def __init__(self, name, length, width, height):

super().__init__(name)

self.length = length

self.width = width

self.height = height

def findArea(self):

surfaceArea = 2 * ((self.length * self.width) + (self.length * self.height) + (self.width * self.height))

return surfaceArea

def display(self):

super().display()

print("Area:", self.findArea())

class Circle(Shape):

def __init__(self, name, radius):

super().__init__(name)

self.radius = radius

def findArea(self):

area = 3.14 * self.radius * self.radius

return area

def display(self):

super().display()

print("Area:", self.findArea())

class Square(Shape):

def __init__(self, name, side):

super().__init__(name)

self.side = side

def findArea(self):

area = self.side * self.side

return area

def display(self):

super().display()

print("Area:", self.findArea())

def generateShapes():

shapes = []

for i in range(10):

randomNumber = random.randint(0, 2)

if randomNumber == 0:

newShape = Circle("Circle", 4.3)

elif randomNumber == 1:

newShape = Square("Square", 4.0)

else:

newShape = Cube("Cube", 4.0, 4.0, 4.0)

shapes.append(newShape)

return shapes

def printResults(option):

if option == 1:

fileName = input("Enter name of file to save your result: ")

with open(fileName, 'w') as f:

for shape in shapes:

shape.display()

f.write(shape.name + " " + str(shape.findArea()) + "\\")

elif option == 2:

for shape in shapes:

shape.display()

elif option == 3:

print("Shape\tArea\\")

for shape in shapes:

shape.display()

if __name__ == "__main__":

while True:

print("welcome to my shape generator!")

print("Please select which option to run the program:")

print("1. Save the results into a file. The program will ask you to input a file name.")

print("2. Print the results on screen.")

print("3. Print the results inside GUI Windows")

print("4. Exit")

choice = int(input("Enter: "))

if choice == 1:

printResults(1)

elif choice == 2:

printResults(2)

elif choice == 3:

printResults(3)

else:

break

So, the above program prompts the user to select one of four options:

  • Save the results into a file. The program will ask the user to input a filename.
  • Print the results on screen.
  • Print the results inside GUI Windows
  • Quit the program

So, Based on the user's choice, the program creates a list of ten objects of the given shapes (Circle, Square, and Cube) using a random number generator, and then prints the results to the file, screen, or GUI window, depending on the user's choice.

User Victor Nazarov
by
8.4k points