Skip to content

Instantly share code, notes, and snippets.

@gschaffner
Last active April 21, 2016 04:02
Show Gist options
  • Save gschaffner/eaa8b9a7462b178653d1f4fcc37c168c to your computer and use it in GitHub Desktop.
Save gschaffner/eaa8b9a7462b178653d1f4fcc37c168c to your computer and use it in GitHub Desktop.
APCS Written Test Practice 2014
// 1a
public static String scrambleWord(String word) {
String newWord = "";
for(int i = 0; i < word.length - 1; i++) {
if(word.charAt(i) == 'A' && word.charAt(i + 1) != 'A') {
newWord += word.charAt(i + 1) + word.charAt(i);
i++;
} else {
newWord += word.charAt(i);
}
}
newWord += word.charAt(word.length - 1); // important! loop only goes to length - 1 to avoid
// an ArrayOutOfBoundsException in the if statement
return newWord;
}
// 1b
public static void scrambleOrRemove(List<String> wordList) {
for(int i = 0; i < wordList.size(); i++) {
String scrambled = scrambleWord(wordList.get(i));
if(wordList.get(i).equals(scrambled)) {
wordList.remove(i);
i--;
} else {
wordList.set(i, scrambled);
}
}
}
// 2a
// skipped because we didn't do GridWorld
// 2b
// skipped because we didn't do GridWorld
// 3a
public SeatingChart(List<Student> studentList, int rows, int cols) {
seats = new Student[rows][cols];
int studentIndex = 0; // the next student to assign a seat to
for(int c = 0; c < seats[0].length; c++) {
for(int r = 0; r < seats.length; r++) {
if(studentIndex < studentList.size()) {
seats[r][c] = studentList.get(studentIndex);
studentIndex++; // don't forget this!
}
// we don't need an else, as objects have a default value of null
}
}
}
// 3b
public int removeAbsentStudents(int allowedAbsences) {
int studentsRemoved = 0;
for(int c = 0; c < seats[0].length; c++) {
for(int r = 0; r < seats.length; r++) {
if(seats[r][c] != null && seats[r][c].getAbsenceCount() > allowedAbsences) { // I forgot the null check,
// which is quite important
seats[r][c] = null;
studentsRemoved++;
}
}
}
return studentsRemoved;
}
// 4
public class Trio implements MenuItem {
private Sandwich mySandwich;
private Salad mySalad;
private Drink myDrink;
public Trio(Sandwich sand, Salad sal, Drink dr) {
mySandwich = sand;
mySalad = sal;
myDrink = dr;
}
public String getName() {
return mySandwich.getName() + "/" + mySalad.getName() + "/" + myDrink.getName() + " Trio";
}
public double getPrice() {
double sandwichPrice = mySandwich.getPrice();
double saladPrice = mySalad.getPrice();
double drinkPrice = myDrink.getPrice();
// This will work, but I'm not supposed to do it apparently... :(
//return ((sandwichPrice >= saladPrice && saladPrice >= drinkPrice) ? (sandwichPrice + saladPrice) :
// (saladPrice >= drinkPrice && drinkPrice >= sandwichPrice) ? (saladPrice + drinkPrice) :
// (drinkPrice + sandwichPrice));
// This is much more boring than ternary. :(
if(sandwichPrice >= saladPrice && saladPrice >= drinkPrice)
return (sandwichPrice + saladPrice);
else if(saladPrice >= drinkPrice && drinkPrice >= sandwichPrice)
return (saladPrice + drinkPrice);
else
return (drinkPrice + sandwichPrice);
}
// the whole getPrice method as one line:
//public double getPrice() { return ((mySandwich.getPrice() >= mySalad.getPrice() && mySalad.getPrice() >= myDrink.getPrice()) ? (mySandwich.getPrice() + mySalad.getPrice()) : (mySalad.getPrice() >= myDrink.getPrice() && myDrink.getPrice() >= mySandwich.getPrice()) ? (mySalad.getPrice() + myDrink.getPrice()) : (myDrink.getPrice() + mySandwich.getPrice())); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment