0

ok no worries. So, I compiled and run everything. The good news is that I managed to remove all the compiling errors and the program even runs! The bad news is that it doest't run the way it should, I must have made some logic error somewhere, and I think I know where, in the switch satement:

switch( classRequested ){
                    case 1:
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
                        userChoice = String.format( "\nYou have selected %s", action );                     
                        userInput = userChoice.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, userChoice );
                        if( userInput == 'y' ){
                            seatingPlan.assignSeat( SECOND_CLASS_SELECTION );
                        }
                        else{
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;
                        }
                        //break;

                    case 2: 
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
                        userChoice = String.format( "\nYou have selected %s ", action );
                        userInput = userChoice.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, userChoice );
                        if( userInput == 'y' ){
                            seatingPlan.assignSeat( FIRST_CLASS_SELECTION );
                        }
                        else{
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;
                        }

Right, userChoice in userChoice = String.format( "\nYou have selected %s", action ); is declared as a string and userInput = userChoice.charAt(0); (userInput is a char) simply convert a string to a char. Maybe I am not doing it correctly but what happens if you run the program is that if the first (or second for that matter) class is full and the user answers 'y' to "Do you want to have a seat in second/first class? Type y for yes and n for no" this if( userInput == 'y' ) - for whatever reason - is always false resulting in the else to be executed. I found out this because of this line JOptionPane.showMessageDialog( null, userChoice ); that correctly returns 'y' or 'n'. But, the moment I change that to this JOptionPane.showMessageDialog( null, userInput ); as it should be, it returns nothing so if( userInput == 'y' ) will never be true and everything falls apart...why is that?
I include the files below (I don't understand why on this forum I can't attach any file in a reply...!):

//ex 7.19 p 333 deitel and deitel
//Seats.java
public class Seats{
    private final int NUMBER_OF_SEATS = 10;
    private boolean[] seats;


    //constructor
    public Seats(){
        //initializing array
        seats = new boolean[ NUMBER_OF_SEATS ];
        //set values to false - doesn't it do automatically?
        for( int counter = 0; counter < seats.length; counter++ ){
            seats[ counter ] = false;
        }//end of loop      
    }//end of constructor

    public int assignSeat( int type){
        int seatPlace = -1; //default seatPlace value
        int firstI = 0;  // firstIndex
        int lastI = 4;
          if ( type == 2 ){
            firstI += 5;//second index
            lastI += 5;
          }  
          for ( int i = firstI; i <= lastI; i++ ){
            if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
              seats[i] = true;
              if ( type == 1 ){
                System.out.printf( "You will seat in firs class on seat number %d\n" , ( i + 1 ) );
              }
              else if ( type == 2 ){
                System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
              }
              return seatPlace = i; // flag sucessfully seat assignment
            }
          }
          // if the method reaches this point, it means no seats were still available,
          return seatPlace;
    }//end of assignSeats
}//end of class

and the other one:

