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 5 of 5

Thread: Arrays?

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Arrays?

    I have the code:

     
    public class main {
     
    	public static void main(String[] args) {
     
    	//ARRAY for first class
    		seat[][] FC = new seat[4][3];
    	    for(int i = 0; i < 5; i++)
    	            for(int j = 0; j < 4; j++)
    	            {
    	            FC[i][j] = new seat();         
    	            } 
     
    	    //ARRAY for economy class
    	    seat[][] EC = new seat[14][5]; 
    	    for(int i = 0;i<14;i++)
    	    		for(int j = 0;j<5;j++) {
    	    			EC[i][j] = new seat();
    	    		}
     
    	ReserveEC(EC);
     
    	System.out.println(FC[0][3].getStatus());
     
    	}
     
    	//METHOD reserve FC
    	public static void ReserveFC(seat x[][], String a) {
    		String choice = a;
    		if (choice.equals("window")) {
    		for(int i=0;i<4;i++){
    			for(int j=0;j<3;j+=3) {
    				if( x[i][j].getStatus() == false)  {
    			x[i][j].setStatus(true);
    			break;
    				}
    			}
    			if (choice.equals("aisle")) {
    				for(int k=0;k<4;k++){
    					for(int l=1;l<3;l++) {
    						if( x[k][l].getStatus() == false)  {
    					x[k][l].setStatus(true);
    					break;
    						}
    					}
     
    			}
    		}
    		}
    		}
    			}
    	//
     
    	//METHOD reserve EC
    	public static void ReserveEC(seat x[][]) {
     
     
    		for(int i=0;i<14;i++){
    			for(int j=0;j<5;j++) {
    				if( x[i][j].getStatus() == false)  {
    			x[i][j].setStatus(true);
    			break;
    				}
    			}
     
    		}
    			}
    	//
     
     
    	}

    and when I run it, I get the error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at main.main(main.java:11)

    I've looked at line 11 ( FC[i][j] = new seat() and I really don't see what I need to fix to be rid of this error...

    I want my parameters for the FC array (first class) to be 5x4, and EC (economy class) to be 15x6 (so I set them to 4x3 and 14x5) anyone see any issues that could help me correct this error?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Arrays?

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3" indicates that your array is of only size 3 (indexes range from 0 to 2, inclusively).
    When looping through an array, you want to loop: while i (or j) is less than the length of the array. This is because you do not want the length of the array (in this case: 3) included in the indexes.
    Does that make sense?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arrays?

    Hard coding numbers for testing array sizes is a poor technique. You should use the array's .length attribute:
    x.length for the first dim and x[i].length for the second dim
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Arrays?

    Thanks! That cleared it up. My code is getting incredibly convoluted though, and I'd rather have everything separated into their own classes. The question is:
    Airline seating. Write a program that assigns seats on an airplane. Assume the airplane has 20 seats in first class (5 rows of 4 seats each, separated by an aisle) and 90 seats in economy class (15 rows of 6 seats each, separated by an aisle). Your program should take three commands: add passengers, show seating, and quit. When passengers are added, ask for the class (first or economy), the number of passengers traveling together (1 or 2 in first class; 1 to 3 in economy), and the seating preference (aisle or window in first class; aisle, center, or window in economy). Then try to find a match and assign the seats. If no match exists, print a message. Your solution should include a class Airplane that is not coupled with the Scanner or PrintStream classes.

    Any idea on a basic outline for this? I've got the seat class, and I'd use that in a firstclass class and an economyclass class, maybe?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Arrays?

    Lol, I work in the airline industry (or I did, up until a month ago).

    If you wanted to go full-out on the object-oriented design (which is probably what you should do, since this is java), consider that the first step in outline a problem statement is locating the nouns. Your nouns will be your classes (most of the nouns, not all of them).
    Step 1:
    So, in your problem statement, your nouns are:
    1. Seat
    2. Airplane
    3. First Class
    4. Row
    5. Economy Class
    6. Passenger
    Step 2:
    There are a few others, but that seems to be most of it. Ok, now we look at our nouns. Some of them are not worth making classes out of. The problem statement says we need an Airplane class, so that is a must. To narrow down our nouns, let's look at their function. More importantly: what type of traits do these nouns have?
    1. Seat - Has type (aisle, window, center). Has Passenger (sometimes).
    2. Airplane - Has Seats. Has First Class. Has Economy Class. Has Passengers.
    3. First Class - Has 20 Seats. Has 5 Rows.
    4. Row - Has Seats
    5. Economy Class - Has 90 Seats. Has 15 Rows.
    6. Passenger - Has Seat.
    Step 3:
    Now we have to look over our nouns and traits and determine the best way to organize it all. First Class and Economy Class both have seats and rows, and they are both used the same way. This is an indication that they should probably have a relationship of some kind (inheritance - they should both extend from the same object, perhaps a SeatingClass object or something). Now, SeatingClasses have Seats and Rows. Perhaps instead of having both, Rows could contain Seats, and SeatingClasses could contain Rows. Or, since rows serve no real purpose other than holding Seats, perhaps Seats should just contain an integer variable, referencing a row number, and we get rid of the Row object altogether. I'll do the latter here. Now, back to Airplane: since SeatingClasses now contain Seats (which know their respective row numbers), why should the Airplane also contain a list of Seats? Our Airplane contains SeatingClasses (FirstClass and EconomyClass), which each contains their respective Seat lists, so we already covered the airplane's seat trait transitively. Now, if a Seat object contains a Passenger (provided it has been assigned), there is also no reason for an airplane to contain a list of Passengers (we can get that from the seat assignments). A Passenger can have a seat, there is nothing wrong there. So the last thing is the traits for Seats. A Seat can be either an aisle seat, a window seat, or a center seat. There are no special differences between them from a design point of view, so there is no reason to create separate classes for them. Instead, the Seat class can contain a variable indicating what type of seat it is.
    So, here is our trimmed down outline:
    1. Seat - Has type (aisle, window, center). Has Passenger (sometimes). Has a Row Number.
    2. Airplane - Has First Class. Has Economy Class.
    3. SeatingClass - Has Seats.
    3. First Class - Is A SeatingClass. Has 20 Seats.
    4. Economy Class - Is A SeatingClass. Has 90 Seats.
    5. Passenger - Has Seat.

    The next step will be determining what method each class should have. That one is up to you. Change the design based on how it makes sense when you implement.
    The 3 Steps I described will probably be one of the most important skills you can develop as a programmer. Make sure you understand what I explained, and ask any questions if you have them.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Help with Arrays
    By mstratmann in forum Object Oriented Programming
    Replies: 1
    Last Post: June 3rd, 2013, 09:19 PM
  2. Arrays
    By LeeDD in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2013, 09:10 PM
  3. Help with Arrays
    By djhunt90 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2012, 02:27 PM
  4. Need help using Arrays!
    By asb23698 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2012, 06:47 PM
  5. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM

Tags for this Thread