Making a Surface Area and Volume calculator

python

#1

So in my class I need to create a Surface Area and Volume calculator here are the instructions:
Write a Python program with a class for rectangular prisms and a subclass for cubes that will calculate volume and surface area of these geometric solids. Use the specs below to guide you through the process. When complete, zip the python file and upload the zipped file to the assignment dropbox.

Create a RectangularPrism class that has three instance variables for the three dimensions of a rectangular prism (length, width, and height).
Create a class method named volume that will return the volume of a rectangular prism. The formula for calculating the volume is to multiply the three dimensions.
Create another class method named surfaceArea that will return the surface area of a rectangular prism. The formula for calculating the surface area is to sum the areas of all six faces.
Create a Cube class that is a subclass of the RectangularPrism class. The Cube class only needs one argument since all three dimensions are the same length. Use the argument for each of the three dimensions.
Do not create new class methods for volume and surfaceArea in the Cube class because they are inherited from the RectangularPrism class.
Download and unzip the attached file. Either copy your code with the two classes to the top of this file or copy the code from this file to the bottom of your code. Add repr() methods to your classes so that when the program is run the output will look as shown below.

I tried my best following the instructions so here is my code:

class RectangularPrism(length, width, height):
indent preformatted text by 4 spaces Formula1 = length * width * height
indent preformatted text by 4 spaces Formula2 = length * length + width * width + height
indent preformatted text by 4 spaces def volume():
indent preformatted text by 4 spacesindent preformatted text by 4 spacesreturn Formula1
indent preformatted text by 4 spaces def surfaceArea():
indent preformatted text by 4 spacesindent preformatted text by 4 spacesreturn Formula2

class Cube(RectangularPrism):
indent preformatted text by 4 spacesdef init(self):
indent preformatted text by 4 spacesindent preformatted text by 4 spacesself.length = length
indent preformatted text by 4 spacesindent preformatted text by 4 spacesself.width = width
indent preformatted text by 4 spacesindent preformatted text by 4 spacesself.height = height

box1 = RectangularPrism(2, 3, 4)
print box1
print "Volume = " + str(box1.volume())
print "Suface Area = " + str(box1.surfaceArea())

print

box2 = Cube(2)
print box2
print "Volume = " + str(box2.volume())
print "Suface Area = " + str(box2.surfaceArea())

I am getting an error and I don't know what I did wrong. What should I write to make this work?


#2

Sorry my code is super confusing so here is the code again:
class RectangularPrism(length, width, height):
Formula1 = length * width * height
Formula2 = length * length + width * width + height
def volume():
return Formula1
def surfaceArea():
return Formula2

class Cube(RectangularPrism):
def init(self):
self.length = length
self.width = width
self.height = height

box1 = RectangularPrism(2, 3, 4)
print box1
print "Volume = " + str(box1.volume())
print "Suface Area = " + str(box1.surfaceArea())

print

box2 = Cube(2)
print box2
print "Volume = " + str(box2.volume())
print "Suface Area = " + str(box2.surfaceArea())


#3

I have indented the correct spots


#4

Please format your code, as it is easier to read.

I would make the Rectangular Prism class like this:

class rectangular_prism:
    def __init__(self, length, width, height):
        self.length = length
        self.width = width
        self.height = height
    def volume(self):
        return self.length * self.width * self.height
    def surface_area(self):
        return self.length * self.width * 2 + self.length * self.height * 2 + self.width * self.height * 2

#5

Now I am stuck with the Cube part. Here is my code:


      class Cube(RectangularPrism):
         def __repr__(self):
              return length
              return width
              return height

#6

Hi, @caelam ,

The code that @cadecodes posted should get you going in the right direction. But use the names for classes and methods that are specified in the instructions, such as RectangularPrism and surfaceArea.

You will need an __init__ method for the Cube class. It should take self and another variable, such as edge, as parameters, and assign that value of edge to all three instance variables. Here's an example ...

        self.length = edge

For this problem, the __repr__ method for each class should have only one return statement. But it appears that you have not posted the specifications for the __repr__ method. The aim is for that method to return a string representation of an instance of its class that displays the attributes of the instance in a nice format. Follow whatever instructions for the format that you have been given.

Following is an example of a __repr__ method that might work for the RectangularPrism class, but you will need to adapt it for the specifications that you were given ...

    def __repr__(self):
        return "RectangularPrism:({}, {}, {})".format(self.length, self.width, self.height)

Make sure you format your code when you post it.