Assignment #12

Instructions

  1. Write a Java application that does the following:
    1. 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.
    2. The class GroceryItem models grocery items that you would buy at the grocery store.
    3. 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.
    4. 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.
    5. The GroceryItem class definition should contain the following 2 instance methods (behaviors): constructor and toString().
    6. The constructor should initialize (assign a beginning value) to all 3 data fields.
    7. The toString() method should return a string of the 3 data fields, so that they can be appropriately displayed.
    8. The LastNameFirstName12 class in the file LastNameFirstName12.java is the client.
    9. In the main() method in the file LastNameFirstName12.java, instantiate (create) 5 GroceryItem objects. Use the constructor to do this.
    10. For example, this code instantiates one GroceryItem object: GroceryItem item1 = new GroceryItem("bananas", 0.99, 22);
    11. 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().
    12. For this assignment, since there is no input (only output), you do not need to use try-catch blocks.
    13. Don't forget to include Java documentation (javadoc) comments in your program. See the Java Coding Standard for details.
    14. 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