Last active
May 2, 2016 04:30
-
-
Save gschaffner/7f3de32aa86d99645da36ff454cbfa76 to your computer and use it in GitHub Desktop.
APCS Written Test Practice Barron's 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1a | |
public String extractCity(String cityZip) { | |
return CityZip.substring(0, cityZip.indexOf(',')); | |
} | |
// 1b | |
public void printNames() { | |
System.out.println(lines.get(0)); | |
for(int i = 1; i < lines.size() - 1; i++) { | |
// I forgot the -1 in the header above | |
if(lines.get(i).equals("")) { | |
System.out.println(lines.get(i + 1); | |
} | |
} | |
} | |
// 1c | |
public String getAddress(String name) { | |
String toReturn = ""; | |
int nameIndex = 0; | |
for(nameIndex = 0; nameIndex < lines.size(); nameIndex++) { | |
if(lines.get(nameIndex).equals(name)) { | |
break; | |
} | |
} | |
int index = nameIndex + 1; | |
while(!lines.get(index).equals("")) { | |
// This loop header is okay because the final elem in lines is always an empty string | |
toReturn += lines.get(index) + "\n"; | |
indes++; | |
} | |
return toReturn; | |
} | |
// 2a | |
public static int countA(WordSet s) { | |
int count = 0; | |
for(int i = 0; i < s.size(); i++) { | |
if(s.findkth(i).indexOf('A') == 0) { | |
count++; | |
} | |
} | |
return count; | |
} | |
// 2b | |
public static void removeA(WordSet s) { | |
int count = countA(s); | |
while(count != 0) { | |
s.remove(s.findkth(1)); | |
count--; | |
} | |
} | |
// 2c | |
public static WordSet commonElements(Wordset s1, Wordset s2) { | |
WordSet temp = new WordSet(); | |
for(int i = 1; i <= s1.size(); i++) { | |
if(s2.contains(s1.findkth(i))) { | |
temp.insert(s1.findkth(i)); | |
} | |
} | |
return temp; | |
} | |
// 3a | |
public void updateLocation(int newRow, int newCol) { | |
loc = new Location(newRow, newCol); | |
} | |
// 3b | |
public void sortAllRows() { | |
for(Contestant[] elem : contestants) { | |
sort(elem); | |
} | |
} | |
// 3c | |
public String findWinnerName() { | |
sortAllRows(); | |
int max = Integer.MIN_VALUE; | |
String winner = ""; | |
for(Contestant[] elem : contestants) { | |
Contestant highestInRow = elem[elem.length - 1]; | |
if(highestInRow.getScore() > max) { | |
max = highestInRow.getScore(); | |
winner = highestInRow.getName(); | |
} | |
} | |
return winner; | |
// I use a for-each instead of a for, but I think it works still | |
} | |
// 4a | |
public class SnowyOwl extends Owl { | |
public SnowyOwl() { | |
super("Snowy owl"); | |
} | |
public String getFood() { | |
int rand = (int)(Math.random() * 3); | |
if(rand == 0) return "hare"; | |
else if(rand == 1) return "lemming"; | |
else return "small bird"; | |
} | |
} | |
// 4b | |
public void allEat() { | |
for(Bird b : birdList) { | |
System.out.println(b.getName + " " + b.getFood); | |
} | |
} | |
// NOTES: | |
// Is knowledge of instanceOf required? Will we have to write any .compareTo methods on the test? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment