Assignment #14
Instructions
-
Write a Java application as described in assignment #12.
-
For assignment #14, add the following code to assignment #12:
-
In the GroceryItem.java file, write an
amount()
method definition, which multiplies the price by the count, and returns the result.
-
You do NOT need to write an
amount
data field (instance variable).
The amount() method call will call the amount() method definition, which will multiply price by count (price * count), and return the result.
-
In the toString() method definition, call the
amount()
method, so that it displays the amount for each item.
-
For example:
display = ... + "\t\t" + count + "\t\t$" + this.amount() + ...
-
"this" is a variable that stores a reference to the object.
In other words, a variable that you can use to call the methods of the class, in the class definition.
-
Change the name of the client program from LastNameFirstName12.java to LastNameFirstName14.java.
-
In the LastNameFirstName14.java file, display the five items, including the amount.
-
Add up and display the total, using the five grocery item variables and the amount() method to do this.
-
For example:
total = item1.amount() + item2.amount() + ...
-
Where "item1", "item2", etc. are GroceryItem variables.
Example Output
item price count amount
bananas $0.99 22 $21.78
oranges $2.99 4 $11.96
garlic $3.99 8 $31.92
grapes $9.99 2 $19.98
papayas $4.99 6 $29.94
TOTAL: $115.58
Click to validate the HTML code
Click to validate the CSS code