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

Thread: Small airline company assignment

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Small airline company assignment

    Back again with another assignment...
    everytime i chose a seat is says that the seat is taken, couldn't figure out where the problem is at:
    public class Plane {
     
        char[][] chart;
        int ROWS = 7;
        int COLS = 5;
     
        Plane() {
     
            chart = new char[ROWS][COLS];
     
     
            for (int i = 0; i < ROWS; i++) {
                chart[i][0] = 'A';
                chart[i][1] = 'B';
                chart[i][2] = ' ';
                chart[i][3] = 'C';
                chart[i][4] = 'D';
            }
        }
     
        public boolean assignSeat(String str) {
            boolean result = false;
     
     
            str = str.toUpperCase();
     
            if (!okRow(str.charAt(0)) || !okSeat(str.charAt(1))) {
                System.out.println("Seat format is invalid\n");
            } else {
                result = markSeat(str.charAt(0), str.charAt(1));
     
                if (result) {
                    System.out.println("The seat is taken\n");
                } else {
                    System.out.println("The seat is busy, try another one\n");
                }
            }
            return result;
        }
     
        public String toString() {
            String result = "";
            for (int i = 0; i < ROWS; i++) {
     
                result += Integer.toString(i + 1);
                result += " ";
     
     
                for (int j = 0; j < COLS; j++) {
                    result += chart[i][j];
                }
     
                result += "\n";
            }
            return result;
        }
     
        private boolean okRow(char r) {
            if (r >= '1' && r <= '7') {
                return true;
            } else {
                return false;
            }
        }
     
        private boolean okSeat(char s) {
            if (s >= 'A' && s <= 'D') {
                return true;
            } else {
                return false;
            }
        }
     
        private boolean markSeat(char row, char seat) {
            int rowIndex, seatIndex;
            boolean result = false;
     
            rowIndex = row - '1';
            seatIndex = seat - 'A';
            if (seatIndex > 1) {
                seatIndex++;
            }
     
            if (chart[rowIndex][seatIndex] != 'X') {
                result = true;
                chart[rowIndex][seatIndex] = 'X';
            }
     
            return result;
        }
     
        public boolean isPlaneFull() {
            for (int i = 0; i < ROWS; i++) {
                for (int j = 0; j < COLS; j++) {
                    if (chart[i][j] != 'X' && chart[i][j] != ' ') {
                        return false;
                    }
                }
            }
            return true;
        }
    }

    and i'm using this to test it:
    import java.util.Scanner;
     
     
    public class PlaneTest {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           Scanner input = new Scanner(System.in);
           String cmd;
     
           System.out.println("Welcome, the plane is currently empty");
           Plane commuter1 = new Plane();
           System.out.println(commuter1.toString());
           System.out.println();
           System.out.println("Please enter a seat location to reserve and press enter to reserve row 1 seat A enter 1A followed by the enter key");
           System.out.println("To exit type Q or q");
     
           cmd = input.nextLine();
     
           while(!cmd.equals("Q") && !cmd.equals("q") && !commuter1.isPlaneFull()){
           		commuter1.assignSeat(cmd);
     
           		System.out.println();
           		System.out.println("Please enter a seat location to reserve and press enter to reserver row 1 seat A enter 1A followed by the enter key");
           		System.out.println("To exit type Q or q");
     
           		cmd = input.nextLine();
           }
     
           System.out.println("You are now exiting.");
           System.out.println(commuter1.toString());
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small airline company assignment

    it says that the seat is taken
    Try debugging the code by printing out the values of the variables that are used to make that decision. The print outs will show you what the computer is seeing and help you understand why the computer is doing what it is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Small airline company assignment

    Sorry for the late respond...
    by values you mean the result?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small airline company assignment

    the values of the variables
    Given this statement:
    x = 4;
    the value of the variable x is 4.

    Given this statement:
    private boolean markSeat(char row, char seat) {
    what are the values of the variables: row and seat?
    Last edited by Norm; October 8th, 2012 at 06:24 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Small airline company assignment

    So I tested the variables...
    the variables change based on what my input is
    if I say i wanna check 1A
    it shows row 1, seat A
    and thats how it supposed to be!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small airline company assignment

    OK, that part works. What about the rest of the code and variables?

    What is the method: markSeat() supposed to do? Does it do it correctly? When does it return true? When does it return false?

    What is the value of chart at rowIndex and seatIndex when markSeat() is called?
    Last edited by Norm; October 8th, 2012 at 07:09 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Setting up an small hosting company
    By Veyper in forum Totally Off Topic
    Replies: 5
    Last Post: November 8th, 2011, 08:01 AM
  2. Airline Management Simulation Game
    By aussiemcgr in forum The Cafe
    Replies: 23
    Last Post: July 8th, 2011, 03:59 PM
  3. problem with choice for airline reservation
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 10:30 PM
  4. Small Database Company Looking for Beta Checkers
    By dzrihen in forum JDBC & Databases
    Replies: 1
    Last Post: January 19th, 2011, 09:24 AM
  5. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM