?xml version="1.0" encoding="UTF-8" ?> Arrays, Objects, and Random Behavior

Arrays, Objects, and Random Behavior

In this lab, you are going to (1) use random numbers to create an interesting display, (2) use an array to store a large collection of objects, and (3) add a new capability to an object.

Random behavior

Generally computer programs follow the one true path, but sometimes it's even better when are a tad unpredictable. Computer games would be pretty boring if they always did the same thing. Artistic computer animations also need a bit of variation to be interesting. Simulations of the "real-world" should also explore, in an unpredictable way the many ways natural phenomenon may occur.

Computer programs can vary their behavior by using random numbers. Java has two ways to be random. The first is through the very versatile java.util.Random class which can be used to generate random booleans, ints, and doubles that follow uniform and normal distribution. If you understand all that, you may want to use the java.util.Random class.

However, in this lab we'll follow the simple path to randomness. The Math.random() will return a random double that is greater than or equal to zero and less than one. Don't be turned off by the double part. If you want to generate a random integer from one to six, like you might get by rolling a die, then do the following: (1), use Math.random() to generate a random double ra, where 0.0≤ra and ra<1.0; (2), multiply ra by six to get a random double rb, where 0.0≤rb and rb<6.0; (3), use the (int) casting operator to get a random int rc where 0≤rc and rc<6; and (4), add one to get a random int rd where where 1≤rd and rd<7 (or rd≤6). Or better yet just do the single assignment:

    int rollOfTheDie = (int)(Math.random()*6) + 1 ;

You want to roll two dice? Then try out the following:

    int rollOfTheDice = (int)(Math.random()*6) + 1 + (int)(Math.random()*6) + 1 ; 

It's really not hard.

Downloading the project framework

Download StarField.zip, a ZIP file containing a NetBeans project named StarField and unZIP this project into your csci/201 directory. Make your Projects panel look like the following continuing.
Projects panel for StarField

Compile the code and run it to see a decent representation of the union of the America flag. Next take a good look at the StarField class because a lot of work went into getting it to work. We even had to read an Executive Order from 1969 to figure out where those stars should go.

Making a messy star

Let's suppose you went into a kindergarten classroom and gave each kid (1), a piece of blue paper that had the same proportions as the union of the US flag; (2), fifty stars; and (3), a bit tube of paper glue. On that piece of blue paper there are fifty little dots which mark exactly where those fifty stars should be placed.

The eager five-year-old kids are told to glue the stars on the flag. Your task for this week is to give a reasonable rendition of the result of their work. We all know they aren't going to get those stars exactly on those dots, but we will assume they try to get them pretty close and don't glue stars on their own foreheads.

First attempt

Start by adding a little randomness in the vertical placement of those starts. Find the statement in the StarField class that goes:

            int starY = (int)((row+1)*F) ;

Change it to add a sloppiness in the gluing process:

            int starY = (int)((row+1)*F) + (int)(Math.random()*20) - 10 ;

By subtracting ten from the exact vertical position of the star and then adding a random number from zero to nineteen, you should get stars that have a little variation. You may need to play around those numbers until you can see stars getting out-of-line.

Are we there yet?

At this time, you may be thinking that this week's lab is going to be pretty smooth setting: Just change that code involving x and you'll be out the door.

But hold on. Take your mouse and resize the window containing the stars. You should get a little bounce in your stars. That's very interesting but it's not what we want. Those kindergartners used so much glue there's no way those stars can move.

What is happening here?

Every time you resize the window, Java calls the paint method of StarField to repaint the union. When this happens, you get a new set of random numbers and bouncing stars. We've got to fix that.

Using arrays to hold the stars

You can't compute a new position for every star on each call of paint. Your program must compute and remember the position at each star when the StarField object is created and then use the remembered positions when calling draw method for each star.

To do this, you must: (1), create an array to hold fifty Star5 objects; (2), initialize that array within the StarField constructor; and (3), draw each of those Star5 objects in the paint method.

Creating the array is easy. Just add something like the following reference variable declaration to the StarField class.

    private Star5[] stars = new Star5[NUMSTARS] ;

You already have the code to compute the random stars, but you must move it from the paint method to the StarField constructor. Be sure to change the line of code that calls the draw method of the Star5 to a line that stores the newly created Star5 object in the array.

Drawing the stars is easy. Add a loop similar to the one below in the big space left in the paint method.

    for (int i = 0; i < NUMSTARS; ++i) {
        stars[i].draw(newG) ;

Show your instructor that your stars don't bounce on window resize.

Making the position random

Now modify StarField so that there is a bit of randomness in the horizontal placement of the stars. I think the kids will do better here.

Put some spin on it

You know those kids aren't going to get the points of those stars lines up straight. Fortunately, Star5 has a constructor with four parameters. The fourth parameter is an integer to specify the number of degrees the star was rotated when it was placed on the union.

Modify StarField to use this constructor and add a little twist to your stars.

Show your instructor that your stars are now messy in three dimensions.

Just one more problem

This one will be hard. It turns out that not all those stars were white. A few were yellow. Consequently in the finished unions about one-tenth of the stars are yellow.

To fix this problem, you must modify the Star5 code. Go to the Star5.java file and add a new reference variable to hold the color of the star. You'll need to either add a new five-parameter constructor or add a setColor method. [Better yet, you should do both.]

You'll also need to modify the draw method to use the color that was set for the star rather than just painting with white.

As soon as you have Star5 accepting different colors, you need to modify StarField to randomly choose yellow about 10% of the time. Think about how often the boolean expression Math.random()<0.1 is true.

Don't feel constrained to have yellow tinged stars. If you want to go wild choose totally random colors or use a variety of grays to make it look like the kids got the stars a little dirty with the glue.

Show your instructor stars with a little color.