Lab 12

The following exercises are to be completed during lab class.  If you do not have time to finish during lab, they must be completed before the beginning of the following lab session.  You will turn in the lab gradesheet  at the beginning of the following lab.

Before beginning the exercises, copy this lab's folder from the T: drive to your H: drive.

  1. Write a program that reads up to 100 integers from a file named data.txt. Store the integers in an array.  The program will then ask the user for values, and will look up each value in the array and print out the index where the value can be found in the array.  If the value can't be found in the array, the program will print "Not found".  The program should continue to look up values until the user enters a sentinel value of -999.  Call your file FindValues.java. Include FindValues.java in your zip file for submission.
     
  2. In this exercise you will complete a class that implements a shopping cart as an array of items.  The file Item.java contains the definition of a class named Item that models an item one would purchase.  An item has a name, a price (the individual price for a quantity of 1), and a quantity (the quantity purchased).  The file ShoppingCart.java implements the shopping cart as an array of Item objects.  The Item.java file is complete and located in the lab 12 folder from the t-drive.  You will be completing the ShoppingCart class (started for you in the lab 12 folder) and writing a program that uses it.  Name that program Shopping.java.  Include the modified ShoppingCart.java and Shopping.java in your zip file for submission. 
    1. Complete the ShoppingCart class by doing the following:
      • Declare an instance variable cart to be an array of Items and instantiate cart in the constructor to be an array holding capacity Items.
      • Fill in the code for the increaseSize method.  Your code should be similar to the grow array part of Listing 10-11 of the text on page 558, but increase the size by 3 elements instead of doubling it.
      • Fill in the code for the addToCart method.  This method should add the item to the cart.
      • Fill in the code for the getTotalPrice method.  This method should calculate and return the total cost of all items in the cart (taking quantity into account). DO NOT add an instance variable to hold the total cost.
      • Fill in the missing code in the toString method.
    2. Write a program that simulates shopping.  The program should have a loop that continues as long as the user wants to shop (use the word "quit" entered as the item name as your sentinel value).  Each time through the loop, read in the name, price, and quantity of the item the user wants to add to the cart.  After adding an item to the cart, the cart contents should be printed.  After loop, print a "Please pay ..." message with the total price of the items in the cart (printed as currency). Do not worry about column spacing for this problem. Use tabs to separate data, but it will not always line up.
       
  3. Add a toString method to the BankAccount class.  The toString method should produce a String that (when printed) would look like this:

    Account holder: Mary Smith
    Account number: 1004098
    Balance: $100.48

    Also add a method to read data from a Scanner parameter object and a method to write data to a PrintWriter parameter object.  The header for your read method would be:

     public void read(Scanner input)

    Note that the Scanner and PrintWriter object are created and initialized outside your method and passed into the method.  The read method reads data for a single BankAccount object from the file using the Scanner and storing it into the object.   The write method writes data from a single BankAccount object into the PrintWriter.  Both methods are designed to work with files (so should be opposites - with the write method producing the file which could be read by the read method). 
    Create a main method in BankAccount.java that tests these three methods. Often a read or write method appears to work, until multiple records are processed so make sure to read multiple records and write all records to the new file.   Include the modified BankAccount.java file in your zip file.  A sample file containing BankAccount data in on the T drive in the lab 12 folder.  You can see the exact format for both the reading and the writing by looking at that file.
     

Use the windows zip utility to zip all required files into a single zip folder. Submit the single zip folder to your lab section under project #12 using Web Submit.

NOTE: Do NOT put your files into a folder to be zipped as this will make grading more complicated. Simply select the necessary .java files, right click, choose Send To = > Compressed (zipped) Folder.