In: Computer Science
Create a class called Cuboid in a file called
cuboid.py. The constructor should take parameters that sets a
private variable for each side of the cuboid. Overload the
following operators: +, -, <, >, ==, len(), and str(). Return
a cuboid with the appropriate volume for the arithmetic operators.
Use the volume of the cuboid to determine the output of the
overloaded comparison operators. Use the surface area of the cuboid
to calculate the result of len(). For str() return a string that
displays the side lengths, volume, and surface area. Let the user
enter values used to create two Cuboid objects. Then print out all
results of the overloaded operators (using the operator, not
calling the dunder method). Create a file called assn14-task1.py
that contains a main() function to run your program. It is fine for
the program to only run once then end. You DO NOT need to create
loop asking use if they want to "Play Again".
Note: The most complicated part of this is the + and
-. Remember that arithmetic operators should return the same type
as the operands, so a cuboid should be returned. The returned
cuboid is based on the volume, which means you'll need to figure
out what the side lengths should be. They can be anything
valid.
Rubric
5 pts: All operators overloaded
properly
5 pts: Print results using the overloaded
operators
5 pts: Proper output
Note: No software dev plan or UML
required
Thanks for the question.
Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.
Thank You !
===========================================================================
class Cuboid():
def __init__(self, length, width, height):
self.__length = length
self.__width = width
self.__height = height
def getLength(self): return self.__length
def getWidth(self): return self.__width
def getHeight(self): return self.__height
def __add__(self, other):
length = self.__length + other.getLength()
width = self.__width + other.getWidth()
height = self.__height + other.getHeight()
return Cuboid(length, width, height)
def __sub__(self, other):
length = self.__length - other.getLength()
width = self.__width - other.getWidth()
height = self.__height - other.getHeight()
return Cuboid(abs(length), abs(width), abs(height))
def __lt__(self, other):
v1 = self.__length * self.__width * self.__height
v2 = other.getLength() * other.getWidth() * other.getHeight()
return v1 < v2
def __gt__(self, other):
v1 = self.__length * self.__width * self.__height
v2 = other.getLength() * other.getWidth() * other.getHeight()
return v1 > v2
def __eq__(self, other):
v1 = self.__length * self.__width * self.__height
v2 = other.getLength() * other.getWidth() * other.getHeight()
return v1 == v2
def __len__(self):
surface_area = 2 * (self.__length * self.__width) + \
2 * (self.__length * self.__height) + \
2 * (self.__width * self.__height)
return int(surface_area)
def __str__(self):
v1 = self.__length * self.__width * self.__height
return 'Sides:[{},{},{}], Volume:{:.2f}, Surface Area:{:.2f}'.format(
self.__length, self.__width, self.__height, v1, self.__len__()
)
======================================================================
from cuboid import Cuboid
def main():
length = float(input('Enter length for cuboid 1: '))
width = float(input('Enter length for width 1: '))
height = float(input('Enter length for height 1: '))
cuboid_one = Cuboid(length, width, height)
length = float(input('Enter length for cuboid 2: '))
width = float(input('Enter length for width 2: '))
height = float(input('Enter length for height 2: '))
cuboid_two = Cuboid(length, width, height)
print('First Cuboid: ', cuboid_one)
print('Second Cuboid: ', cuboid_two)
print('First Cuboid + Second Cuboid:', cuboid_two + cuboid_one)
print('First Cuboid - Second Cuboid:', cuboid_one - cuboid_two)
print('First Cuboid < Second Cuboid:', cuboid_one < cuboid_two)
print('First Cuboid == Second Cuboid:', cuboid_one == cuboid_two)
print('Cuboid One Length: ', len(cuboid_one))
print('Cuboid Two Length: ', len(cuboid_two))
main()
====================================================================



