Question

** Using Python 3 ** Write a Box class whose init method takes three parameters and...

** Using Python 3 **

Write a Box class whose init method takes three parameters and uses them to initialize the length, width and height of a Box. It should also have a method named volume that returns the volume of the Box. Write a function named box_sort (not part of the Box class) that uses insertion sort to sort a list of Boxes from greatest volume to least volume.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

class Box:

def __init__(self,l,w,h):

self.length = l

self.width = w

self.height = h

def getVolume(self):

return self.length * self.width * self.height

def box_sort(boxes):

#traverse through each box object

for i in range(1, len(boxes)):

box = boxes[i]

#move the boxes from 0 to i-1 index which have greater

#volume than the current box

j = i-1

while j >=0 and box.getVolume() > boxes[j].getVolume() :

boxes[j+1] = boxes[j]

j -= 1

boxes[j+1] = box

return boxes

def main():

#create boxes and sort them

box1 = Box(4,6,2)

box2 = Box(1,2,3)

box3 = Box(3,4,6)

boxes = box_sort([box1,box2,box3])

for i in range(len(boxes)):

print(boxes[i].getVolume())

main()

Add a comment
Know the answer?
Add Answer to:
** Using Python 3 ** Write a Box class whose init method takes three parameters and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT