Last active
May 2, 2016 22:19
-
-
Save gschaffner/103b99613c76412250b2a0fd54a5ab61 to your computer and use it in GitHub Desktop.
APCS Written Test Practice Barron's 3
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 boolean isAisleSeat(int row, int seatNumber) { | |
return seatNumber == 0 || seatNumber == SEATS_PER_ROW - 1; | |
} | |
// 1b | |
public boolean twoTogether() { | |
for(int i = 0; i < NUM_ROWS; i++) { | |
for(int j = 0; j < SEATS_PER_ROW - 1; j++) { | |
if(seats[i][j] == 0 && seats[i][j + 1] == 0) { | |
seats[i][j] = 1; | |
seats[i][j + 1] = 1; | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
// 1c | |
public int findAdjacent(int row, int seatsNeeded) { | |
for(int i = 0; i <= SEATS_PER_ROW - seatsNeeded; i++) { | |
boolean success = true; | |
for(int j = 0; j < seatsNeeded; j++) { | |
if(seats[row][i + j] == 1) | |
success = false; | |
} | |
if(success) | |
return i; | |
} | |
return -1; | |
// Very different from what the key has, needs a check | |
} | |
// 2a | |
public String replaceAll(String line, String sub, String repl) { | |
String toReturn = ""; | |
int lastIndexOf = 0 - sub.length(), indexOf = line.indexOf(sub); | |
while(indexOf != -1) { | |
toReturn += line.substring(lastIndexOf + sub.length(), indexOf); | |
toReturn += repl; | |
lastIndexOf = indexOf; | |
indexOf = line.indexOf(sub, indexOf + 1); | |
} | |
toReturn += line.substring(lastIndexOf + sub.length()); | |
return toReturn; | |
// Very different from what the key has, needs a check | |
// Also, why is this entire method not static? | |
} | |
// 2b | |
public void createPersonalizedLetters() { | |
for(int i = 0; i < customers.size(); i++) { | |
List<String> temp = makeCopy(); | |
for(int j = 0; j < temp.size(); j++) { | |
temp.set(j, replaceAll(temp.get(j), "@", customers.get(i).getName())); | |
temp.set(j, replaceAll(temp.get(j), "&", customers.get(i).getCity())); | |
temp.set(j, replaceAll(temp.get(j), "$", customers.get(i).getState())); | |
} | |
writeLetter(temp); | |
} | |
} | |
// 3 | |
public class Outfit implements ClothingItem { | |
// I forgot the implements until I was typing it | |
private Shoes shoes; | |
private Pants pants; | |
private Top top; | |
public Outfit(Shoes s, Pants p, Top t) { | |
shoes = s; | |
pants = p; | |
top = t; | |
} | |
public String getDescription() { | |
return shoes.getDescription() + "/" + pants.getDescription() + "/" + top.getDescription() + " outfit"; | |
} | |
public double getPrice() { | |
double s = shoes.getPrice(); | |
double p = pants.getPrice(); | |
double t = top.getPrice(); | |
if(s + p >= 100 || p + t >= 100 || t + s >= 100) | |
return 0.75 * (s + p + t); | |
else | |
return 0.90 * (s + p + t); | |
} | |
} | |
// 4a | |
public void shuffle() { | |
for(int k = tiles.size() - 1; k > 0; k--) { | |
// I accidentally had k++ until I was typing it | |
int rand = (int)(Math.random * (k + 1)); | |
Tile temp = tiles.get(k); | |
tiles.set(k, tiles.get(rand)); | |
tiles.set(rand, temp); | |
} | |
unusedSize = tiles.size(); | |
} | |
// 4b | |
public void replaceTiles(TileSet t) { | |
while(playerTiles.size() < NUM_LETTERS && !t.allUsed()) { | |
playerTiles.add(t.getNewTile()); | |
} | |
} | |
// 4c | |
public int getWordScore(int[] indexes) { | |
if(indexes[0] == -1) | |
// This is okay because the precondition states that indexes is sorted in ascending order, | |
// and no other negative values can be in indexes. | |
return 0; | |
int sum = 0; | |
for(int i = 0; i < indexes.length; i++) { | |
sum += playerTiles.get(indexes[i]).getValue(); | |
} | |
if(indexes.length == NUM_LETTERS) | |
sum += 20; | |
return sum; | |
} | |
// NOTES: | |
// final is the keyword for constants. Variables denoted final should be named in ALL_CAPS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment