Question

In: Computer Science

Create a class called Cuboid in a file called cuboid.py. The constructor should take parameters that...

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

Solutions

Expert Solution

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()

====================================================================


Related Solutions

Class as a ChapteredVideo. The constructor should take the following parameters: ID, length, num_chapters, chap_lengths, location,...
Class as a ChapteredVideo. The constructor should take the following parameters: ID, length, num_chapters, chap_lengths, location, [extension] The new parameter should be stored as their own instance variables. The ChapteredVideo object should behave as the Video. However, the method get_ID should now return a list which contains whatever the Video previously would return as its ID as the first element, followed by a list of chapter names. Each chapter name will be formed by the ID of the main video...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
Create a file called grocery.ts. It should have a definition of a class with the obvious...
Create a file called grocery.ts. It should have a definition of a class with the obvious name Grocery. The class should have some basic attributes such as name, quantity, etc. Feel free to add any other attributes you think will be necessary. Add few grocery items to an array of groceries, such as milk, bread, and eggs, along with some quantities (i.e. 3, 6, 11). Display these grocery items as HTML output. The output of this assignment will be grocery.ts...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
you will find a class called Employee. This class should include a constructor that sets name...
you will find a class called Employee. This class should include a constructor that sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will create a...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT