Using Loops and Arrays
The Java Motel, with its ten comfortable rooms, sits in a quiet place off the main highway. Aside from a small, separate office, the motel is just one long row of ground-floor rooms.
Each room is easily accessible from the spacious front parking lot. Oddly enough, the motel’s rooms are numbered 0 through 9. I could say that the numbering is a fluke — something to do with the builder’s original design plan. But the truth is, starting with 0 makes the examples in this chapter easier to write.
You, as the Java Motel’s manager, store occupancy data in a file on your computer’s hard drive. The file has one entry for each room in the motel.
Deciding on a loop’s limit at runtime
On occasion, you may want a more succinct report than the one. “Don’t give me a long list of rooms,” you say. “Just give me the number of guests. To get such a report, you need a slightly smarter program. The program is in Listing, with runs of the program.
Reader, Meet Arrays; Arrays, Meet the Reader
A weary traveler steps up to the Java Motel’s front desk. “I’d like a room,” says the traveler. So, the desk clerk runs a report like the one. Noticing the first vacant room on the list, the clerk. “I’ll take it,” says the traveler.
It’s so hard to get good help these days. How many times have you told the clerk to fill the higher numbered rooms first? The lower-numbered rooms are older, and they are badly in need of repair. For example, indoor pool. (The pipes leak, so the carpet is soaking wet
has serious electrical problems (so, for that room, you always get paid in advance). Besides, is vacant, and you charge more for the higher numbered rooms. Here’s where a subtle change in presentation can make a big difference. You need a program that lists vacant rooms in reverse order.
That way, Room 8 catches the clerk’s eye before does. Think about strategies for a program that displays data in reverse. With the input, the program’s output should look like the display
Storing values in an array
After you’ve created an array, you can put values into the array’s components. For example, the guests in Room 6 are fed up with all those mint candies that you put on peoples’ beds. So, they check out and Room 6 becomes vacant. You should put the value 0 into the 6 components. You can do it with this assignment statement
On one weekday, business is awful. No one’s staying at the motel. But then you get a lucky break. A big bus pulls up to the motel. The side of the bus has a sign that says “Loners’ Convention.” Out of the bus come 25 people, each walking to the motel’s small office, none paying attention to the others who were on the bus.
Working with Arrays
Earlier in this chapter, a busload of loners showed up at your motel. When they finally left, you were glad to get rid of them, even if it meant having all your rooms empty for a while. But now, another bus pulls into the parking lot. This bus has a sign that says “Gregorian Club.”
Out of the bus come 50 people, each more gregarious than the next. Now everybody in your parking lot is clamoring to meet everyone else. While they meet and greet, they’re all frolicking toward the front desk, singing the club’s theme song. (Oh no! It’s the Gregorian Chant!)
Hey! The program in Listing is pretty big! It may be the biggest program so far in this book. But big doesn’t necessarily mean difficult. If each piece of the program makes sense, you can create each piece on its own, and then put all the pieces together. Voilà! The code is manageable.