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,...

1 answer below »

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:




  • Verify that you have correctly coded the header of yourforloop.




  • Verify that you have correctly coded the body of the loop.



Answered 1 days AfterApr 29, 2021

Answer To: 6.12 Programming Activity 2: UsingforLoops In this activity, you will write aforloop: For this...

Pulkit answered on May 01 2021
135 Votes
82493/cashierdrawing.java
82493/cashierdrawing.java
/* CashierDrawing Class
* Anderson, Franceschi
*/
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class CashierDrawing
{
 // ArrayList representing the various Items and subtotals
 // as they are used by th
e loop
 private ArrayList subTotals;
 SubTotal currentSubTotal;
 Item currentItem;
 // current index of subTotals
 private int index;
 private Cart cart;
 
 public CashierDrawing( )
 {
   subTotals = new ArrayList( );
   index = 0;
   cart = new Cart( );
 }
 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( )
   //

   double total = 0;
   for(int i=0;i   {
     Item item = this.getNext();
     total += item.getPrice();
     animate(total);
   }
   JOptionPane.showMessageDialog(null,"The total price of the item is: " + total, "Total Price", JOptionPane.INFORMATION_MESSAGE);
   //
   //  End of student code
   //
 }
  public Item getNext( )
  {
   // get next item
   // if the first item is the divider, that is ok - the cart is empty
   int number = ( (int) ( Math.random( ) * cart.getItemSize( ) ) );
   cart.setCurrentItem( number );
   // animate divider if necessary
   // if ( number == 3 )
      // animate( cart.getTotal( ) );

   Item item = ( cart.getItems( ) )[cart.getCurrentItem( )];
   // update subTotal
   currentItem = item;
   currentSubTotal = new SubTotal( item, 0 );
   return item;
  }
 
 public void animate( double total )
 {
   // update subTotals
   currentSubTotal.setSubTotal( total );
   // put it in ArrayList
   subTotals.add( currentSubTotal );
 }

  public void reset( )
  {
   index = 0;
  }

  public void incrementIndex( )
  {
   index++;
  }

  public int getIndex( )
  {
   return index;
  }

  public boolean done( )
  {
    return index >= subTotals.size( );
  } 

  public SubTotal getSubTotal( int index )
  {
    return subTotals.get( index );
  }

  public String toString( )
  {
    String result = "List: ";
    for( SubTotal current : subTotals )
    {
      result += current.toString( ) + " ";
    }
    return result;
  }

  public void clearSubTotals( )
  {
    subTotals.clear( );
  }

  public boolean isEmpty( )
  {
    return subTotals.size( ) == 0;
  }
}
82493/mini-6.docx
6.12 Programming Activity 2: Using for Loops
I’m working in Netbeans.
In this activity, you will write a for loop:
For this Programming Activity, you will again calculate the total cost of the items in a grocery cart. This time, however, you will...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here