Solutions to CompSci 6 Test 1 Spring 2006 1. A. Not Equivalent. If value=8 and other=3 then A prints ok twice, the first code segment prints it once. B. Not Equivalent. If value=8 and other=5, then B prints ok, the first code segment doesn't print anything. C. Equivalent D. Equivalent 2. A. int B. two, count and dig C. 3 D. 0 E. Returns the number of times "value" appears in "number", where value is a digit from 1-9. 3. A. House B. getAddress() C. myNumberOfBedrooms is private, the BeachHouse class cannot access it. D. Make myNumberOfBedrooms protected, then the subclass BeachHouse can access this state. 4. public ArrayList equalize(ArrayList schools) { int dukecount = 0; int unccount = 0; for (String current: schools) { if (current.equals("duke")) dukecount++; if (current.equals("unc")) unccount++; } while (dukecount < unccount) { schools.add("duke"); dukecount++; } while (unccount < dukecount) { schools.add("unc"); unccount++; } return schools; } 5. A. // returns a String equivalent to phrase with all occurrences of // "@" with " AT " public String replaceAt(String phrase) { String result = ""; int pos = 0; int atpos = phrase.indexOf("@"); while (atpos >= 0) { result = result + phrase.substring(pos, atpos) + " AT "; pos = atpos + 1; atpos = phrase.indexOf("@", pos); } result += phrase.substring(pos); return result; } public String replaceAt2(String phrase) { ArrayList parts = new ArrayList(); for (int j=0; j= 0) { if (pos == 0) // first character { phrase = " AT " + phrase.substring(pos+1); } else if (pos == phrase.length()-1) // last character { phrase = phrase.substring(0,pos) + " AT "; } else { phrase = phrase.substring(0, pos) + " AT " + phrase.substring(pos+1); } pos = phrase.indexOf("@"); } return phrase; } public String replaceAt4(String phrase) { Scanner fromString = new Scanner(phrase); fromString.useDelimiter("@"); String result = ""; if (fromString.hasNext()) // first piece result = fromString.next(); while (fromString.hasNext()) // get the rest { result += " AT " + fromString.next(); } return result; } public String replaceAt5(String phrase) { return phrase.replace("@", " AT "); } B. public ArrayList replaceAllAts(Scanner input) { ArrayList result = new ArrayList(); while (input.hasNext()) { result.add(replaceAt(input.nextLine())); } return result; } public ArrayList replaceAllAts2(Scanner input) { ArrayList result = new ArrayList(); while (input.hasNext()) { result.add(input.nextLine()); } for (String current : result) { current = replaceAt(current); } return result; } public ArrayList replaceAllAts3(Scanner input) { ArrayList result = new ArrayList(); input.useDelimiter("\n"); while (input.hasNext()) { result.add(replaceAt3(input.next())); // gets one line } return result; } 6. Part A A1. // Constructor public Skier(String name, int number) { myName = name; myNumber = number; myTime = -1; } A2. // returns name of skier public String getName() { return myName; } // returns race number of skier public int getNumber() { return myNumber; } // returns best race time of skier so far, or a negative number if the // skier has not raced yet. public int getTime() { return myTime; } A3. // sets skiers best time to raceTime if this is the first run // or if raceTime is faster than the skiers best time previously public void setTime(int raceTime) { if ((myTime < 0)|| (raceTime < myTime)) { myTime = raceTime; } } Part B B1. // constructor - reads in skiers names and race numbers public SkiRace(Scanner input) { mySkiers = new ArrayList(); while (input.hasNext()) { mySkiers.add(new Skier(input.next(), input.nextInt())); } } B2. // Print the name of the skier with the fastest time. If there // is a tie, then print all such names with that time. // Assume all times are between 0 and 1000 public void PrintNamesWithBestSkiTimes() { // compute min time > 0 int minRaceTime = 1001; for (Skier current : mySkiers) { int currentTime = current.getTime(); if (currentTime < minRaceTime) { minRaceTime = currentTime; } } // Print names of those with minimum race time for (Skier current: mySkiers) { if (current.getTime() == minRaceTime) System.out.println(current.getName()); } } public void PrintNamesWithBestSkiTimes2() { // compute min time > 0 int minRaceTime = mySkiers.get(0).getTime(); String result = ""; for (Skier current : mySkiers) { int currentTime = current.getTime(); if (currentTime < minRaceTime) { minRaceTime = currentTime; // new fast time result = current.getName(); // get first name } else if (currentTime == minRaceTime) { result += " " + current.getName(); // another with same time } } System.out.println(result); }