Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The AirSeating class models a particular way in which an airplane\'s seats are f

ID: 3809675 • Letter: T

Question

The AirSeating class models a particular way in which an airplane's seats are filled. Here is the class source code: import java.util.Random; //size seats, nominally 0-99; patrons are 0-99 public class AirSeating{ private int size; private int[] seats; // if seats[7] holds 4, passenger 4 is in seat 7 Random ran = new Random(); public AirSeating(int planeSize){ size = planeSize; seats = new int[size]; } private void emptyPlane(){ for(int i = 0; i < size; i++) seats[i] = -1; // start with all at -1 } private void seatFirstPerson(){ // places 0th person in an empty plane int firstPersonPos = ran.nextInt(size); // pick random seat for person 0 seats[firstPersonPos] = 0; // put 0 in chosen seat } private int findKthEmpty(int k){ /* array seats holds numbers from 0 to size-1 - actual passengers, or -1 * which means that the seat is empty. What's index of kth empty seat? */ int ctr = k; // tallies number of empties seen as you walk seats int where = 0; while (where < size){ if (empty(where)) // is location "where" empty, that is, -1? if (ctr == 0) return where; else ctr--; where++; } System.out.println("error in findKthEmpty"); return(-1); // return statement required } private boolean empty(int loc){ // exercise } public void fillSeats(){ /* places all passengers in their seats * Variable leftOver holds number of empty seats left on plane * pick empty seat n at random: that's where p will go */ emptyPlane(); // build empty plane seatFirstPerson(); // places person 0 int leftOver = size-1; // person 0 already placed int n; // will be he kth empty seat for(int p = 1; p < size; p++){ if(empty(p)) {seats[p] = p; leftOver--;} // if p's seat empty: place p else { n = findKthEmpty(ran.nextInt(leftOver)); seats[n] = p; leftOver--; // in loop, after p placed there's one fewer empty seat left } } } public boolean lastPatronTest(){ // an exercise } public int printSeat(int who){ // an exercise } public int ownSeatCount(){ // an exercise } } The method lastPatronTest, with boolean return type, returns true or false depending on whether the last passenger on the plane gets his or her own seat. Provide an implementation for this method.

Explanation / Answer

In the code, it is mentioned that if seats[7] holds 4, passenger 4 is in seat 7 . Which means the passenger number is stored as an array value.

So, the required method whose definition you written, lastPatronTest() this method's work is that if the last passenger is getting his or her own seat, return true otherwise return false. I interpreted this as as the seats are allocated randomly to every passengers, if the last passenger i.e. 99 numbered passenger gets his/her own means seat 99 then it returns true or else returns false.

So, thinking this, the code required is:

public boolean lastPatronTest() { // an exercise
boolean isAssignedSeat = false;
if (seats[99] == 99) {
isAssignedSeat = true;
}

return isAssignedSeat;
}

This method can also be reduced in number of instructions. As we are just changing the variable value based on the comparison and do not do any modification, we can directly return the result of comparison as:

public boolean lastPatronTest() { // an exercise
return seats[99] == 99;

}

This should be the required code. I think it is fine. But if there is any modification, do comment. I will modify as per it.

Do comment if there is any query. Thank you. :)