Weekly Lab 2 – Arrays (part 2)

Maximum Points = 10

 The purpose of this lab is to review your study of computer programming and algorithms from CS 1 focusing on classes and ArrayList.

 

In this exercise you will implement a shopping cart using the ArrayList class. The file Item.java contains the definition of a class named Item that models an item one would purchase (attached). An item has a name, price, and quantity (the quantity purchased). The file Shop.java (attached) is an incomplete program that models shopping.

 

            0. Combine Item.java and Shop.java into a Project, compile and run.

1. Complete Shop.java as follows:

a. Declare and instantiate a variable cart to be an empty ArrayList.

b. Fill in the statements in the loop to add an item to the cart. Comments in the code indicate where these statements go.

c. Fill in the statements in the loop to print the cart contents (using the default toString in the ArrayList class). Comments in the code indicate where these statements go.

d. Compile your program and run it.

 

IF there is time, also do the following:

 

2. You should have observed two problems with using the default printing for the cart object: the output doesn't look very good and the total price of the goods in the cart is not computed or printed. Modify the program to correct these problems by replacing the print statement with a loop that does the following:

a. gets each item from the cart and prints the item

b. computes the total price of the items in the cart (you need to use the getPrice and getQuantity methods of the Item class). The total price should be printed after the loop.

3. Compile and run your program.

 

 

 (Due before 11:59 pm on Friday, January 21, 2011) Submit your .java files containing your program to the dropbox in WebCT.

 Grades are determined using the following scale:

Grading Rubric  (Word document)