フライトの座席番号をパラメーターとして受け取り、その座席が利用可能かどうかを示すブール値を返す isAvailable というブール値メソッドを作成しようとしています。三目並べプログラムに似たコードを使用すると、コードは 20 のような座席番号を 2 次元配列の行と列のインデックスに変換します。フライトに空席がある場合は true を返します。
public class Flight {
/*
* declare instance variables
* each flight object that is created will have its own copy of these
* These are declared as private to ensure that external Java code
* cannot access them directly and assign any arbitrary values to them.
*/
String flightNumber;
int rows;
int seatsPerRow;
boolean [][] seat;
double fare;
String origin;
String destination;
/*
* The constructor
* Contains code that initially sets up each flight object when it is
* first created. Always has the same name as the class.
*/
public Flight (String flightNumber, int rows, int seatsPerRow, double fare, String origin, String destination) {
//call the mutator methods and let them initialize the instance variables
setflightNumber(flightNumber);
setRows(rows);
setSeatsPerRow(seatsPerRow);
setFare(fare);
setOrigin(origin);
setDestination(destination);
seat = new boolean [rows][seatsPerRow]
for(int i = rows, int x = seatsPerRow; i>0, x>0; i--, x--){
seat[i][x] = true;
}
}//end of constructor
/*
* The accessor methods
* These report the current values of the Flight object's instance variables
*/
public String getFlightNumber() {
return flightNumber;
}
public int getRows() {
return rows;
}
public int getSeatsPerRow() {
return seatPerRow;
}
public double getFare() {
return fare;
}
public String getOrigin() {
return origin;
}
public String getDestination() {
return destination;
}
public boolean isAvailable(int userRow, int userSeatPerRow) {
int row = 0;
int seatPerRow = 0;
}
}//end of accessors