Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Airline twodimensional boolean array  RSS feed

 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a topic that has ben asked many times but here comes a new question.
I have a boolean array that looks like this



I reserved one row for first class and another for economy class. This code works just as I want but my question is how can I loop through it so I don't need
all the if statements? I tried it many ways. I tried something like this but I cant get it to work.



Here I have the working code but I need to loop the boolean array so I don't need all the if statements. It would be much coding if I had 100 seats

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You need nested loops.

Something like:

Also, don't write if (condition == true) or if (condition == false).
Use if (condition) or if (!condition).
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you want this for a booking application? Why are you using booleans? you should have a Plane class and the Plane object should have Seat objects. Then you can mark the Seats with their class, whether they are booked, name of passenger, etc. You can also have first class or economy seats.
That code looks confused. I am not sure what it is supposed to do but it isn't doing anything I would like to do. You appear to be going through all the Seats until you find one marked false and you are changing it to true and saying you have booked seat 10. No, not even that.
You should start by writing down simply what you want to do, and getting it in to smaller and smaller parts. Then you can easily get it into code.

Have you been told that is a two‑dimensional array? Because there is no such thing as a 2D array; what you have is an array of arrays.
Never write == false or == true which are booth poor style and error‑prone if you write = by mistake. Write
if (!seats[1][2]) ...
for false with the bang sign ! and for true write
if (seats[1][2]) ...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the trap you fall into when you start with the nuts and bolts of a problem instead of the high-level concepts. I try to defer low-level design decisions for as long as possible. If you start with the code that you want to write, regardless of the underlying implementation, the code will often reveal the correct data structure to use.

Then, I slowly drill down to these methods and think about how I would like to write them, still trying to avoid implementation details if I can. I keep drilling down deeper, little by little, until I can no longer ignore the implementation details. At that point, it's often pretty obvious whether I'll need a list or array or whatever. Of course, the other aspect to this is writing unit tests and refactoring every step of the way.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, this seems like a good opportunity to try out the Functional Programming features in Java 8. In particular, filtering a collection of Seats on a Flight to match a given predicate seems like exactly the kind that can be solved very elegantly with FP. However, on the other hand, FP can be daunting even for experienced programmers. If you're not sure then stick with the Object-Oriented Programming style for now.
 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cleaned the code a bit. I tried so many ways to do it with the loops like this but I know there is somewhere I need to do a change. This is the nearest I get. First time i run this method it prints out that I have seat 00 and seat 01 in two rows (it loops two times without a break) second time it gives the third seat 02 and after that it is true.



Output is like this so I am near the solution;

Please choose 1 for Firstclass or 2 for Economyclass :
1
You have place number 00 in the Firstclass
You have place number 01 in the Firstclass
Please choose 1 for Firstclass or 2 for Economyclass :
1
You have place number 02 in the Firstclass
First class is full, do you want to book in the Economyclass? 1 for yes 2 for no

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tip: do not hardcode indices.
 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:One tip: do not hardcode indices.


I just started to learn Java and I really like it. What does it mean...indices?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Burefield wrote:I just started to learn Java and I really like it. What does it mean...indices?

The number you use inside the brackets, to indicate which element of an array or collection you want to access
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have a two dimensional array of booleans, indicating if a plane seat is booked or not

However you have added several assumptions to your codebase based on your if statements.

- Seats in the first row (seat[0]) are 'first class' seats, the remaining are 'economy'
- Seats are allocated in strict numerical order
- Seat [0][0] is place number 10. Seat[0][1] is place number 11...

This logic is embedded in your if statements, where you refer to explicit seats by their index in the array.
Which is the bit people have suggested you remove, and the bit you were originally asking about.

Campbell gave you the answer already: use an object to represent all of this information, so that you don't tie it to specific 'magic numbers' that only you know about.

So seats
- have a number
- may be first class or economy class
- may be booked or not.

Write a class "Seat" that can encapsulate that information.