//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
    //constants
    static final int FIRST_CLASS_SELECTION = 1;//first class
    static final int SECOND_CLASS_SELECTION = 2;//second class
    public static void main( String[] args ){

        Seats seatingPlan = new Seats();
        //int firstCounter = 0;//keep the seat count
        //int secondCounter = 0;


        //display message
        String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
        //flag determines whether seats have been assigned or not
        int seatAssigned = 0;
        //create message            
        String message = String.format( "Thank you. You have chosen %s class.", action );
        //display message
        JOptionPane.showMessageDialog( null, message ); 
        //convert string to int         
        int classRequested = Integer.parseInt( action );
        while( classRequested != 0){
            //call function with class chose by user
            seatAssigned = seatingPlan.assignSeat( classRequested );
/*          if( seatAssigned != -1 ){
                switch( classRequested ){
                    case 1:
                        firstCounter++;
                        break;
                    case 2:
                        secondCounter++;
                        break;                      
                }               

            } */
            if( seatAssigned == -1 ){
                //contains y (assign seat in the other class )or n (dont' assign seat in the other class)
                String userChoice;
                //to convert user input from string to char
                char userInput;
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
                        userChoice = String.format( "\nYou have selected %s", action );                     
                        userInput = userChoice.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, userChoice );
                        if( userInput == 'y' ){
                            seatingPlan.assignSeat( SECOND_CLASS_SELECTION );
                        }
                        else{
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;
                        }
                        //break;

                    case 2: 
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
                        userChoice = String.format( "\nYou have selected %s ", action );
                        userInput = userChoice.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, userChoice );
                        if( userInput == 'y' ){
                            seatingPlan.assignSeat( FIRST_CLASS_SELECTION );
                        }
                        else{
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;
                        }
                        //break;                        
                }//end of switch
                break;
                /*if( ( firstCounter == 5 ) && ( secondCounter == 5) ){//the flight is full
                    System.out.println( "\nSorry the flight is full, the next one leaves in 3hrs\n" );
                    break;//leaves the while loop
                }
                */              
            }//end of if( seatAssigned == -1 )

            //ask for input again
            action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
            //create message            
            message = String.format( "Thank you. You have chosen %s class.", action );
            //display message
            JOptionPane.showMessageDialog( null, message ); 
            classRequested = Integer.parseInt( action );            
        }//end of while
        System.out.println( " The next plane leaves in 3 hrs " );

    }//end of main
}//end of class
0

userChoice is set to the prompt "\nYou have selected etc, then you take the first char of that ('\n'). The user's input is in the variable action, but you don't use that. A good example of why names matter?

0

oh I see, I am such a donkey!!
ok I have amended that and almost everything seems to be working ok. I can hear you says "yay!" but no, I had the insane idea og handling also the situation when the plane is full, so I added some more code (and modified the existing one too) so that, in theory at least, when the plane is full a message saying "Sorry the flight is full, the next one leaves in 3hrs" prints out. Well, good idea isn't it>? Except that it is not working the way it should. Here's what I have done:
1)I have added 2 variables (you must have seen these in the code they were commented out):

        int firstCounter = 0;//keep the seat count
        int secondCounter = 0;

Then in the while loop at the top straight after the function call I have a switch statement:

seatAssigned = seatingPlan.assignSeat( classRequested );
            if( seatAssigned != -1 ){
                switch( classRequested ){
                    case 1:
                        firstCounter++;
                        break;
                    case 2:
                        secondCounter++;
                        break;                      
                }               

            }

