Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 2D Array Question

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2D Array Question

    Let's say in one class I have a 2D array with rows 16 and columns 6 named AirPlaneSeatInitializer. Now in another class I have code that prompts the user to input data named AirPlane. For this particular program it also asks what class the user wants (First, Business, Economy)...

    I would like to take the data (2) that was just entered into variable inValue2, and put it into the 2D array, changing the element value from * to X. Also, if the element value is already an X, that element is skipped over and it searches for the next element in the array. Furthermore, the seats must be adjoining.

    How would I go about doing that? I understand the logic: if ( inValue2 > 0 ) array element [i][j] = X

    I already have the code to display the current seating arrangement without user data being entered, and I have already set it up to prompt the user for all of the input data...
    Last edited by gmorris1986; June 18th, 2010 at 11:41 AM.


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: 2D Array Question

    Why don't you put up the code you already have?

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D Array Question

    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);
          }
     
      	 }
          }
       }
    }

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. A question about an array
    By faizana2006 in forum Collections and Generics
    Replies: 2
    Last Post: October 28th, 2009, 04:36 PM
  4. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM