Solution to Test 1 CompSci 6 Spring 2007 Problem 1 - Part A A. String B. int pos, int pos2 C. Brodhead, Richard D. C D E, A E. It builds a new string that is everything after the second blank, followed by a comma, followed by the original first word. Problem 1 - Part B A. 1, numbers B. Integer C. 18 D. It is the sum of the even numbers in the array E. int count = 0; for (int k=0; k < numbers.size(); k++) { if (numbers.get(k) % 2 == 0) { count += numbers.get(k); } } return count; F. int count = 0; int k = 0; while (k < numbers.size()) { if (numbers.get(k) % 2 == 0) { count += numbers.get(k); } k++; } return count; Problem 2 A. Book B. myAuthor = name; myAuthor is private in the Book class and not accessible by the PictureBook class. To fix it, the Book class should have a method to change the name of the author that is public, and then you could call that method. C. 1. Correct, output is: Patterson 2. The constructor with two arguments is not defined. 3. Correct, output is: Brown Hurd 4. Cannot convert a Book to a PictureBook. Problem 3 Part A Part A1 public GroceryStore (Scanner input) { myItems = new ArrayList(); myPrices = new ArrayList(); myQuantity = new ArrayList(); while (input.hasNext()) { myItems.add(input.next()); myPrices.add(input.nextDouble()); myQuantity.add(input.nextInt()); } } Part A2 // returns number of different types of items public int getNumberOfItems() { return myItems.size(); } Part A3 // returns total number of items in store public int getTotalNumberOfItems() { int sum = 0; for (int k=0; k= minPrice && myPrices.get(k) <= maxPrice) { count += 1; } } return count; } Part A6 public void addItem(String item, double price, int quantity) { for (int k=0; k ItemsInCart (String cart) { ArrayList allitems = new ArrayList(); while (cart.length()> 0) { int pos = cart.indexOf(" "); if (pos == -1) // only 1 word left { allitems.add(cart); cart = ""; } else { allitems.add(cart.substring(0,pos)); cart = cart.substring(pos+1); } } return allitems; } Part A7 (alternate) public ArrayList ItemsInCart (String cart) { ArrayList allitems = new ArrayList(); Scanner in = new Scanner(cart) while (in.hasNext()) { allitems.add(in.next()); } return allitems; } Part A8 public double RingUpBasket(String cart) { ArrayList allitems = ItemsInCart(cart); double price = 0.0; double sum = 0.0; for (String item: allitems) { price = getPriceOfitem(item); System.out.println("item " + item + " price is " + price); if (price >0) { sum += price; } else { System.out.println("ERROR: item " + item + " not in db"); } } return sum; } Part B private ArrayList myItems; Part B1 public GroceryStore2 (Scanner input) { myItems = new ArrayList(); while (input.hasNext()) { myItems.add(new GroceryItem(input.next(), input.nextDouble(), input.nextInt())); } } Part B1 (alternate body of while loop - write it all out) while (input.hasNext()) { String item = input.next(); Double price = input.nextDouble(); int quantity = input.nextInt(); GroceryItem temp = new GroceryItem(item, price, quantity); myItems.add(temp); } Part B2 public double getPriceOfitem(String item) { for (GroceryItem stockItem: myItems) { if (stockItem.getItem().equals(item)) return stockItem.getPrice(); } return -1.0; }