Page 1364 - AP Computer Science A, 7th edition
P. 1364

Section II
1. (a) public boolean isAisleSeat (int row, int seatNumber)
{
return seatNumber == 0 || seatNumber == SEATS_PER_ROW – 1;
}
(b) public boolean twoTogether() {
for (int r = 0; r < NUM_ROWS; r++)
for (int c = 0; c < SEATS_PER_ROW–1; c++)
if (seats[r][c] == 0 && seats[r][c+1] == 0) {
seats[r][c] = 1; seats[r][c+1] = 1; return true;
} return false;
}
(c) public int findAdjacent(int row, int seatsNeeded) {
int index = 0, count = 0, lowIndex = 0; while (index < SEATS_PER_ROW)
{
while (index < SEATS_PER_ROW && seats[row] [index] == 0)
{
count++;
index++;
if (count == seatsNeeded)
return lowIndex;
}
count = 0; index++;
lowIndex = index;
}
return –1;









































































   1362   1363   1364   1365   1366