The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 4 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
The
AirSeatingclass 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
}
}
Â
Implement the Airseating method empty, lastPatronTest, printSeat and ownSeatCount.Â
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly