Question: java Candy class Add a private field (instance variable) for nuts. public Candy() calls the no-arg constructor of the parent class and then sets nuts
java
Candy class
Add a private field (instance variable) for nuts.
public Candy()
calls the no-arg constructor of the parent class and then sets nuts to false. This means, it contains no nuts by default.
public Candy(String name, double price, int quantity, boolean nuts)
calls the parent constructor. Which parameters must be sent?
sets the nuts data field to the value of nuts.
Add a public accessor and mutator for nuts.
public void display()
(Overrides the parents display method)
Calls the display method of the parent
Displays either (nuts) or (no nuts) depending if the nuts instance variable is true or false.
public String toString()
(Overrides the object class toString method)
Creates and returns a string that is built from:
Candy, plus
the returned result of the parents toString() method plus
a comma and the value stored in nuts.
Sample return value: Candy,Snickers,0.85,10,true
public void store(PrintWriter outFile) throws IOException
outFile refers to an opened output file stream
There is only one line of code that will print the returned result from the toString() method to the output stream.
Soda class
Add a private field (instance variable) for caffeine.
public Soda()
calls the no-arg constructor of the parent class and then sets caffeine to false. This means, it contains no caffeine by default.
public Soda(String name, double price, int quantity, boolean caffeine)
calls the parent constructor. Which parameters must be sent?
sets the caffeine data field to the value of caffeine.
Add a public accessor and mutator for caffeine.
public void display()
(Overrides the parents display method)
Calls the display method of the parent.
Displays either (caffeine) or (no caffeine) depending if the caffeine instance variable is true or false.
public String toString()
(Overrides the object class toString method)
Creates and returns a string that is built from:
Soda, plus
The returned result of the parents toString() method plus
a comma and the value stored in caffeine.
Sample return value: Soda,Mtn Dew,0.9,8,true
public void store(PrintWriter outFile) throws IOException
outFile refers to an opened output file stream
There is only one line of code that will println out to outFile the returned result from the toString() method.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