Then have your 'two dimensional array' of Seat objects instead of booleans.
You will need to do some extra setup to put values into the array - i.e. create all the seats initially unbooked, but your code will be cleaner as a result, and you get rid of the 'if' statements as you referred to them.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
So seats
- have a number
- may be first class or economy class
- may be booked or not.

Write a class "Seat" that can encapsulate that information.

Agree


Then have your 'two dimensional array' of Seat objects instead of booleans.

I don't necessarily agree. Since the seat already has a number, why do you need a "two-dimensional" array? That will probably just lead you to confusion between Seat.number and its positional indices in the array of arrays. At best, there will be duplication between the two. At worst, there will be discrepancies and confusion.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you do need a data structure to keep the seats.
Agreed you could just have a list of them (that might be an appropriate data structure in some cases)

My understanding was that the 2d array was a representation of the layout of seats in the plane.

And the reason I made seat number an attribute is that there didn't appear to be any obvious correlation between the row/seat and the seatnumber except for its sequential order. I guess the calculation would be row * [number of seats in a row] + col + 10.



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's natural to look at an airplane seating chart and, as a programmer, see a "grid" that seems to easily translate to a two-dimensional array. IMO, an array doesn't quite fit the standard nomenclature for seats on a plane though. On all the flights I've ever been on, seat assignments are designated with a row number and a seat letter, with A on the port side, B to the right of A, C to the right of B, and so on to the starboard side. What's more, in first class, some of the letters are not used at all. For example, on a 787, you can have seats 1A, 1B, 1D, 1E, 1K, and 1L and no seats designated as 1C, 1F, 1G, 1H, 1I, or 1J. Other irregularities in the "grid" can be seen around exit rows, galleys, and the narrower portions of the tail section on some planes.

As an exercise, sure, I guess you can pretend that your fictitious plane's seating layout is perfectly symmetrical and seats are designated as row#/seat# but I still think you'd be falling into a trap of letting the visual aspect of the plane's seating layout (the view) influence your internal data structure choice too much. I would much rather separate the way the seats are arranged in a seating layout from the way I identify the seats, whether they are objects in memory or kept in a persistent store like a database or file.

If it were me, I would just have a List<Seat> or Map<Seat>. These seem like they would fit better with the kind of code I wanted to write.

The variables on lines 1-3 would be assigned values somehow, maybe after the user enters information in a form, or a batch of data is processed from an input file.

Lines 6-9 might be the code that books a particular seat for a passenger. Let's say the user opens a browser and goes to a "Seat Selection" page. The user selects a "seat" by clicking on an image that has seatIds associated with it. When a particular area of the seat layout image is clicked, a request with the corresponding seatID is submitted to the program which eventually executes these lines of code to book the seat. Nothing really screams "Use an array!" in this code when I start thinking about how to implement it. However, the seatId definitely looks like it could be a key to a Map or a unique attribute that I can use to find a particular item in a List.

This may be going above the "Beginner Java" level and into more of a design discussion. If all you want to do is learn how to work with arrays, I guess you could go with the Seat[][] structure for now and leave the vagaries of real-world airline booking for another day.
 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my problem. It was inside the inner for loop. I took away the break; and changed it to start();



Now it works fine. Now it's time for next chapter. Thank you for all the responses.
Output is now how I want, like this:

Please choose 1 for Firstclass or 2 for Economyclass :
1
You have place number 00 in the Firstclass
Please choose 1 for Firstclass or 2 for Economyclass :
1
You have place number 01 in the Firstclass
Please choose 1 for Firstclass or 2 for Economyclass :
1
You have place number 02 in the Firstclass
Please choose 1 for Firstclass or 2 for Economyclass :
1
First class is full, do you want to book in the Economyclass? 1 for yes 2 for no
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Burefield wrote:I solved my problem. . . .
No, you have simply kludged something which we all thought was a poor design.
 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
James Burefield wrote:I solved my problem. . . .
No, you have simply kludged something which we all thought was a poor design.


Ok, well I'm a beginner and did an assignment where I needed to create a boolean array and fill the seats 00-02 and 10-12 and fill the boolean array to true one by one.
First I did another way but teacher said it needed the boolean array. If you could point out where I did wrong with the boolean array I would be really happy and maybe
I could improve?
 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I think it's natural to look at an airplane seating chart and, as a programmer, see a "grid" that seems to easily translate to a two-dimensional array. IMO, an array doesn't quite fit the standard nomenclature for seats on a plane though. On all the flights I've ever been on, seat assignments are designated with a row number and a seat letter, with A on the port side, B to the right of A, C to the right of B, and so on to the starboard side. What's more, in first class, some of the letters are not used at all. For example, on a 787, you can have seats 1A, 1B, 1D, 1E, 1K, and 1L and no seats designated as 1C, 1F, 1G, 1H, 1I, or 1J. Other irregularities in the "grid" can be seen around exit rows, galleys, and the narrower portions of the tail section on some planes.

As an exercise, sure, I guess you can pretend that your fictitious plane's seating layout is perfectly symmetrical and seats are designated as row#/seat# but I still think you'd be falling into a trap of letting the visual aspect of the plane's seating layout (the view) influence your internal data structure choice too much. I would much rather separate the way the seats are arranged in a seating layout from the way I identify the seats, whether they are objects in memory or kept in a persistent store like a database or file.

If it were me, I would just have a List<Seat> or Map<Seat>. These seem like they would fit better with the kind of code I wanted to write.

The variables on lines 1-3 would be assigned values somehow, maybe after the user enters information in a form, or a batch of data is processed from an input file.

Lines 6-9 might be the code that books a particular seat for a passenger. Let's say the user opens a browser and goes to a "Seat Selection" page. The user selects a "seat" by clicking on an image that has seatIds associated with it. When a particular area of the seat layout image is clicked, a request with the corresponding seatID is submitted to the program which eventually executes these lines of code to book the seat. Nothing really screams "Use an array!" in this code when I start thinking about how to implement it. However, the seatId definitely looks like it could be a key to a Map or a unique attribute that I can use to find a particular item in a List.

This may be going above the "Beginner Java" level and into more of a design discussion. If all you want to do is learn how to work with arrays, I guess you could go with the Seat[][] structure for now and leave the vagaries of real-world airline booking for another day.


This is just an exercise about arrays
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Burefield wrote:I solved my problem.

That's fine, James. As a beginner you have to start somewhere, even if it's on shaky ground. Once you get your ”Java legs" under you though, you really should think more about what your program code means to other people reading it. Right now you have code that doesn't quite make sense to those of us who are more experienced. It's like you built something with four wheels and a pedal and called it a car and we're looking at it and thinking "If that's a car then I'm the king of England." Like I said, you have to start somewhere, so just keep trying to make your programs better. Good luck.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Junilu has explained it better than I would have.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Burefield wrote:This is just an exercise about arrays

Yes, I realize it is. As far scenarios go though, however, I think your teacher could have given you a better one in which to use an array of booleans. For reasons already stated, the airplane seating chart is just a not good fit. A better fit probably would be something like a game like Battleship or Connect Four. I could see you using an array of boolean with those problems.

Edit: On second thought maybe not so much for those suggestions either. The classic problem where boolean arrays are used is the Sieve of Eratosthenes to find prime numbers. Another that just came to mind is Conway's Game of Life simulation.
 
James Burefield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
James Burefield wrote:This is just an exercise about arrays

Yes, I realize it is. As far scenarios go though, however, I think your teacher could have given you a better one in which to use an array of booleans. For reasons already stated, the airplane seating chart is just a not good fit. A better fit probably would be something like a game like Battleship or Connect Four. I could see you using an array of boolean with those problems.

Edit: On second thought maybe not so much for those suggestions either. The classic problem where boolean arrays are used is the Sieve of Eratosthenes to find prime numbers. Another that just came to mind is Conway's Game of Life simulation.


Nice, I'll check those out. Thank's again
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is particularly worth finding out about the Sieve of Eratosthenes which is a standard technique rather than simply being a teaching example. It is also quite simple and you can write the whole application in about twelve lines.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!