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

Thread: College Assignment please help

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default College Assignment please help

    Hi im having trouble returning a array from a method, ive highlighted these section's of code in red, can any help me with this or recommend a thread where i can read up on the solution to my problem

    Here's the Assignment question i was given.
    Write a program that simulates the booking of seats on board a small airplane. The layout of the aircraft is illustrated
    below. You will be required to create a menu based system with the following choices:
    (a) View Seating (b) Book a Seat (c) Cancel a Booking (d) Reset All (e) Quit
    Each of the above will require a method which takes an array as a parameter. You can of course add more options to
    this basic menu list if you wish. However at all times the user should be able to view the state of the aircraft bookings
    i.e. option (a) and thus be presented with a representation of a 2D array similar to that below:
    You are required to output the current values of the array to show the current seating arrangement in graphic format. As
    can be seen, you will need output some space for an aisle to separate columns A and B from columns C and D.
    Similarly rows 1-4 (non-smoking) should be output separately from rows 5 – 6 (smoking)
    The grid references should also be output to the screen. A,B,C and D for the columns and 1-8 for the rows. You don’t
    have to simulate the box effect as in the diagram above, but try to keep the output as clear as possible with all reference
    points indicated.
    Use ‘X’ to indicate that a seat is booked and a hyphen ‘-‘ to indicate that the seat is still available.
    Bookings can be made by selecting option (b) At which point the user is asked which seat number (reference) they
    wish to book. For example, seat B4 is selected in the diagram above.
    Option (c) should allow the user to enter a booked seat reference and make it available again. Option (d) Should reset
    the bookings and free up ALL seats once again.
    Things to consider : Smoking or Non-Smoking:
    Seats in rows 5,6,7 and 8 are designated as smoking seats. When making a booking - option (c) - the user should be
    asked their preference (Smoking or non-smoking?) If for example they choose smoking and all seats are full in that
    section, they should be informed of this and offered a seat in the non-smoking section. If the seats are not full in the
    smoking section and they proceed to enter a reference number that is in the non-smoking section, they should be
    prompted to confirm their choice. The same should apply to seats chosen in the non-smoking section

    class abc
    {
    	public static void main(String[] args) //calling of methods from main method
    	{
    		char choice;
    		char array1[][] = new char[8][4];
    		do
    		{
    			System.out.println("please choose from the options below");
    			System.out.println("(a) View Seating (b) Book a Seat (c) Cancel a Booking (d) Reset All (e) Quit");
    			choice = Keyboard.readChar();
    			switch(choice)
    			{
    		     	case 'a':viewseating(array1);break;
    		     	case 'b':bookseat(array1);break;
    		     	case 'c':cancel(array1);break;
    		     	case 'd':reset(array1);break;
    		     	case 'e':System.out.println("thank you goodbye");break;
    		     	default: System.out.print("Enter (a)(b)(c)(d)(e)");
          		}//close switch
    		}while(choice != 'e');
    	}//close main
    /*****************************************************************************************************************/
     
     
    static void viewseating(char array1[][])//output array
    {
    	int i, j;
    	for(i=1; i<=4; i++)
    	{
    		for(j=1; j<=2; j++)
    		{
    			System.out.println(array1[i][j]);
    		}//close inner for
    	}//close outer for
    	System.out.print("\n\n\n\n\n");
    	for(i=1; i<= 4; i++)
    	{
    		for(j=3; j<=4; j++)
    		{
    			System.out.println(array1[i][j]);
    		}//close inner for
    	}//close outer for
    	System.out.println("");
    	for(i=5; i<=8; i++)
    	{
    		for(j=1; j<=2; j++)
    		{
    			System.out.println(array1[i][j]);
    		}//close inner for
    	}//close outer for
    	System.out.print("\n\n\n\n\n");
    	for(i=5; i<=8; i++)
    	{
    		for(j=3; j<=4; j++)
    		{
    			System.out.println(array1[i][j]);
    		}//close inner for
    	}//close outer for
    }//close method viewseating
     
    /******************************************************************************************************************/
     
    static char bookseat(char array1[][])//to do//assign values to a =1 b = 2 c =3 d = 4 ??for array??//needs if statement to check if seat is booked already
    {
     
    	int seat, i, j;
    	char row;
    	char ans, ans1;
    	array1[row][seat] = array1[i][j];
    	System.out.println("would you like smoking or non smoking");
    	System.out.println("press (a) for smoking or (b) for non smoking");
    	ans = Keyboard.readChar();
    	while(ans != 'a' && ans != 'b')//validating that correct letters have been entered
    	{
    		System.out.println("Please choose either (a) for smoking or (b) for non smoking");
    		ans = Keyboard.readChar();
    	}
    	if(ans == 'a')
    	{
    		System.out.println("please enter the row you wish to sit in (a)(b)(c)(d)");
    		row = Keyboard.readChar();
    		while(row != 'a' && row != 'b' && row !='c' && row != 'd')//validating that correct letters have been entered
    		{
    			System.out.println("There are only four rows (a)(b)(c)(d) please choose one");
    			row = Keyboard.readChar();
    		}//close while
    		System.out.println("please enter the seat number you wish to sit in");
    		seat = Keyboard.readInt();
    		while(seat > 4)
    		{
    			System.out.println("seat "+seat+ "is not in the smoking section");
    			System.out.println("please choose a seat from the smoking section 1 - 4");
    			System.out.println("please enter the seat number you wish to sit in");
    			seat = Keyboard.readInt();
    		}//close while
    		System.out.println("are you sure you want to book this seat row"+row+" seat number "+seat);
    		System.out.println("press (y) for yes and (n) for no");
    		ans1= Keyboard.readChar();
    		while(ans1 != 'y' && ans1 != 'n')//validating that correct letters have been entered
    		{
    			System.out.println("Please choose either (y) for yes or (n) for no");
    			ans1 = Keyboard.readChar();
    		}
    		if(ans1 == 'y')
    		{
    			System.out.println("you have succesfully booked a seat row "+row+" seat number "+seat);
    		}//close if
    		if(ans1 == 'n')
    		{
    			System.out.println("you will be returned to the main menu");
    		}//close if
    	}//close if
    	if(ans == 'b')
    	{
    		System.out.println("please enter the row you wish to sit in");
    		row = Keyboard.readChar();
    		while(row != 'a' && row != 'b' && row !='c' && row != 'd')//validating that correct letters have been entered
    		{
    			System.out.println("There are only four rows (a)(b)(c)(d) please choose one");
    			row = Keyboard.readChar();
    		}//close while
    		System.out.println("please enter the seat number you wish to sit in");
    		seat = Keyboard.readInt();
    		while(seat < 4 && seat > 8)
    		{
    			System.out.println("seat "+seat+ "is not in the non smoking section");
    			System.out.println("please choose a seat from the non smoking section, seat 5 to 8");
    			System.out.println("please enter the seat number you wish to sit in");
    			seat = Keyboard.readInt();
    		}//close while loop
    		System.out.println("are you sure you want to book this seat row "+row+" seatnumber "+seat);
    		System.out.println("press (y) for yes and (n) for no");
    		ans1= Keyboard.readChar();
    		while(ans1 != 'y' && ans1 != 'n')//validating that correct letters have been entered
    		{
    			System.out.println("Please choose either (y) for yes or (n) for no");
    			ans1 = Keyboard.readChar();
    		}
    		if(ans1 == 'y')
    		{
    			System.out.println("you have succesfully booked seat row "+row+" seat number "+seat);
    		}//close if
    		if(ans1 == 'n')
    		{
    			System.out.println("you will be returned to the main menu");
    		}//close if
    	}//close if
     
    	[COLOR="Red"]return (array1);[/COLOR]
    }//close bookseat method
     
    /**************************************************************************************************************************************/
     
    static char cancel(char array1[][])
    {
    }
     
    /**************************************************************************************************************************************/
     
    static char reset(char array1[][])
    {
    	int i, j;
    	char ans2;
    	System.out.println("are you sure you want to reset the table (y) or (n)");
    	ans2=Keyboard.readChar();
    	while(ans2 != 'y' && ans2 != 'n')//validating that correct letters have been entered
    	{
    		System.out.println("Please choose either (y) for yes or (n) for no");
    		ans2 = Keyboard.readChar();
    	}//close while loop
    	if(ans2 == 'y')
    	{
    		for(i = 0; i <array1.length; i++)
    		{
    			for(j = 0; j <array1.length; j++)
    			{//[COLOR="Blue"]Missing code[/COLOR]
    			}//close inner for
    		}//close outer for
    		System.out.println("All seats on the airplane have been reset to available");
    	}//close if
    	else
    	{
    		System.out.println("You will be returned to the main menu");
    	}//close else
    	[COLOR="red"]return (array1);[/COLOR]
    }//close reset method
     
    /*************************************************************************************************************************************/
     
    }//close class
    Last edited by The Lost Plot; April 15th, 2010 at 12:07 PM.


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

    Default Re: College Assignment please help

    ok so ive solved the above problem and found loads more error's, ive resolved these aswell but iim having problems outputting the array in the proper way it should be

    a b c d
    ___________
    - - - -
    - - - -
    - - - -
    - - - -
    - - - -
    - - - -
    - - - -
    - - - -

    i can output the array but array[0][0] is B,1 when it should be A,1

    Here's the method to output the array..
    	int i, j;
    	System.out.println("a  b  	 c  d");
    	System.out.println("_________________");
    	for(i=0; i<array1.length; i++)
    	{
    		if(i == 4)
    		{
    			System.out.println();
    			System.out.println();
    		}
    		for(j=0; j<array1[i].length; j++)
    		{
    			if(j %2 == 0)
    			{
    				System.out.print("	 ");
    			}
    			if(j %4 == 0)
    			{
    				System.out.println();
    			}
    			System.out.print(array1[i][j]+"  ");
     
    		}//close inner for
    	}//close outer for
    Last edited by The Lost Plot; April 17th, 2010 at 11:31 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: College Assignment please help

    Hi there, I have the same college assiment as youself, so if u dont mind im using your code, but I keep getting this error when i compile it G:\sp.java:150: incompatible types
    found : char[][]
    required: char
    return (array1);
    ^
    G:\sp.java:186: incompatible types
    found : char[][]
    required: char
    return (array1);
    ^
    2 errors..

    Could you please help me with this error, I would be very thankfull

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: College Assignment please help

    Alright I know I'm kind of late, but I just did this assignment yesterday, didn't find it very difficult.
    package AircraftSeats;
     
    import java.util.Scanner;
    import java.util.LinkedList;
     
    public class AircraftSeats {
     
    	public static LinkedList<String> allSeats = new LinkedList<String>();
     
    	public static void main(String args[]){
    		for(int i = 1; i <= 8; i++){
    			for(char a = 'a'; a <= 'd'; a++){
    				allSeats.add(String.format("%s%d", Character.toString(a).toUpperCase(), i));
    			}
    		}
    		boolean[] seats = new boolean[32];
    		for(int i = 0; i < 32; i++){
    			seats[i] = false;
    		}
    		Scanner input = new Scanner(System.in);
    		while(true){
    			System.out.println("(A):View Seating\n(B):Book A Seat\n(C):Cancel Booking\n(D):Reset All\n(E):Exit");
    			String choice = input.next();
    			if(choice.equalsIgnoreCase("a")){
    				printOutSeats(seats);
    			}else if(choice.equalsIgnoreCase("b")){
    				System.out.println("Enter seat name: letter#");
    				String seat = input.next();
    				bookSeat(seat, seats);
    			}else if(choice.equalsIgnoreCase("c")){
    				System.out.println("Enter seat name: letter#");
    				String seat = input.next();
    				cancelSeat(seat, seats);
    			}else if(choice.equalsIgnoreCase("d")){
    				resetSeats(seats);
    			}else if(choice.equalsIgnoreCase("e")){
    				System.exit(0);
    			}
    		}
    	}
     
    	public static void printOutSeats(boolean[] seats){
    		int n = 0;
    		System.out.println("  A B\tC D");
    		for(int i = 1; i < 9; i++){
    			System.out.print(String.format("%d ", i));
    			System.out.println(String.format("%s %s\t%s %s", !seats[n] ? "X" : "-", !seats[n+1] ? "X" : "-", !seats[n+2] ? "X" : "-", !seats[n+3] ? "X" : "-"));
    			n += 4;
    		}
    	}
     
    	public static void cancelSeat(String seat, boolean[] seats){
    		for(int i = 0; i < allSeats.size(); i++){
    			if(allSeats.get(i).equalsIgnoreCase(seat)){
    				seats[i] = false;
    				System.out.println("Successfully cancelled " + seat);
    				break;
    			}
    		}
    	}
     
    	public static void bookSeat(String seat, boolean[] seats){
    		for(int i = 0; i < allSeats.size(); i++){
    			if(allSeats.get(i).equalsIgnoreCase(seat)){
    				if(!seats[i]){
    					seats[i] = true;
    					System.out.println("Successfull");
    				}else{
    					System.out.println("Seat already taken");
    				}
    				break;
    			}
    		}
    	}
     
    	public static void resetSeats(boolean[] seats){
    		for(int i = 0; i < 32; i++){
    			seats[i] = false;
    		}
    	}
     
    }

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: College Assignment please help

    Although the fact that you 3 seem to be in the class is quite comical, Spoon-feeding is not.
    Please read forum policy regarding spoon-feeding SupportIsPower.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: College Assignment please help

    Ohh sorry, I'm very new to this forum and I thought "spoonfeeding" was okay. It won't happen again, as of this point.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up I hope i was solved ur problem just checkout with some patients

    ...edited by moderator
    Last edited by copeg; March 13th, 2012 at 09:27 AM.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: College Assignment please help

    @sriisreal, please read the forum rules, as well as the two posts above yours.

Similar Threads

  1. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  2. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  3. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM