question about an airline reservation system
what does this statement means?
seats[ smoking++ ] = 1;
what does this statement means?
seats[ smoking++ ] = 1;
my question is why do we use these kind of statements? like to increment makes sense by why using an assignment statement?
seats[smoking++] = 1; i know what smoking++ does inside the array but why do we use an assignment statement = 1? that is where i am confused.
if i dont use any assignment statement and do this seats[smoking++] compiler gives me an error which is genuine
> if smoking is 0 and seats[0] then seats[0+1] = 1; ?
> correct
No. Run this example:
int[] array = {1,1,1,1,1};
int index = 0;
System.out.println(index+", "+java.util.Arrays.toString(array));
array[index++] = 666;
System.out.println(index+", "+java.util.Arrays.toString(array));
As you can see, the value at index is first assigned the value 666 and after that, the index get incremented with one.
X++ is called post-increment and
++X is called pre-increment.
> my question is why do we use these kind of
> statements? like to increment makes sense by why
> using an assignment statement?
> seats[smoking++] = 1; i know what smoking++ does
> inside the array but why do we use an assignment
> statement = 1? that is where i am confused.
>
> if i dont use any assignment statement and do this
> seats[smoking++] compiler gives me an error which is
> genuine
I don't understand you.
now i understand . so if i assign another alue array[index++] = 777 it will increment the index and place the value at the appropriate index. Thanks
> now i understand . so if i assign another alue
> array[index++] = 777 it will increment the index and
> place the value at the appropriate index. Thanks
But remember:
when doing array[index++] = 777 first the assignment is being performed, then the increment of index;
when doing array[++index] = 777 first the increment of index is being performed, then the assignment.
> now i understand . so if i assign another alue
> array[index++] = 777 it will increment the index and
> place the value at the appropriate index. Thanks
that depends on what the appropriate index is...
> now i understand . so if i assign another alue
> array[index++] = 777 it will increment the index and
> place the value at the appropriate index. Thanks
It will stick 777 in the element at the current value of index, and then it will increment index.
By the way, next time please give a little more thought to your subject line. Your question really had nothing to do with and airline reservation system. It was about the post-increment operator.
Also, seats[x] = 1; looks suspicious.
Why would an array of ints be called "seats"? Does a seat have a value of 0 or 1? That's almost certainly a bad idea.
Right, it's called abstraction. If you have an array of ints for seats, you aren't being very object-oriented.
Maybe you can have a Seat class with a variable that indicates whether or not it's been filled:
public class Seat
{
private boolean reserved;
public boolean isReserved() { return reserved; }
public void setReserved(boolean reserved) { this.reserved = reserved; }
}
Now you can iterate through a List of Seats and ask each one if it's been reserved. Nice, self-documenting abstraction.
It's a common mistake for people who don't understand objects to program in too low a level. Everything's a primitive; the most sophisticated data structure is an array.
%
> Maybe you can have a Seat class with a variable that
> indicates whether or not it's been filled:
I might even go with a two-valued enum: RESERVED, AVAILABLE.
* It avoids confusion as to whether true means "yes, it's reserved" or "yes it's available". (Though always calling reserved() and naming your locals that use it reserved will alleviate that too.)
* If we later find we need another state, we simply add it to the enum an add the appropriate handling elsewhere. No need to change the type of everything that depends on it.
> > Maybe you can have a Seat class with a variable
> that
> > indicates whether or not it's been filled:
>
> I might even go with a two-valued enum: RESERVED,
> AVAILABLE.
Could be worth doing if a seat can be unreserved and still unavailable.In that case I might add another state to my enum.
>
> * It avoids confusion as to whether true means "yes,
> it's reserved" or "yes it's available". (Though
> always calling reserved() and naming your locals that
> use it reserved will alleviate that too.)
Yes, agreed. That's what I didn't like about 0 and 1. What do those mean?
>
> * If we later find we need another state, we simply
> add it to the enum an add the appropriate handling
> elsewhere. No need to change the type of everything
> that depends on it.
I like it, Jeff.
%
It's a bit of a toss-up. An enum might be overengineering here--a case of small boy with new language feature. I'm mainly just tossing it out there as an option, along with a couple of potential reasons why it might make sense. It's up to the OP to determine if any of that applies to him, or if a simple "reserved" boolean is sufficient.
If nothing else, I hope we've planted the seeds of examining pros and cons of different approaches
> It's a bit of a toss-up. An enum might be
> overengineering here--a case of small boy with new
> language feature. I'm mainly just tossing it out
> there as an option, along with a couple of potential
> reasons why it might make sense. It's up to the OP to
> determine if any of that applies to him, or if a
> simple "reserved" boolean is sufficient.
>
> If nothing else, I hope we've planted the seeds of
> examining pros and cons of different approaches
"Ok, so how do I determine if it applies to me?" :)
> It's a bit of a toss-up. An enum might be
> overengineering here--a case of small boy with new
> language feature.
Not if there really is a meaningful state to add.
Note the word: "if". It would depend on the requirements.
> I'm mainly just tossing it out
> there as an option, along with a couple of potential
> reasons why it might make sense. It's up to the OP to
> determine if any of that applies to him, or if a
> simple "reserved" boolean is sufficient.
>
> If nothing else, I hope we've planted the seeds of
> examining pros and cons of different approaches
Good attempt, anyway.
%
thanks for the prompt replies. well i was studying core java from deitel. I prefer to use arraylist and list or other data structures. I am lacking in array alot. i dont know why. I've read 2 books already and i am good in programming but bad when anyone ask me to write a program using array. I can do the same thing with an arraylist or list.
So i started reading more books and more assignments on arrays. until i found this code example which is like this
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlaneExample extends JApplet
implements ActionListener {
JTextField input;
JLabel prompt1, prompt2;
JButton yesButton, noButton;
int section, seats[], smoking;
int nonsmoking, people;
public void init()
{
input = new JTextField( 4 );
input.addActionListener( this );
prompt1 = new JLabel( "Please type 1 for smoking" );
prompt2 = new JLabel( "Please type 2 for nonsmoking" );
yesButton = new JButton( "Yes" );
noButton = new JButton( "No" );
yesButton.addActionListener( this );
noButton.addActionListener( this );
// The enabled method has not been introduced up
// to this point in the book. It is used here to
// disable the buttons by "graying" them.
yesButton.setEnabled( false );
noButton.setEnabled( false );
seats = new int[ 11 ];
smoking = 6;
nonsmoking = 1;
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.add( prompt1 );
c.add( prompt2 );
c.add( input );
c.add( yesButton );
c.add( noButton );
}
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == input && people <= 10 ) {
section = Integer.parseInt( input.getText() );
String s = "";
if ( section == 1 ) {
if ( smoking <= 10 ) {
s = "Smoking. Seat #" + smoking;
seats[ smoking++ ] = 1; //this is where i dont know what was going on
people++;
}
else if ( smoking > 10 && nonsmoking <= 5 ) {
// enable buttons
yesButton.setEnabled( true );
noButton.setEnabled( true );
s = "Smoking is full. Non-smoking?";
}
else
s = "Next flight leaves in 3 hours.";
}
else if ( section == 2 ) {
if ( seats[ nonsmoking ] == 0 && nonsmoking <= 5 ) {
s = "Nonsmoking. Seat #" + nonsmoking;
seats[ nonsmoking++ ] = 1; // same here
people++;
}
else if ( nonsmoking > 5 && smoking <= 10 ) {
// enable buttons
yesButton.setEnabled( true );
noButton.setEnabled( true );
s = "Nonsmoking is full. Smoking?";
}
else
s = "Next flight leaves in 3 hours.";
}
else
s = "Invalid input.";
showStatus( s );
}
else if ( e.getSource() == yesButton ) {
if ( section == 1 ) {
showStatus( "Your seat assignment is " + nonsmoking );
seats[ nonsmoking++ ] = 1;
}
else { // section is 2
showStatus( "Your seat assignment is " + smoking );
seats[ smoking++ ] = 1;
}
people++;
noButton.setEnabled( false );
yesButton.setEnabled( false );
}
else if ( e.getSource() == noButton ) {
showStatus( "Next flight leaves in 3 hours." );
noButton.setEnabled( false );
yesButton.setEnabled( false );
}
}
}
seats[ nonsmoking++ ] = 1;
seats[smoking++ ] = 1;
these were the 2 part which i cannot understand may be because of my lack of practice in arrays.
> thanks for the prompt replies. well i was studying
> core java from deitel. I prefer to use arraylist and
> list or other data structures.
Nobody said not to.
> I am lacking in array
> alot.
Then you should practice with them.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
> So i started reading more books and more assignments
> on arrays. until i found this code example which is
> like this
> seats[ nonsmoking++ ] = 1;
> seats[smoking++ ] = 1;
>
> these were the 2 part which i cannot understand may
> be because of my lack of practice in arrays.
Okay, so do you still have a question?