In the switch( classRequested ){case 1: if the user selects 'y' I increase the secondClass counter because I call the function with a parameter of 2 and finally an if statement determines whether the plane is full or not:

if( ( firstCounter == 5 ) && ( secondCounter == 5) ){//the flight is full
                    System.out.println( "\nSorry the flight is full, the next one leaves in 3hrs\n" );
                    break;//leaves the while loop
                }

So here's the amended code:

    //ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
    //constants
    static final int FIRST_CLASS_SELECTION = 1;//first class
    static final int SECOND_CLASS_SELECTION = 2;//second class
    public static void main( String[] args ){

        Seats seatingPlan = new Seats();
        int planeLeave = 0; //flag if users don't want to book more seats
        int firstCounter = 0;//keep the seat count
        int secondCounter = 0;


        //display message
        String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
        //flag determines whether seats have been assigned or not
        int seatAssigned = 0;
        //create message            
        String message = String.format( "Thank you. You have chosen %s class.", action );
        //display message
        JOptionPane.showMessageDialog( null, message ); 
        //convert string to int         
        int classRequested = Integer.parseInt( action );
        while( classRequested != 0){
            //call function with class chose by user
            seatAssigned = seatingPlan.assignSeat( classRequested );
            if( seatAssigned != -1 ){
                switch( classRequested ){
                    case 1:
                        firstCounter++;
                        break;
                    case 2:
                        secondCounter++;
                        break;                      
                }               

            }
            if( seatAssigned == -1 ){
                //contains y (assign seat in the other class )or n (dont' assign seat in the other class)
                //String userChoice;
                //to convert user input from string to char
                char userInput;
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
                        message = String.format( "\nYou have selected %s", action );                        
                        userInput = action.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, message );
                        //System.out.println("\nThe value of userInput is " + userInput);
                        if( userInput == 'y' ){
                            secondCounter++;
                            seatingPlan.assignSeat( SECOND_CLASS_SELECTION );

                        }
                        else{
                            planeLeave = 1;
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;//leave switch
                        }
                        break;//leave switch

                    case 2: 
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
                        message = String.format( "\nYou have selected %s ", action );
                        userInput = action.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, message );

                        if( userInput == 'y' ){
                            firstCounter++;
                            seatingPlan.assignSeat( FIRST_CLASS_SELECTION );

                        }
                        else{
                            planeLeave = 1;
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;//leave switch
                        }
                        break;//leave switch                        
                }//end of switch
                if( planeLeave == 1 ){//if uses don't want more seats leave the application
                    break;//leave application
                }

                if( ( firstCounter == 5 ) && ( secondCounter == 5) ){//the flight is full
                    System.out.println( "\nSorry the flight is full, the next one leaves in 3hrs\n" );
                    break;//leaves the while loop
                }

            }//end of if( seatAssigned == -1 )

            //ask for input again
            action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
            //create message            
            message = String.format( "Thank you. You have chosen %s class.", action );
            //display message
            JOptionPane.showMessageDialog( null, message ); 
            classRequested = Integer.parseInt( action );    
        }//end of while
        System.out.println( " The next plane leaves in 3 hrs " );

    }//end of main
}//end of class

It compiles ok, but it looks like the counters don't increment the way they should...
Needless to say I run a few tests before posting but I don't seem to be able to find the error...maybe an extra pair of eyes can spot it?
thanks

0

Rather than add more variables so SeatsTest can double-guess what's in Seats, it would be better encapsulation to add one public method to Seats like
public boolean planeIsFull() {...
then in SeatsTest you just call that method whenever you want to know if the plane is full.
The implementation of that method now doesn't matter a lot (you can always change it), but I would just scan the array of seats - as soon as I see a vacant one I can return false, if I finish looping thru all the seats I return true.

0

ok will try that. I am just thinking, if the function returns true (all seats allocated) or false (some free) what would I do with those values returned? I mean with the true value it's pretty straightforward in that, if the value is true then I will end the program, but if it returns false, what do I do with that value? DO I have to use it for something?

0

gosh, this exercise is a nightmare...I have included the function but the program seems to ignore it completely for whatever reason. Basically in Seats.java the function returns true to the function caller if all the seats are taken and a few ifs statements handle this till another if terminates the application. I don't use the false value returned by the function:

...
public boolean planeIsFull(){
        int counter = 0;//holding the number of filled seats
        for( int i = 0; i < seats.length; i++ ){         
            if( seats[ i ] == true ){//if all the seats are taken
                counter++;//increase counter
            }

        }//end of loop

        if( counter == NUMBER_OF_SEATS ){//if counter == 10 then the plane is full
            return true;
        }
        else{
            return false;
        }
    }//end of planeIsFull
}//end of class

