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

EBK JAVA PROGRAMMING
8th Edition
ISBN:9781305480537
Author:FARRELL
Publisher:FARRELL
Chapter2: Using Data
Section: Chapter Questions
Problem 9PE
icon
Related questions
Question

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

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT