Complete the following class definition, Box, for representing cardboard boxes w
ID: 3692368 • Letter: C
Question
Complete the following class definition, Box, for representing cardboard boxes with given dimensions, width-by-height-by-depth, that can hold a number of objects weighing, in total, up to the specified max Weight. Write in the code for the two incomplete methods: addObject and isLargerThan class Box: def --_init_-- (self, width, height, depth, maxWeight): self.width = width self.height = height self.depth = depth self.maxWeight = maxWeight self.objectsInside = [] self.currentWeightInside = 0 self.currentNumberOfObjectsInside = 0 def volume(self): return self.width * self.height * self.depth def numberofObjectsInside(self): return self.currentNumberOfObjectsInside def reinainingWeightCapacity(self): return self.maxWeight - self.currentWeightInside # If the box's weight capacity will not be exceeded, # add given object to the box, updating objectsInside, # currentWeightInside and currentNumberOfObjectsInside # Otherwise, print an appropriate message. # def addObject(self, object, objectWeight): ??? # Return False if the size (by volume) of self is larger than that of the other box # Return False otherwise. # def isLargerThan(self, otherBox): ???Explanation / Answer
Here is the modified code for you:
#!/usr/bin/python
class Box:
def __init__(self, width, height, depth, maxWeight):
self.width = width
self.height = height
self.depth = depth
self.maxWeight = maxWeight
self.objectsInside = []
self.currentWeightInside = 0
self.currentNumberOfObjectsInside = 0
def volume(self):
return self.width * self.height * self.depth
def numOfObjectsInside(self):
return self.currentNumberOfObjectsInside
def remainingWeightCapacity(self):
return self.maxWeight - self.currentWeightinside
def addObject(self, object, objectWeight):
if remainingWeightCapacity >= objectWeight:
self.objectsInside.append(object)
self.currentWeightInside = self.currentWeightinside + objectWeight
self.currentNumberOfObjectsInside = self.currentNumberOfObjectsInside + 1
else:
print 'Adding this object will exceed box capacity.'
def isLargerThan(self, otherBox):
if self.volume() > otherBox.volume():
return True
return False
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.