6.12 Programming Activity 2: UsingforLoops
In this activity, you will write aforloop:
For this Programming Activity, you will again calculate the total cost of the items in a grocery cart. This time, however, you will write the program for the Express Lane. In this lane, the customer is allowed up to 10 items. The user will be asked for the number of items in the grocery cart. Your job is to write aforloop to calculate the total cost of the items in the cart.
Like Programming Activity 1, the framework will animate yourforloop, displaying the items in the cart moving down a conveyor belt toward a cashier station (a grocery bag). It will also display the unit price of the item, the correct subtotal, and your current subtotal. By comparing the correct subtotal to your subtotal, you will be able to check whether your code is calculating the correct value.
Figure 6.28demonstrates the animation. The cart contains five items. The third item, a carton of milk, is being scanned at a unit price of $2.00, bringing the correct subtotal for the cart to $9.00.
Instructions
Copy the files in this chapter’s Programming Activity 2 folder in the source code provided with this text to a folder on your computer. Searching for five stars (*****) in theCashierDrawing.javacode will show you where to add your code. You will add your code inside thecheckoutmethod of theCashierDrawingclass (the method header for thecheckoutmethod has already been coded for you).Example 6.19shows a fragment of theCashierDrawingclass, where you will add your code:
Figure 6.28
Sample Animation
EXAMPLE 6.19 ThecheckoutMethod in theCashierDrawingClass
public void checkout( int numberOfItems ) { /* ***** Student writes the body of this method ***** */ // // The parameter of this method, numberOfItems, // represents the number of items in the cart. The // user will be prompted for this number. // // Using a for loop, calculate the total price // of the groceries in the cart. // // The getNext method (in this CashierDrawing class) returns the next // item in the cart, which is an Item object (we do not // know which item will be returned; this is randomly generated). // getNext does not take any arguments. Its API is // Item getNext( ) // // As the last statement of the body of your for loop, // you should call the animate method. // The animate method takes one argument a double, // which is your current subtotal. // For example, if the name of your variable representing // the current subtotal is total, your call to the animate // method should be: // animate( total ); // // The getPrice method of the Item class // returns the price of the Item object as a double. // The getPrice method does not take any arguments. Its API is // double getPrice( ) // // End of student code // // }
To write the body of yourforloop, you can use the following methods:
You can access items in the cart using thegetNextmethod of theCashierDrawingclass, which has the following API:
Item getNext( )
ThegetNextmethod returns anItemobject, which represents anItemin the cart. As you can see, thegetNextmethod does not take any arguments.Since we call the methodgetNextfrom inside theCashierDrawingclass, we can simply call the method without an object reference. For example, a call togetNextcould look like the following:
Item newItem; newItem = getNext( );
After you get a newItem, you can “scan” the item to get its price by calling thegetPricemethod of theItemclass. ThegetPricemethod has this API:
double getPrice( )
Thus, you would get the next item, then get its price using code like the following:
Item newItem; double price; newItem = getNext( ); price = newItem.getPrice( );
When you have finished writing the code for thecheckoutmethod, compileCashierDrawing.javaand run the application from theCashierApplicationclass. When the application finishes executing, verify that your code is correct by:
checking that your subtotal matches the correct subtotal displayed
checking that you have processed all the items in the cart by verifying that the current item number matches the total number of items. For example, if the cart has five items, check that the message in the top right of the screen displays:Item # 5 of 5.
Troubleshooting
If your method implementation does not animate or animates incorrectly, check these items: