Chapter 3

Test Your Thinking

As we said at the beginning of the chapter, it is our jobs as programmers to represent the real world as structured data. Once we can accomplish this, then our battle is almost won. If we have well-structured data, then processing it becomes much easier. Look at how computers have jumped into the everyday lives of "regular" people now what we have been able to digitize pictures, music, video, and text. The World Wide Web has been able to grow extremely quickly because the structure of the data (the web pages) is well defined and standardized.

It's a simple task to represent someone's age as an integer, or a price as a floating-point number. However, more complex objects require more complex organization of the data.

1) Imagine that you are in charge of keeping score at a golf tournament. Assume that there are 18 holes in the course, and that the players will play for three days. The number of players won't be known until the starting day since it's an open tourney. How would you structure your data to keep track of each player's score?

Answer:

There are better ways of doing this (see chapter 10), but given what has been covered so far, there is one possible answer:

If each player only played one game of golf, then all we would need to do it create an array of scores for each hole, like this:

int scores[] = new int[18];

However, since we have three games to score, we can make a two-dimensional array.

int playerScores[][] = new int[3][18];

Technically, this is an array of int arrays, if that's easier to visualize. For example, a golfer's score for hole #13 on day #2 would be found in

playerScores[1][12];

Remember that the indices will go from 0 to 2 and 0 to 17, and not 1 to 3 and 1 to 18.

In general, a player's score for a particular day and hole would be:

playerScores[day - 1][hole - 1];

We'll need a "playerScores" variable for each player in the tournament. Each would have their own name, of course, but they would all be arrays of arrays.

2) You did so well with your job at the golf course that you now work in a stadium selling tickets. Your stadium houses football games and rock concerts and the prices of the tickets depend on the seat location and the event. Your job is to keep track of the ticket prices for each seat for each event and to keep track of which seats have been sold.

Answer:

For this problem, you can use two arrays, one to keep track of the prices, and another to keep track of its sale status.

Let's say there are 20000 seats in this stadium. Here is one possible answer:

int numberOfSeats = 20000;

float prices[numberOfSeats]:

boolean sold[numberOfSeats];

You can set the prices, as follows:

prices[100] = 20.50;

// This sets the price of seat #100 to $20.50.

In general, to set the price of a seat:

prices[seatNumber] = price;

Likewise, if seat #100 were sold, then you would record it this way:

sold[100] = true;

Using these two variables, you can keep track of the status and price of each seat.

3) Your success at the stadium has catapulted you to the Presidency of the United States. Congratulations! As President, it is your duty to write computer programs (the Constitution was amended by President Scott Adams before you were elected). You need to keep track of how each member of Congress voted on various issues and you need to know how many Republicans and Democrats are in each house of Congress.

Answer:

As always, there are many ways of approaching a problem. This is just one possible solution.

Let's start off by keeping track of the number of Democrats and Republicans in both houses of Congress. In fact, we can keep track of all of the different parties (Reform Party, Libertarian Party, Green Party, Peace and Freedom, and the any others).

First, we assign each member of congress a number. We'll start with the Senate.

int BROWER_ERIC = 0;

int CHU_KEVIN = 1;

int LOY_MARC = 2;

int MOORE_RALPH = 3;

int TAUB_MARK = 4;

// and so on ...

We can now build arrays using these values as indices. A simple example would be their names:

String senatorNames[] = {

"Eric Brower",

"Kevin Chu",

"Marc Loy",

"Ralph Moore",

"Mark Taub"

// and so on ...

}

Thus, the following code:

System.out.println(names[CHU_KEVIN]);

would print the following:

Kevin Chu

In the actual Senate, there would be 100 senators, and therefore 100 names numbered 0 to 99.

Similarly, we can assign each political party a number:

int DEMOCRAT = 0;

int REPUBLICAN = 1;

int REFORM = 2;

int LIBERTARIAN = 3;

int GREEN = 4;

int PEACE_AND_FREEDOM = 5;

// and so on ...

We can now assign party affiliations, just like we did with the names:

int senatorParty[] = {

LIBERTARIAN,

REPUBLICAN,

GREEN,

PEACE_AND_FREEDOM,

REPUBLICAN,

// and so on ...

}

Thus, senator #0 is a Libertarian, senator #1 is a Republican, #2 is a Green Party member, and so forth. Please note that these are not necessarily our party affiliations. This is just an example!

To keep track of how each person voted, we can create the following for each vote:

// Vote result:

int NAY = 0;

int YEA = 1;

int ABSTAIN = -1;

// Create the array for the bill.

int senateBill_A101[] = new int[100];

Given this array, we would record each senator's vote:

senateBill_A101[BROWER_ERIC] = NAY;

senateBill_A101[CHU_KEVIN] = YEA;

// and so on ...

The array senateBill_A101 now has a record of how each senator voted for Senate Bill "A101." Note that we even defined a value for abstention, because not all senators vote on all bills. It's important to understand the process you're modeling so that you can represent it correctly. If we hadn't defined that value, then a senator who abstained from the vote might incorrectly be assigned a vote of "NAY".