and here's the SeatsTest.java

    //ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
    //constants
    static final int FIRST_CLASS_SELECTION = 1;//first class
    static final int SECOND_CLASS_SELECTION = 2;//second class
    public static void main( String[] args ){

        Seats seatingPlan = new Seats();
        int planeLeave = 0; //flag if users don't want to book more seats
        //int firstCounter = 0;//keep the seat count
        //int secondCounter = 0;


        //display message
        String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
        //flag determines whether seats have been assigned or not
        int seatAssigned = 0;
        //create message            
        String message = String.format( "Thank you. You have chosen %s class.", action );
        //display message
        JOptionPane.showMessageDialog( null, message ); 
        //convert string to int         
        int classRequested = Integer.parseInt( action );
        while( classRequested != 0){
            //call function with class chose by user
            seatAssigned = seatingPlan.assignSeat( classRequested );
            boolean seatsStatus = seatingPlan.planeIsFull();//check the status of the seat
            if( seatsStatus ){
                break;//terminate the application
            }
            if( seatAssigned == -1 ){
                //contains y (assign seat in the other class )or n (dont' assign seat in the other class)
                //String userChoice;
                //to convert user input from string to char
                char userInput;
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
                        message = String.format( "\nYou have selected %s", action );                        
                        userInput = action.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, message );
                        //System.out.println("\nThe value of userInput is " + userInput);
                        if( userInput == 'y' ){
                            //secondCounter++;
                            seatingPlan.assignSeat( SECOND_CLASS_SELECTION );

                            seatsStatus = seatingPlan.planeIsFull();//check the status of the seat
                            if( seatsStatus ){
                                break;//leave switch
                            }                           
                        }
                        else{
                            planeLeave = 1;
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;//leave switch
                        }
                        break;//leave switch                        
                    case 2: 
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
                        message = String.format( "\nYou have selected %s ", action );
                        userInput = action.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, message );                     
                        if( userInput == 'y' ){
                            //firstCounter++;
                            seatingPlan.assignSeat( FIRST_CLASS_SELECTION );                            
                            seatsStatus = seatingPlan.planeIsFull();//check the status of the seat
                            if( seatsStatus ){
                                break;//leave switch
                            }                           
                        }
                        else{
                            planeLeave = 1;
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;//leave switch
                        }
                        break;//leave switch                        
                }//end of switch
                if( ( planeLeave == 1 ) ){//if uses don't want more seats leave the application
                    break;//leave application
                }
                else if( seatsStatus ){
                    System.out.println( "\nSorry but the flight is full, the next one leaves in 3 hrs" );
                    break;//leave application
                }                               
            }//end of if( seatAssigned == -1 )          
            //ask for input again
            action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
            //create message            
            message = String.format( "Thank you. You have chosen %s class.", action );
            //display message
            JOptionPane.showMessageDialog( null, message ); 
            classRequested = Integer.parseInt( action );    
        }//end of while
        System.out.println( " The next plane leaves in 3 hrs " );

    }//end of main
}//end of class

What happens is the program terminates ok but the message Sorry but the flight is full, the next one leaves in 3 hrs

0

ah sorry, I forgot to print the message when the function is first called, plus a few more changes, now it works!!! aaaaaaaaaaaahhh! I can't believe it, it wooooorksss!!
Right, big big thanks to all you guys who've contributed to this thread, thanks a lot for your help and patience!
I have learned a lot from this exercise, and I will make absolutely sure I will come up with a very good pseudocode before I start coding and not the other way around (which is what I have essentially done this time).
Here is the final version of the program in case anybody wants to use it (I doubt it) or use it like a starting point (a bit more likely!)
I suppose I have looked at it so many times that now I miss the obvious!

//ex 7.19 p 333 deitel and deitel
//Seats.java
public class Seats{
    private final int NUMBER_OF_SEATS = 10;
    private boolean[] seats;


    //constructor
    public Seats(){
        //initializing array
        seats = new boolean[ NUMBER_OF_SEATS ];
        //set values to false - doesn't it do automatically?
        for( int counter = 0; counter < seats.length; counter++ ){
            seats[ counter ] = false;
        }//end of loop      
    }//end of constructor

