In: Computer Science
-if user select 3Duser needs to select the shape type (Cube, Cylinder, or Pyramid) after selected shape the user needs to specify whether to find the surface-Area and the volume using user-defined-constructor or default-constructor, accordingly the needed constructor is used. Important Points: 1- all selections must be done within an infinite loop such that the user selects to exit when needed. 2- all entered values need to be validated, otherwise, repetition occurs (must be re-entered again). 3- Any selection must create an instance (object) of the corresponding class, the object must test the class functionality by setting its values and displaying the output. 4- Summary Print() method should display the information in a proper way,
o User Name: jone
o Selected category : 2D
o Shape Name: square
o Side Length: 5 m3
o Volume: 29
o Surface Area : 60 m2
Have created 3 classes of 3D shape like Cube, Cylinder, Pyramid. And has taken all the required parameters and after successful validation, print the report on console.
Please go through this code snippet.
import math
import sys
# define classes and constructor and methods
class Cube:
def __init__(self, side):
self.edge = side
def getSurface_Area(self):
return 6 * self.edge ** 2
def getVolume(self):
return self.edge ** 3
class Cylinder:
def __init__(self, radius, height):
self.radius = radius
self.height = height
def getSurface_Area(self):
return 2 * math.pi * self.radius * (self.radius + self.height)
def getVolume(self):
return math.pi * self.height * self.radius ** 2
class Pyramid:
def __init__(self, height, length, width):
self.height = height
self.length = length
self.width = width
def getSurface_Area(self):
return (self.length * self.width) + (
self.length * math.sqrt((self.width / 2) ** 2 + self.height ** 2)) + self.width * math.sqrt(
((self.length / 2) ** 2) + self.height ** 2)
def getVolume(self):
return self.height * self.width * self.length / 3
def getUserInput_Calculate(name, choice):
"""
This method will take user input based on the choice and provide the output details
:return:
"""
if choice == 0:
sys.exit(0)
elif choice == 1:
# Take the required user inputs and validates for Cube
while True:
try:
edge = float(input('Enter a value for edge: '))
break
except:
continue
cube = Cube(edge)
print('-' * 30)
print('User Name: ' + name)
print('Shape Name: Cube')
print('Volume : ' + str(cube.getVolume()))
print('Surface Area : ' + str(cube.getSurface_Area()))
print('-' * 30)
print('\n')
elif choice == 2:
# Take the required user inputs and validates for Cylinder
while True:
try:
radius = float(input('Enter radius : '))
height = float(input('Enter height : '))
break
except:
continue
cylinder = Cylinder(radius, height)
print('-' * 30)
print('User Name: ' + name)
print('Shape Name: Cylinder')
print('Volume : ' + str(cylinder.getVolume()))
print('Surface Area : ' + str(cylinder.getSurface_Area()))
print('-' * 30)
print('\n')
elif choice == 3:
# Take the required user inputs and validates for Pyramid
while True:
try:
height = float(input('Enter height : '))
length = float(input('Enter length : '))
width = float(input('Enter width : '))
break
except:
continue
pyramid = Pyramid(height, length, width)
print('-' * 30)
print('User Name: ' + name)
print('Shape Name: Cylinder')
print('Volume : ' + str(pyramid.getVolume()))
print('Surface Area : ' + str(pyramid.getSurface_Area()))
print('-' * 30)
print('\n')
else:
print('Please select a correct option...')
def main():
while True:
name = input('Enter User name : ')
choice = input(
'Press 1 to select Cube\nPress 2 to select Cylinder\nPress 3 to select Pyramid\nPress 0 to exit:')
# check if user given input is int or not. if not integer then repeat the same statement.
if choice.isdigit():
choice = int(choice)
print(choice)
else:
continue
getUserInput_Calculate(name, choice)
if __name__ == "__main__":
main()
Output:
Enter User name : John
Press 1 to select Cube
Press 2 to select Cylinder
Press 3 to select Pyramid
Press 0 to exit:7
7
Please select a correct option...
Enter User name : John
Press 1 to select Cube
Press 2 to select Cylinder
Press 3 to select Pyramid
Press 0 to exit:1
1
Enter a value for edge: u
Enter a value for edge: 7
------------------------------
User Name: John
Shape Name: Cube
Volume : 343.0
Surface Area : 294.0
------------------------------
Enter User name : John
Press 1 to select Cube
Press 2 to select Cylinder
Press 3 to select Pyramid
Press 0 to exit:2
2
Enter radius : t
Enter radius : 8
Enter height : y
Enter radius : 8
Enter height : 0
------------------------------
User Name: John
Shape Name: Cylinder
Volume : 0.0
Surface Area : 402.1238596594935
------------------------------
Enter User name : John
Press 1 to select Cube
Press 2 to select Cylinder
Press 3 to select Pyramid
Press 0 to exit:3
3
Enter height : 10
Enter length : 5
Enter width : 3
------------------------------
User Name: John
Shape Name: Cylinder
Volume : 50.0
Surface Area : 96.48266323252417
------------------------------
Enter User name : John
Press 1 to select Cube
Press 2 to select Cylinder
Press 3 to select Pyramid
Press 0 to exit:0
0
Process finished with exit code 0
Still if you have any doubt, please feel free to post in comment box. I would be glad to help you here.
If you like my explanation and answer, requesting you to give a thumbs up which really motivates us to provide a good quality answer.