java - How to create boolean method? -
i'm trying create boolean method called isavailable accepts seat number on flight parameter, returns boolean indicates whether seat available. code similar tic-tac-toe program, code convert seat number 20 row , column index two-dimensional array. returns true if there any available seats on flight.
public class flight { /* * declare instance variables * each flight object created have own re-create of these * these declared private ensure external java code * cannot access them straight , assign arbitrary values them. */ string flightnumber; int rows; int seatsperrow; boolean [][] seat; double fare; string origin; string destination; /* * constructor * contains code sets each flight object when * first created. has same name class. */ public flight (string flightnumber, int rows, int seatsperrow, double fare, string origin, string destination) { //call mutator methods , allow them initialize instance variables setflightnumber(flightnumber); setrows(rows); setseatsperrow(seatsperrow); setfare(fare); setorigin(origin); setdestination(destination); seat = new boolean [rows][seatsperrow] for(int = rows, int x = seatsperrow; i>0, x>0; i--, x--){ seat[i][x] = true; } }//end of constructor /* * accessor methods * these study current values of flight object's instance variables */ public string getflightnumber() { homecoming flightnumber; } public int getrows() { homecoming rows; } public int getseatsperrow() { homecoming seatperrow; } public double getfare() { homecoming fare; } public string getorigin() { homecoming origin; } public string getdestination() { homecoming destination; } public boolean isavailable(int userrow, int userseatperrow) { int row = 0; int seatperrow = 0; } }//end of accessors
is looking for?
public boolean isavailable(int userrow, int userseatperrow) { homecoming !seat[userrow][userseatperrow]; }
this assumes seat[x][y]
array true if seat in row x, place y occupied
if seat occupied, seat[userrow][userseatperrow]
true
.
when !
, invert logic, true
becomes false
.
so function returns false
seat occupied, since not available. opposite happens if seat empty. function returns true
to find first available seat this:
(int row = 0; row < getrows(); row++) { (int column = 0; column < getseatsperrow(); column++) { if (isavailable(row, column)) { system.out.println("sear " + row +", " + column + " emply!!!!!!"); } } }
to check if there available seats
public boolean arethereanyavailableseats() { (int row = 0; row < getrows(); row++) { (int column = 0; column < getseatsperrow(); column++) { if (!seat[userrow][userseatperrow]) { homecoming true; } } } homecoming false; }
java arrays loops multidimensional-array
No comments:
Post a Comment