Question: Python: (No handwritten answers please, thank you) Codes in text: class Fruit: def __init__(self, oz=1, unit_price = 1, color=None): self.__oz = oz self.__unit_price = unit_price

Python: (No handwritten answers please, thank you)

Python: (No handwritten answers please, thank you) Codes in text: class Fruit:Codes in text:

class Fruit: def __init__(self, oz=1, unit_price = 1, color=None): self.__oz = oz self.__unit_price = unit_price self.__color = color

# xxx: implement following def __add__(self,k): """overload operator + when self is the 1st operand if k is just a number, return an object x with x.__oz = self.__oz + k if k and self have the same type, return an object x with x.__oz = self.__oz + k.__oz otherwise, return None """ x = None if self.__class__ == k.__class__: # xxx fill in the missing codes pass elif k.__class__ == int: # xxx fill in the missing codes pass return x # xxx: implement following def __radd__(self,k): """overload + when self is the 2nd operand """ pass # xxx: implement following def __iadd__(self,k): """overload += if k is just a number, return object self with self.__oz += k if k and self are the same type, return object self with self.__oz +=k.__oz otherwise, return None """ pass # xxx: implement following def __imul__(self,k): """overload *= if k is just a number, return object self with self.__oz *= k if k and self are the same type, return object self with self.__oz *=k.__oz otherwise, return None """ pass # xxx: implement following def __mul__(self,k): pass

def __rmul__(self,k): return self.__mul__(k) @property def oz (self): return self.__oz def price(self): return self.oz* self.__unit_price # xxx: implement following def __str__(self): pass

@property def id(self): return 0 # xxx: implement the getter method for self.__oz pass

# xxx: implement following def __repr__(self): pass class Apple (Fruit): __count = 0 # xxx: implement following def __init__(self, oz=1, unit_price = 10, color=None): ### xxx: fill in the missing code here self.__id = self.__class__.__count self.__class__.__count += 1

# xxx: implement the getter method for id # xxx: class Fuji (Apple): __count = 0 # xxx: implement following def __init__(self, oz=1, unit_price = 20, color="RED"): ### xxx: fill in the missing code here self.__id = self.__class__.__count self.__class__.__count += 1

# xxx: implement the getter method for id # xxx: class Orange (Fruit): __count = 0 # class method # xxx: implement following def __init__(self, oz=1, unit_price = 3, color=None): ### xxx: fill in the missing code here self.__id = self.__class__.__count self.__class__.__count += 1 # xxx: implement the getter method for id # xxx:

. . . Read instructions marked XXX Fill in the missing codes indicated by XXX You could add helper funcitons, but never delete any portion of this documents. Restart Kernel & Run All before your submission to make sure your program does run throught without crash Grading: full credit if the program output is exactly as expected O credit if the program output is obviously incorrect partial credit may be given at will if the program output is close to the expected. Note that you will get 0 credits for this test if your program does not compile Your program crashes at any point . you delete any texts or codes A: Fruit Arithmetics (Total 30pt) Topics: . Object-oriented programming Magic (dunder) methods (double underscored methods) str vs repr str is a convenient string representation repr is a detailed string representation that are used in containers like list overloaded operators with the dunder's use lambda funcitons as the keys to sort, min, and max getter and setter methods format method list comprehensions . . . . Read instructions marked XXX Fill in the missing codes indicated by XXX You could add helper funcitons, but never delete any portion of this documents. Restart Kernel & Run All before your submission to make sure your program does run throught without crash Grading: full credit if the program output is exactly as expected O credit if the program output is obviously incorrect partial credit may be given at will if the program output is close to the expected. Note that you will get 0 credits for this test if your program does not compile Your program crashes at any point . you delete any texts or codes A: Fruit Arithmetics (Total 30pt) Topics: . Object-oriented programming Magic (dunder) methods (double underscored methods) str vs repr str is a convenient string representation repr is a detailed string representation that are used in containers like list overloaded operators with the dunder's use lambda funcitons as the keys to sort, min, and max getter and setter methods format method list comprehensions

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!