Here is the AirPlaneSeatInitializer class containing the 2D array...
import javax.swing.*; public class AirPlaneSeatInitializer { private static String [] seatLayout = {"4509", "2707", "1606", "1505", "3507", "3306", "3005", "4208"}; private static int layOutController = -1; /** * Returns the number of rows in a seat layout string of the form #### * * @return int the int value number of rows */ public static int getRows(String layOut) { return Integer.parseInt(layOut.substring(0, 2)); } /** * Returns the number of columns in a seat layout string of the form #### * * @return int the int value number of columns */ public static int getColumns(String layOut) { return Integer.parseInt(layOut.substring(2)); } /** * Resets the object so that is may be used again */ public static void reset() { layOutController = -1; } /** * Checks if the object contains more layout strings. * * @return true if there are more Layout Strings. Returns false otherwise. */ public static boolean hasMoreLayoutStrings() { if (layOutController < seatLayout.length - 1) return true; return false; } /** * Returns the next Layout stirng in the object. Must use with hasMoreLayoutStrings() * * @return String the Layout String Objects */ public static String getNextLayoutString() { layOutController++; return seatLayout[layOutController]; } /** * User can pick a plane randomly with a particular layout * * @return String the plane's payout in the form #### */ public static String pickAPlaneWithLayout() { int selected = -1; while (selected < 0 || selected > 7) { double val = Math.random() * 10; //System.out.println(val); selected = (int) val; //System.out.println(selected); } return seatLayout[selected]; } private static void showSeats(char [][]seatArray) { for (int i = 0; i < seatArray.length; i++) { for (int j = 0; j < seatArray[0].length; j++) System.out.print("" +seatArray[i][j]+ " "); System.out.println("\n"); } } /** * Given number of rows and columns, this method will return a 2-D array * of with some cells filled with 'C' and some filled with 'X'. The number * of rows must within the range 15 to 45 and the number of columns must * within the range 5 to 9. * * @return char[][] a 2-D character array. */ public static char [][]get2DSeatsArrayForAirPlane( int rows, int cols) { char [][] seatsArray = new char[rows][cols]; int seatsFilled = 0; if ((rows > 45 || rows < 15) || (cols < 5 || cols > 9)) { JOptionPane.showMessageDialog(null, "Your request cannot be granted. Please\n" + "enter a valid number of rows and/or\n" + "valid number of seats per row.\n" + "Rangers allowed - 15 <= rows <= 45 and\n"+ "5 <= seats-per-row <= 9.", "Error", JOptionPane.ERROR_MESSAGE); return null; } for (int i = 0; i < seatsArray.length; i++) for (int j = 0; j < seatsArray[0].length; j++) seatsArray[i][j] = 'X'; seatsArray [0][0] = 'C'; seatsArray [0][cols-1] = 'C'; seatsArray [rows-1][0] = 'C'; seatsArray [rows-1][cols-1] = 'C'; seatsFilled = 4; if (rows < 28) { seatsArray [(rows-1)/2][0] = 'C'; seatsArray [(rows-1)/2][cols-1] = 'C'; seatsFilled += 2; } else if (rows >= 28) { seatsArray [(int)(rows* 0.25)][0] = 'C'; seatsArray [(int)(rows* 0.25)][cols-1] = 'C'; seatsArray [(int)(rows* 0.75) - 2][0] = 'C'; seatsArray [(int)(rows* 0.75) - 2][cols-1] = 'C'; seatsFilled += 4; } int seed = -1; //Find a % to dynamically fill the seats //any percent between 0% and 80% while (seed < 0 || seed > 80) { double val = Math.random() * 100; //System.out.println(val); seed = (int) val; //System.out.println(seed); } //calculate the number of sets to be filled int seatsToBeFilled = seatsFilled + (rows * cols)* seed/100; int currentFill = 0; int row_pos = -1 , col_pos = -1; while (currentFill < seatsToBeFilled) { //randomly find a row row_pos = -1 ; while (row_pos < 0 || row_pos > rows-1) { double val = Math.random() * 100; //System.out.println(val); row_pos = (int) val; } //Randomly find a col col_pos = -1; while (col_pos < 0 || col_pos > cols-1) { double val = Math.random() * 100; //System.out.println(val); col_pos = (int) val; } //Randomly fill a seat if (seatsArray[row_pos][col_pos] == 'X') { seatsArray[row_pos][col_pos] = '*'; currentFill++; } }//end outer while return seatsArray; } /** public static void main(String[] args) { AirPlaneSeatInitializer.reset(); char ary[][] = get2DSeatsArrayForAirPlane(45, 9); System.out.println("Rows : " + ary.length + ", Cols : " + ary[0].length); for (int i = 0; i < ary.length-1; i++) { for (int j = 0; j < ary[0].length-1; j++) { System.out.print(ary[i][j] + " "); } System.out.println(""); } } **/ } And here is the AirPlane class that prompts the user for the data...
import javax.swing.*; import java.util.*; public class AirPlane { public static void main (String args[]) { String input1 = JOptionPane.showInputDialog("Welcome to the Airline Reservation System!\n\nPlease select your option:\n1. Reserve a seat\n2. Show current seating by rows\n3. Exit"); int inValue1 = Integer.parseInt(input1); if ( inValue1 == 1 ) { String input2 = JOptionPane.showInputDialog("Enter the number of adult passengers: "); int inValue2 = Integer.parseInt(input2); String input3 = JOptionPane.showInputDialog("Enter the number of passengers under 2:"); int inValue3 = Integer.parseInt(input3); if ( inValue3 > 0 ) { JOptionPane.showMessageDialog(null, "You must have been assigned to the Economy class.", "Economy Class", JOptionPane.PLAIN_MESSAGE); } if ( inValue3 > inValue2 ) { JOptionPane.showMessageDialog(null, "You must have at least 1 adult per 1 child!", "Error", JOptionPane.ERROR_MESSAGE); } if ( inValue3 < 1 ) { String input5 = JOptionPane.showInputDialog("Select you class: F = First B = Business E = Economy"); } } else if ( inValue1 == 2 ) { String input4 = JOptionPane.showInputDialog("Which rows would you like to view?\n\n1. Rows 1-4\n2. Rows 5-8\n3. Rows 9-12\n4. Rows 13-16\n5. All Rows"); int inValue4 = Integer.parseInt(input4); if ( inValue4 == 1 ) { char ary[][] = AirPlaneSeatInitializer.get2DSeatsArrayForAirPlane(16, 6); System.out.println("Rows : 1-4, Cols : " + ary[0].length); for (int i = 0; i < 4; i++) { for (int j = 0; j < ary[0].length-1; j++) { System.out.print(ary[i][j] + " "); } System.out.println(""); } } if ( inValue4 == 2 ) { char ary[][] = AirPlaneSeatInitializer.get2DSeatsArrayForAirPlane(16, 6); System.out.println("Rows : 5-8, Cols : " + ary[0].length); for (int i = 4; i < 8; i++) { for (int j = 0; j < ary[0].length-1; j++) { System.out.print(ary[i][j] + " "); } System.out.println(""); } } if ( inValue4 == 3 ) { char ary[][] = AirPlaneSeatInitializer.get2DSeatsArrayForAirPlane(16, 6); System.out.println("Rows : 9-12, Cols : " + ary[0].length); for (int i = 8; i < 12; i++) { for (int j = 0; j < ary[0].length-1; j++) { System.out.print(ary[i][j] + " "); } System.out.println(""); } } if ( inValue4 == 4 ) { char ary[][] = AirPlaneSeatInitializer.get2DSeatsArrayForAirPlane(16, 6); System.out.println("Rows : 13-16, Cols : " + ary[0].length); for (int i = 12; i < 16; i++) { for (int j = 0; j < ary[0].length-1; j++) { System.out.print(ary[i][j] + " "); } System.out.println(""); } } if ( inValue4 == 5 ) { char ary[][] = AirPlaneSeatInitializer.get2DSeatsArrayForAirPlane(16, 6); System.out.println("Rows : " + ary.length + ", Cols : " + ary[0].length); for (int i = 0; i < ary.length-1; i++) { for (int j = 0; j < ary[0].length-1; j++) { System.out.print(ary[i][j] + " "); } System.out.println(""); } if ( inValue1 == 3 ) { System.exit(0); } } } } }