CompSci 6 Test 1 Spring 2010 1. A. Output x/z = 1 x/y = 2.5 multiply 15 6.0 urh now? 9 12 6 7 done B. 1. int 2. in and status 3. Scanner and String 4. 3 5. It reads data from a file in which each line has a name, an age, and a condition. It returns the number of people in the file that have a specified condition, where the condition is a parameter. C. Two rectangles are drawn. A red rectangle is drawn with its top left corner at (50,100), width 50 and height 100. A green rectangle is drawn with its top left corner at (100, 200), width 50 and height 100. 2. public double averageInRange(int [] numbers, int low, int high) { double average = 0; int count = 0; for (int value: numbers) { if (value >= low && value <= high) { count++; average += value; } } return average/count; } ALT SOLN: public double averageInRange(int [] numbers, int low, int high) { double average = 0; int count = 0; for (int k=0; k= low && numbers[k] <= high) { count++; average += numbers[k]; } } return average/count; } ALT 2 SOLN public double averageInRange(int [] numbers, int low, int high) { ArrayList elements = new ArrayList(); for (int value: numbers) { if (value >= low && value <= high) { elements.add(value); } } double average = 0; for (int value: elements) { average += value; } return average/element.size(); } 3. public ArrayList findNames(ArrayList birthdates, int year) { ArrayList answers = new ArrayList(); for(String str: birthdates) { int birthyear = Integer.parseInt(str.substring(str.indexOf(",")+2)); String name = str.substring(0,str.indexOf(":")); if (year == birthyear) { answers.add(name); } } return answers; } ALT SOLN: public ArrayList findNames(ArrayList birthdates, int year) { ArrayList answers = new ArrayList(); Syear = year + ""; for(String str: birthdates) { String birthyear = str.substring(str.indexOf(",")+2); String name = str.substring(0,str.indexOf(":")); if (Syear.equals(birthyear)) { answers.add(name); } } return answers; } ALT 2 SOLN public ArrayList findNames(ArrayList birthdates, int year) { ArrayList answers = new ArrayList(); for(int k=0; k< birthdates.size(); k++) { String str = birthdates.get(k); int birthyear = Integer.parseInt(str.substring(str.indexOf(",")+2)); String name = str.substring(0,str.indexOf(":")); if (year == birthyear) { answers.add(name); } } return answers; } ALT 3 SOLN (convert strings to scanners) public ArrayList findNames(ArrayList birthdates, int year) { ArrayList answers = new ArrayList(); for(String str: birthdates) { // convert string into a scanner Scanner input = new Scanner(str); String name = ""; while (input.hasNext() && name.length > 0 && name.charAt[name.length-1] != ":") { name += " " + input.next(); } if (name.charAT[name.length-1] == ":") { name = name.substring(0,name.length()-1); } // get the year input.next(); // ignore the month input.next(); // ignore the day, int birthyear = input.nextInt(); if (birthyear == year) { answers.add(name); } } return answers; } 4. PART A. public SkiJumper(String name, double jump) { myName = name; myCurrentJump = 0; myBestJump = jump; } public String getName() { return myName; } public double getCurrentJump() { return myCurrentJump; } public double getBestJump() { return myBestJump; } public void setCurrentJump(double jump) { myCurrentJump = jump; if (myCurrentJump > myBestJump) { myBestJump = myCurrentJump; } } PART B. public void competition(Scanner in) { // read in first two lines and create two SkiJumpers. SkiJumper skierOne = new SkiJumper(in.next() + " " + in.next(), in.nextDouble()); SkiJumper skierTwo = new SkiJumper(in.next() + " " + in.next(), in.nextDouble()); // process the rest of the file while(in.hasNext()) { String name = in.next() + " " + in.next(); if (name.equals(skierOne.getName())) { skierOne.setCurrentJump(in.nextDouble()); } else { skierTwo.setCurrentJump(in.nextDouble()); } } System.out.println(skierOne.getName() + "'s longest jump was " + skierOne.getBestJump()); System.out.println(skierTwo.getName() + "'s longest jump was " + skierTwo.getBestJump()); // print a message stating who jumped further if (skierOne.getBestJump() > skierTwo.getBestJump()) { System.out.println(skierOne.getName() + " jumped further"); } else if (skierTwo.getBestJump() > skierOne.getBestJump()) { System.out.println(skierTwo.getName() + " jumped further"); } else { System.out.println("They jumped the same distance"); } }