Incremental Java
Multiple Boxes

Multiple Boxes

Computers and programs wouldn't be very useful if you had one box that held one value.

Think about it. A spreadsheet can hold hundreds of values. A game needs to keep track of points, positions, how to draw the image. A browser needs to keep track of the URL, how to layout the webpage, and so forth.

Any reasonable computations requires many boxes.

Now that we have so many boxes, we need a way to identify one box from another box. In the previous lesson, when we performed a read or write operation, we knew exactly which box we were reading from or writing to.

We're going to have one restriction. Even though we have many boxes, we can only read or write to one box at a time. However, now we need to say which box we wish to read from or write to.

Box IDs

We're going to give each box a special number, called the box ID (for box identification).

We'll number the boxes starting at 0. Why 0? It turns out numbering from 0 is very common in computer programming. As weird as that is, you need to get used to it. So why not get used to it right now.

If we have 3 boxes, we number them box 0, box 1, and box 2.

If we have 10 boxes, the boxes are numbered from box 0 up to box 9.

If we have N boxes, the boxes are numbered from box 0 up to box N - 1. We'll often refer to N rather than a specific number. You see the use of variables such as N a lot in computer programming, so you might as well get used to it now.

Confusion!

Here's the first place you may get confused, so please pay attention.

We have two numbers for a box: the box ID and the value in the box. For example, box 3 may have the value -1. Box 10 may have the value 100. Box 12 might have the value 12.

When you hear a number and a box, you need to distinguish between the box ID and the box value. The box ID tells you the box we're referring to, and the box value is the value inside the box.

Modified Rules

Previously, we used commands like "Read" and "Write 7" to look at the value of a box, or to change it to a new value.

Now that we have more than one box, we need to modify the commands to indicate which box we're talking about.

In particular, we'll now say:

When you read a value from the box, say its value. When you write, write the values of all the boxes. If there are three boxes, you can write [ 0, 0, -1 ] to indicate that box 0 has value 0, box 1 has value 0, and box 2 has value -1.

When you write to a box, only that value is changed. The other boxes are unchanged.

Exercise

Let's try an exercise to see if you understand multiple boxes. Assume we have three boxes. Based on our rules, the boxes are numbered box 0, box 1, and box 2. Assume that each box starts with an initial value of 0. Thus, we have [ 0, 0, 0 ].

To do this exercise, draw three boxes, left to right. Above the first box, write the number 0 (that's box 0). For the middle box, write the number 1. For the right box, write the number 2. Inside each box, write the number 0. That's the initial value of each box.

Let's begin:

  1. Read from Box 2
  2. Write 7 to Box 2
  3. Write 3 to Box 1
  4. Read from Box 2
  5. Read from Box 0
  6. Read from Box 2
  7. Write -1 to box 2
  8. Read from box 2
  9. Read from box 1

Solution

Solutions are written in red
  1. Read from Box 2 Box 2 has value 0
  2. Write 7 to Box 2 [ 0, 0, 7 ]
  3. Write 3 to Box 1 [ 0, 3, 7 ]
  4. Read from Box 2 Box 2 has value 7
  5. Read from Box 0 Box 0 has value 0
  6. Read from Box 2 Box 2 has value 7
  7. Write -1 to box 2 [ 0, 3, -1 ]
  8. Read from box 2 Box 2 has value -1
  9. Read from box 1 Box 2 has value 3
Again, steps 4 and 6 give the same value for box 2, because we didn't change the value of box 2 between steps 4 and 6.