Unformatted text preview:

Compsci 6 Test 1 Spring 2007PROBLEM 1 : (Mystery Repeat Repeat Repeat: (24 pts))PART A (10 pts):Consider the following Mystery method. Note the precondition that there must be at leasttwo blanks in phrase.// precondition: assume phrase has at least two blanks.public String Mystery (String phrase){int pos = phrase.indexOf(" ");int pos2 = phrase.indexOf(" ",pos+1);return phrase.substring(pos2+1) + ", " + phrase.substring(0,pos);}A. What type is the return value for the method Mystery?B. List all the local variables and their types in Mystery.C. What is the return value for the call Mystery(“Richard H. Brodhead”)?D. What is the return value for the call Mystery(“A B C D E”)?E. Describe in words what the method Mystery does.PART B (14 pts): Consider the following Mystery2 metho d.public int Mystery2(ArrayList<Integer> numbers){int count = 0;for (Integer num: numbers){if (num % 2 == 0){count += num;}}return count;}A. How many parameters does Mystery2 have? List their names.B. What type of values are stored in the ArrayList?C. Suppose values is an ArrayList<Integer> and has the values 5, 8, 10, 17, and 3 stored inthis order from position 0 to position 4. What is the return value of the call Mystery2(values)?D. Describe in words what the method Mystery2 does.1E. Rewrite the complete body of Mystery2 using a for loop that is not a collections for loop.F. Rewrite the complete body of Mystery2 using a while loop instead of a for loop.PROBLEM 2 : (Read a good book lately?: (12 pts))Consider the following two classes.public class Book {private String myTitle;private String myAuthor;private int myPages;public Book(String Title, String author, int pages){ myTitle = Title;myAuthor = author;myPages = pages; }public String getTitle(){ return myTitle; }public String getAuthor(){ return myAuthor; }public int getPages(){ return myPages; }}public class PictureBook extends Book {private String myIllustrator;public PictureBook(String title, String author,String illustrator, int pages){super(title, author, pages);myIllustrator = illustrator;}public String getAuthor(){ return super.getAuthor() + " " + myIllustrator;}public void setAuthor(String name){ myAuthor = name;}2}A : Which of the two classes is the superclass?B : Eclipse would flag one error in the PictureBook class. Explain which line causes theerror and how to fix it.C : Assume the error in the PictureBook class from part B is fixed. Consider the following4 sections of code. State if the two lines of code are valid or contain an error. If theycontain an error, then state what the error is. If they do not contain an error, then list thecorresponding output.// 1.Book book1 = new Book(‘‘Cross’’, ‘‘Patterson’’, 400);System.out.println(book1.getAuthor());// 2.Book book2 = new Book(‘‘Eragon’’, ‘‘Paolini’’);System.out.println(book2.getAuthor());// 3.Book book3 = new PictureBook(‘‘Goodnight Moon’’, ‘‘Brown’’, ‘‘Hurd’’, 32);System.out.println(book3.getAuthor());// 4.PictureBook book4 = new Book(‘‘Polar Express’’, ‘‘Van Allsburg’’,32);System.out.println(book4.getAuthor());PROBLEM 3 : (Who is hungry? (52 pts))Consider the class GroceryStore shown below. It stores information about all the items ithas including the name, price and the quantity of the item it has in s tock. This informationis stored in ArrayLists in which the kth item in each ArrayList corresponds.public class GroceryStore {private ArrayList<String> myItems; // name of itemsprivate ArrayList<Double> myPrices; // price of itemsprivate ArrayList<Integer> myQuantity; // quantity of items in stock// Constructor, assume no duplicate items in the file3public GroceryStore (Scanner input){ // code not shown }// returns number of different types of itemspublic int getNumberOfItems(){ // code not shown }// returns total number of items in storepublic int getTotalNumberOfItems(){ // code not shown }// returns price of specific item, returns -1 if item not in storepublic double getPriceOfitem(String item){ // code not shown }// returns number of unique items with price between minPrice and maxPrice, inclusivepublic int getNumberOfItemsWithPrice(double minPrice, double maxPrice){ // code not shown }// add an item to the grocery store if it is not there. If it is// already there, update the quantity and use this price as the new price.public void addItem(String item, double price, int quantity){ // code not shown }// Given a string of items, returns an ArrayList of the itemspublic ArrayList<String> ItemsInCart (String cart){ // code not shown }//returns the cost of all the items in the basketpublic double RingUpBasket(String cart){ // code not shown }}PART A (40 pts):PART A1 (6 pts): Consider the constructor for the class GroceryStore. It should read inthe stores’ inventory from a file and store it in the three ArrayLists myItems, myPrices andmyQuantity, such that the kth item in each ArrayList corresponds to the kth item in theother ArrayLists.Note that the Scanner input is already bound to a file. The file is in the following format.Each line in the file has the name of an item (containing no blanks), the price of the itemand the quantity of the item in stock.Consider the input file shown on the left and the corresponding ArrayLists shown on theright.Complete the constructor for the GroceryStore class below.// assume no duplicate items in the file4public GroceryStore (Scanner input){}PART A2 (3 pts) :Complete the method getNumberOfItems that returns the number of unique items in thegrocery store. Assume that the entries in the ArrayList myItems are all unique.For the example in PART A, getNumberOfItems() would return 6 as there are six items inthe ArrayLists.// returns number of different types of itemspublic int getNumberOfItems(){}PART A3 (4 pts) :Complete the method getTotalNumberOfItems() that returns the total number of items inthe store.For the example in PART A, getTotalNumberOfItems() would return 697, which is the sumof all the quantities of the items in stock.// returns total number of items in storepublic int getTotalNumberOfItems(){}PART A4 (5 pts) :Complete the method getPriceOfItem(String item) that returns the price of item.For the example in PART A, getPriceOfItem(mug) would return 3.50. The call getPriceOf-Item(rootbeer) would return -1 as rootbeer is not an item at the s tore.5// returns price of specific item, returns -1 if item not in storepublic double getPriceOfitem(String item){}PART A5 (5 pts)


View Full Document

Duke CPS 006 - Test 1

Download Test 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Test 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Test 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?