remaining, an int that defaults to 4. (Assume that any argument given is non-negative, no data validation is required.) __repr__ returns a str version of the KitKat as formatted below, note that single quotes appear around the brand Example of ouput: >>> candy = KitKat() # using both defaults >>> candy
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
The constructor ( __init__ ) accepts two optional arguments
- flavor, a str that defaults to 'Milk Chocolate'
- number of fingers remaining, an int that defaults to 4. (Assume that any argument given is non-negative, no data validation is required.)
__repr__ returns a str version of the KitKat as formatted below, note that single quotes
appear around the brand
Example of ouput:
>>> candy = KitKat() # using both defaults
>>> candy
KitKat('Milk Chocolate', 4)
>>> candy = KitKat('Green Tea',3) # flavor and fingers both specified
>>> candy
KitKat('Green Tea', 3)
>>> candy = KitKat('Mint') # flavor specified, fingers default to 4
>>> candy
KitKat('Mint', 4)
The eat method allows one to eat one or more fingers/sections of a KitKat . Details:
- accepts one optional argument, the number of fingers to eat
defaults to 1 if no argument is supplied
otherwise, you may assume this is a positive integer - reduces the current number of fingers by the number eaten
Any attempt to eat more fingers than the KitKat currently has, will consume only those that are there. (i.e., fingers cannot go negative) - returns a list containing copies of the flavor, one per finger that was actually eaten.
Example of output:
>>> candy = KitKat('Green Tea',4)
>>> candy.eat() # defaults to 1
['Green Tea']
>>> candy
KitKat('Green Tea', 3)
>>> candy.eat(4) # try to eat 4, but only 3 there, so eat 3
['Green Tea', 'Green Tea', 'Green Tea']
>>> candy
KitKat('Green Tea', 0)
>>> candy.eat() # nothing left to eat
[]
>>> candy
KitKat('Green Tea', 0)
Two KitKat objects are compared by comparing the number of fingers available, for these comparisons the flavor is ignored. There are two comparisons supported:
== - this method must be named __eq__ , it accepts two KitKat objects and returns True if and only if they currently have the same number of fingers.
> - this method must be named __gt__ , it accepts two KitKat objects and returns True if the first KitKat has strictly more fingers than the second KitKat .
Hints/Notes:
- You may not have written __gt__ before. Structurally the code is identical to __eq__ , the only difference is that the Boolean expression inside the method is different, as described above.
>>> KitKat('Green Tea', 3) == KitKat('Mint', 3)
True
>>> KitKat('Green Tea', 3) == KitKat('Mint', 2)
False
>>> KitKat('Green Tea', 3) > KitKat('Mint', 3)
False
>>> KitKat('Green Tea', 3) > KitKat('Mint', 2)
True
>>> KitKat('Green Tea', 2) > KitKat('Mint', 4)
False
Step by step
Solved in 2 steps