Question: Java Item class Add private data fields (instance variables) for the items name, price, and quantity. public Item() sets price and quantity to 0 and
Java
Item class
Add private data fields (instance variables) for the items name, price, and quantity.
public Item()
sets price and quantity to 0 and name to the empty string.
public Item(String name, double price, int quantity)
Use the setters (mutators) to set the instance variables to the parameter data.
Add public accessors and mutators for name, price, and quantity. Guard against a negative price or quantity by setting the data member to 0.
public void reduce(int amount)
decrements the quantity field by amount if amount > 0 and amount <= quantity.
public void display()
Displays the candy name, price (with $, at least one digit before decimal pt and 2 after), and quantity (followed by the word remaining ) on the same line with comma and space separators
Sample: Pepsi, $1.25, 7 remaining
public String toString()
(Overrides the object class toString method)
Displays the candy name, price, and quantity with comma separators. There is no additional spacing or price formatting
Sample: Pepsi,1.5,7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