    public int assignSeat( int type){
        int seatPlace = -1; //default seatPlace value
        int firstI = 0;  // firstIndex
        int lastI = 4;
          if ( type == 2 ){
            firstI += 5;//second index
            lastI += 5;
          }  
          for ( int i = firstI; i <= lastI; i++ ){
            if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
              seats[i] = true;
              if ( type == 1 ){
                System.out.printf( "You will seat in firs class on seat number %d\n" , ( i + 1 ) );
              }
              else if ( type == 2 ){
                System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
              }
              return seatPlace = i; // flag sucessfully seat assignment
            }
          }
          // if the method reaches this point, it means no seats were still available,
          return seatPlace;//returns -1
    }//end of assignSeats
    public boolean planeIsFull(){
        int counter = 0;//holding the number of filled seats
        for( int i = 0; i < seats.length; i++ ){         
            if( seats[ i ] == true ){//if all the seats are taken
                counter++;//increase counter
            }

        }//end of loop

        if( counter == NUMBER_OF_SEATS ){//if counter == 10 then the plane is full
            return true;
        }
        else{
            return false;
        }
    }//end of planeIsFull
}//end of class

and

    //ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
    //constants
    static final int FIRST_CLASS_SELECTION = 1;//first class
    static final int SECOND_CLASS_SELECTION = 2;//second class
    public static void main( String[] args ){

        Seats seatingPlan = new Seats();
        int planeLeave = 0; //flag if users don't want to book more seats
        //int firstCounter = 0;//keep the seat count
        //int secondCounter = 0;


        //display message
        String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
        //flag determines whether seats have been assigned or not
        int seatAssigned = 0;
        //create message            
        String message = String.format( "Thank you. You have chosen %s class.", action );
        //display message
        JOptionPane.showMessageDialog( null, message ); 
        //convert string to int         
        int classRequested = Integer.parseInt( action );
        while( classRequested != 0){
            //call function with class chose by user
            seatAssigned = seatingPlan.assignSeat( classRequested );
            boolean seatsStatus = seatingPlan.planeIsFull();//check the status of the seat
            if( seatsStatus ){
                System.out.println( "\nSorry but the flight is full. " );
                break;//terminate the application
            }
            if( seatAssigned == -1 ){
                //contains y (assign seat in the other class )or n (dont' assign seat in the other class)
                //String userChoice;
                //to convert user input from string to char
                char userInput;
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
                        message = String.format( "\nYou have selected %s", action );                        
                        userInput = action.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, message );
                        //System.out.println("\nThe value of userInput is " + userInput);
                        if( userInput == 'y' ){
                            //secondCounter++;
                            seatingPlan.assignSeat( SECOND_CLASS_SELECTION );

                            seatsStatus = seatingPlan.planeIsFull();//check the status of the seat
                            if( seatsStatus ){
                                break;//leave switch
                            }                           
                        }
                        else{
                            planeLeave = 1;
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;//leave switch
                        }
                        break;//leave switch                        
                    case 2: 
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
                        message = String.format( "\nYou have selected %s ", action );
                        userInput = action.charAt(0);//convert user input to char
                        JOptionPane.showMessageDialog( null, message );                     
                        if( userInput == 'y' ){
                            //firstCounter++;
                            seatingPlan.assignSeat( FIRST_CLASS_SELECTION );                            
                            seatsStatus = seatingPlan.planeIsFull();//check the status of the seat
                            if( seatsStatus ){
                                break;//leave switch
                            }                           
                        }
                        else{
                            planeLeave = 1;
                            //System.out.println( " The next plane leaves in 3 hrs " );
                            break;//leave switch
                        }
                        break;//leave switch                        
                }//end of switch
                if( ( planeLeave == 1 ) ){//if uses don't want more seats leave the application
                    break;//leave application
                }
                else if( seatsStatus ){
                    System.out.println( "\nSorry but the flight is full. " );
                    break;//leave application
                }                               
            }//end of if( seatAssigned == -1 )          
            //ask for input again
            action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
            //create message            
            message = String.format( "Thank you. You have chosen %s class.", action );
            //display message
            JOptionPane.showMessageDialog( null, message ); 
            classRequested = Integer.parseInt( action );    
        }//end of while
        System.out.println( "The next plane leaves in 3 hrs." );

    }//end of main
}//end of class
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.