Assignment #12
Instructions
-
Write a Java application that does the following:
-
Create two files for your program: LastNameFirstName12.java, which has the main() method definition,
and a second file, GroceryItem.java, which 3 data fields and 2 method definitions.
-
The class GroceryItem models grocery items that you would buy at the grocery store.
-
The GroceryItem class definition should contain the following 3 data fields (attributes, instance variables): name of the item, price of the item, and count of the item.
-
Use the appropriate Class to store your data.
Data field name stores the name of the item, so it should be class String.
Data field price stores money, which is a decimal number, so it should be class Double.
Data field count is a whole number, so it should be class Integer.
-
The GroceryItem class definition should contain the following 2 instance methods (behaviors): constructor and toString().
-
The constructor should initialize (assign a beginning value) to all 3 data fields.
-
The toString() method should return a string of the 3 data fields, so that they can be appropriately displayed.
-
The LastNameFirstName12 class in the file LastNameFirstName12.java is the client.
-
In the main() method in the file LastNameFirstName12.java, instantiate (create) 5 GroceryItem objects. Use the constructor to do this.
-
For example, this code instantiates one GroceryItem object:
GroceryItem item1 = new GroceryItem("bananas", 0.99, 22);
-
In the main() method in the file LastNameFirstName12.java, use the toString() method to display the 5 Grocery Item objects.
Use either System.out.println() or JOptionPane.showMessageDialog().
-
For this assignment, since there is no input (only output), you do not need to use try-catch blocks.
-
Don't forget to include Java documentation (javadoc) comments in your program.
See the Java Coding Standard for details.
-
See the syllabus for the Assignment Grading Guidelines for your program assignments.
Example Output
name price count
bananas $0.99 20
oranges $2.99 5
garlic $3.99 10
grapes $9.99 2
papayas $4.99 6
Click to validate the HTML code
Click to validate the CSS code