<p>public class PlainModel<br /><br />{<br /> private String[] planeArray;<br />
ID: 3633525 • Letter: #
Question
<p>public class PlainModel<br /><br />{<br /> private String[] planeArray;<br /><br /> public PlainModel()<br /> {<br /> planeArray =new String[10];<br /> for(int i=0;i {<br /> planeArray[i]= "x" ;<br /> }<br /><br /> }<br /><br /> public void printList()<br /><br /> {<br /> for(int i=0 ; i {<br /> System.out.println(i+" "+planeArray[i] );<br /> }<br /><br /> System.out.println();<br /> }<br /> public void reservation(String name, int seat)<br /> {<br /> if(planeArray[seat].equals("x"))<br /> {<br /> planeArray[seat]=name;<br /> System.out.println("thnks 4r flyin ");<br /> }<br /> else<br /> {<br /> System.out.println("choose a diff seat");<br /> }<br /><br /><br /> }<br /> public int emptySeats()<br /> {<br /> int seatCount=0;<br /> for(int i=0;i {<br /> if (planeArray[i].equals("x"))<br /> {<br /> seatCount++;<br /> }<br /><br /> }<br /> return seatCount;<br /> }<br /><br /> public void cancel(String name)<br /> {<br /> boolean found=false;<br /><br /> for(int i=0;i {<br /> if(planeArray[i].equals(name))<br /> {<br /> planeArray[i]="x";<br /> found=true;<br /> }<br /> else<br /> {<br /> System.out.println("choose a diff seat");<br /> }<br /> }<br /> if (found== false)<br /> {<br /> System.out.println("sorry this passenger is not on the flight");<br /> }<br /> else<br /> {<br /> System.out.println("cancelled thaat shiit");<br /><br /> }<br /><br /> }<br /><br /> public void coupleSeats()<br /> {<br /><br /> for(int i=0;i {<br /> if ( (planeArray[i].equals("x")) && (planeArray[i+1].equals("x") ));<br /> {<br /> System.out.println(planeArray[i]+" "+ planeArray[i+1]+"available");<br /> }<br /><br /> }<br /><br /><br /><br /> }<br /><br /><br />}</p>Explanation / Answer
Hi, apart from the syntax error (semi colon in if statement) you don't really explain much what you need since a "void" return value doesn't return anything and as far as I can see, your code looks fine. Except for cramster messing it up with crappy formatting and breaking your for loops there.
So I re-wrote your code, closed the broken for-loops, and added a second method getCoupleSeats() just in case I misunderstood your question. getCoupleSeats() returns a 2-item array of integers indicating 2 adjacent seat indices to use in a couples reservation. it will return the first couples' seats par it finds, or null if it went through all the free seats and haven't found any free ones next to eachother. Otherwise, if all you need is to display possible ouples' seats then your original coupleSeats() method works just fine. ECEPT FOR the fact that your loop here needs to stop at i < 9, since it's checking i and i+1, if you let it go all the way to the last seat, it can't check a non-existent seat next to it at i+1 and will throw an index out of bounds exception or smthing.
I hope this helps, please remember to rate ! :)
public class PlainModel {
private String[] planeArray;
public PlainModel() {
planeArray = new String[10];
for (int i = 0; i < 10; i++ ) {
planeArray[i] = "x";
}
}
public void printList() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + planeArray[i]);
}
System.out.println();
}
public void reservation(String name, int seat) {
if (planeArray[seat].equals("x")) {
planeArray[seat] = name;
System.out.println("thnks 4r flyin ");
} else {
System.out.println("choose a diff seat");
}
}
public int emptySeats() {
int seatCount = 0;
for (int i = 0; i < 10; i++) {
if (planeArray[i].equals("x")) {
seatCount++;
}
}
return seatCount;
}
public void cancel(String name) {
boolean found = false;
for (int i = 0; i < 10; i++) {
if (planeArray[i].equals(name)) {
planeArray[i] = "x";
found = true;
} else {
System.out.println("choose a diff seat");
}
}
if (found == false) {
System.out.println("sorry this passenger is not on the flight");
} else {
System.out.println("cancelled thaat shiit");
}
}
public void coupleSeats() {
// check the first 9 seats - not 10
for (int i = 0; i < 9; i++) {
if ((planeArray[i].equals("x")) && (planeArray[i + 1].equals("x")))
{
System.out.println(planeArray[i] + " " + planeArray[i + 1] + "available");
}
}
}
public int[] getCoupleSeats()
{
int[] twoFreeAdjacentSeats = new int[2];
// check the first 9 seats - not 10
for (int i = 0; i < 9; i++) {
if ((planeArray[i].equals("x")) && (planeArray[i + 1].equals("x")))
{
twoFreeAdjacentSeats[0] = i;
twoFreeAdjacentSeats[1] = i+1;
return twoFreeAdjacentSeats;
}
}
// if you haven't returned anything by now
// then there's no place for couples on this flight
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.