Small airline company assignment
Back again with another assignment...
everytime i chose a seat is says that the seat is taken, couldn't figure out where the problem is at:
Code :
public class Plane {
char[][] chart;
int ROWS = 7;
int COLS = 5;
Plane() {
chart = new char[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
chart[i][0] = 'A';
chart[i][1] = 'B';
chart[i][2] = ' ';
chart[i][3] = 'C';
chart[i][4] = 'D';
}
}
public boolean assignSeat(String str) {
boolean result = false;
str = str.toUpperCase();
if (!okRow(str.charAt(0)) || !okSeat(str.charAt(1))) {
System.out.println("Seat format is invalid\n");
} else {
result = markSeat(str.charAt(0), str.charAt(1));
if (result) {
System.out.println("The seat is taken\n");
} else {
System.out.println("The seat is busy, try another one\n");
}
}
return result;
}
public String toString() {
String result = "";
for (int i = 0; i < ROWS; i++) {
result += Integer.toString(i + 1);
result += " ";
for (int j = 0; j < COLS; j++) {
result += chart[i][j];
}
result += "\n";
}
return result;
}
private boolean okRow(char r) {
if (r >= '1' && r <= '7') {
return true;
} else {
return false;
}
}
private boolean okSeat(char s) {
if (s >= 'A' && s <= 'D') {
return true;
} else {
return false;
}
}
private boolean markSeat(char row, char seat) {
int rowIndex, seatIndex;
boolean result = false;
rowIndex = row - '1';
seatIndex = seat - 'A';
if (seatIndex > 1) {
seatIndex++;
}
if (chart[rowIndex][seatIndex] != 'X') {
result = true;
chart[rowIndex][seatIndex] = 'X';
}
return result;
}
public boolean isPlaneFull() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (chart[i][j] != 'X' && chart[i][j] != ' ') {
return false;
}
}
}
return true;
}
}
and i'm using this to test it:
Code :
import java.util.Scanner;
public class PlaneTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String cmd;
System.out.println("Welcome, the plane is currently empty");
Plane commuter1 = new Plane();
System.out.println(commuter1.toString());
System.out.println();
System.out.println("Please enter a seat location to reserve and press enter to reserve row 1 seat A enter 1A followed by the enter key");
System.out.println("To exit type Q or q");
cmd = input.nextLine();
while(!cmd.equals("Q") && !cmd.equals("q") && !commuter1.isPlaneFull()){
commuter1.assignSeat(cmd);
System.out.println();
System.out.println("Please enter a seat location to reserve and press enter to reserver row 1 seat A enter 1A followed by the enter key");
System.out.println("To exit type Q or q");
cmd = input.nextLine();
}
System.out.println("You are now exiting.");
System.out.println(commuter1.toString());
}
}
Re: Small airline company assignment
Quote:
it says that the seat is taken
Try debugging the code by printing out the values of the variables that are used to make that decision. The print outs will show you what the computer is seeing and help you understand why the computer is doing what it is doing.
Re: Small airline company assignment
Sorry for the late respond...
by values you mean the result?
Re: Small airline company assignment
Quote:
the values of the variables
Given this statement:
x = 4;
the value of the variable x is 4.
Given this statement:
private boolean markSeat(char row, char seat) {
what are the values of the variables: row and seat?
Re: Small airline company assignment
So I tested the variables...
the variables change based on what my input is
if I say i wanna check 1A
it shows row 1, seat A
and thats how it supposed to be! :-??
Re: Small airline company assignment
OK, that part works. What about the rest of the code and variables?
What is the method: markSeat() supposed to do? Does it do it correctly? When does it return true? When does it return false?
What is the value of chart at rowIndex and seatIndex when markSeat() is called?