CS 124, Spring 2017
Lab 5: Arrays
In this lab, you will work with arrays for the first time. You will use arrays to add a new feature to the AdditionQuiz program from Lab 3. You will finally learn what "String[] args" means in the main() routine of a program. And you will learn about animation and use arrays to keep track of multiple animated objects in an animation.
As usual, you will need a copy of TextIO.java. You will also need a copy of the AdditionQuiz program from Lab 3. You can use your own, or you can use mine. A copy of my AdditionQuiz.java can be found in /classes/cs124. Finally, you will need a copy of FourthGraphics.java, which you can also find in /classes/cs124.
Because of the test, this lab will be due a little later than usual: It must be submitted by 12:00 noon on Friday, February 24. You will submit three files, named AdditionQuiz.java, CommandArgs.java, and FourthGraphics.java. Turn them in using the Submit124 web page, as usual.
Exercise 1: Getting Started With Arrays
For the first exercise, start with a copy of AdditionQuiz.java, either your version or mine. You will modify the program by adding some arrays and using them to store the information about the numbers used in each problem and the user's score for that problem. Do not change the name of the program. Open the program for editing. You should make the following changes:
- Declare two array variables, of type int[], to hold the two numbers that appear in each one problem. One array will hold the first number from each problem, and one will hold the second number.
- Declare another array to hold the user's score on each problem. This array will be either of type int[] or double[], depending on whether you keep track of the score as an int or as a double.
- Create the actual arrays. Remember that declaring an array variable does not create the array. To make an array, you have to use new, such as "new int[10]". The length of each array will be 10, since there are ten problems.
- When the program creates the numbers for a problem, store a copy of each number in the appropriate array. When the program gets a score for the problem, store a copy in the appropriate array. After the loop ends, the arrays contain a record of the problems and the scores.
- At the end of the program, in addition to telling the user the total score on the quiz, print out a table showing all the problems and the user's score on each problem. For example:
Num. Problem Your Score ---- ----------- ---------- 1. 17 + 2 10 2. 4 + 17 10 3. 7 + 12 10 4. 4 + 10 10 5. 43 + 6 5 6. 21 + 12 10 7. 80 + 19 0 8. 23 + 0 10 9. 25 + 17 5 10. 40 + 4 10
You can use System.out.printf to make a neatly formatted table like this one. All the numbers are printed using the format specifier %2d to make them line up nicely. (You could improve the program by adding more information to the table, such as the correct answer for each problem, the user's first answer and the user's second answer when there was one.)
Exercise 2: What's All This About "String[] args"?
This is a very short exercise to point out the meaning of "String[] args" in the first line of a program, public static void main(String[] args). Start by creating a new program file named CommandArgs.java.
"String[] args" represents an array of strings that comes into the program from outside. Specifically, the array holds the "command line arguments" from the java command that is used to run the program. These are words that appear on the command line after the name of the Java class. For example, if the program is named CommandArgs and the user runs the program with the command
java CommandArgs Now is the hour
there there are four command line arguments: "Now", "is", "the", and "hour". In the main() routine of the program, args represents an array of four strings containing the four words from the command line. That is, args.length is 4, args[0] is "Now", args[1] is "is", args[2] is "the", and args[3] is "hour".
Usually, command line arguments contain important information for the program, such as the name of a file that the program is going to process. For this exercise, however, the CommandArgs program should simply print the number of command line arguments and then print each argument in a numbered list. For example,
There were 4 command line arguments. 1. Now 2. is 3. the 4. hour
Like I said, it's short.
(There are some complications in how the command line arguments are determined. For example, if you enclose several words in quotation marks, then those words are joined together into a single command line argument. If you use a * on the command line, it acts as a wildcard matching file names in the current directory. The word that contains the * is expanded into as many file names as match the wildcard. For example, try using *.java as an argument to the CommandArgs program.)
Exercise 3: Arrays, Animation, and Global State Variables
When you compile and run the program FourthGraphics.java, you will see a simple animation. The only animated object is a "moon" that continually rises and sets. But there is also a background of small "stars". You will make the stars move, and you will add some other feature to the animation.
A computer animation is simply a sequence of drawings, shown rapidly one after the other, with small changes from one drawing to the next. Each drawing is called a "frame" of the animation. This is not so different from previous graphics programs that you have worked on, where the picture was drawn every five seconds. Now, however the picture is redrawn thirty times per second. Another difference is that the individual frames are not independent—you have to keep track of certain information between frames, that is, between calls to the paintComponent() method.
In Java, you can have variables that are outside any subroutine definition (but still inside some class). Such variables are called global variables. A global variable keeps its value between calls to subroutines, so it can keep track of the "state" of the program as it changes over time. For the animation program, the state includes a frameNumber that starts at 0 and goes up by one each time the paintComponent() method is called. You can animate an object by using the value of frame number when computing the position or other property of the object. That's what's done for the "moon" in FourthGraphics.java.
There are also two arrays in the program that are used as global state variables. The arrays store the positions of the "stars". There are 100 stars. There are two arrays, named starX and starY, each one holding 100 real numbers. The center of star number i has coordinates (starX[i],starY[i]). The arrays are created and filled with random values in a method named init(). The main routine calls init() once, at the start of the program; init() is provided as a place where you can initialize global state variables. The two arrays are used in paintComponent() when drawing the stars.
Your first task is to make the stars move. To get an interesting effect, add a small, random amount to the x-coordinate of each star, and add a different small, random amount to the y-coordinate of each star. The random amount can be selected as a random real number in the range -1.5 to 1.5. You need both positive and negative amounts so that the stars won't drift off in one direction. Instead, they will sort of wiggle in place. (This is an example of something called Brownian motion.) Remember that the coordinates are stored in the two arrays, which are of type double[]. You just need to go through the arrays and add a random value to each element. Do this at the beginning of the paintComponent() method. Run the program, and see what it looks like.
Your second task is to add several more arrays to the program and use them in the animation. The arrays need to be declared as global state variables. They need to be created and filled with initial values in the init() method. And they need to be updated and used in the paintComponent() method.
One option is to give the stars random colors. Use an array of Color to store the color of each star. (Doing this by itself is not sufficient since it uses only one array.)
Another option would be to add a new kind of animated object. For example, you could have "falling stars" that drop from the top of the picture to the bottom of the picture. Use two arrays for their x- and y-coordinates. You could also use a third array to hold a different speed for each falling star. In each frame, just add the star's speed to the star's y-coordinate to make it move down a bit. Ideally, when a star moves off the bottom of the picture, you should move it back to the top (with a new random x-coordinate and maybe a new random speed). Instead of stars, you could make "meteors" that move diagonally or snow that falls slowly, with some brownian motion in the horizontal direction. Or you could have clouds that drift horizontally. Or you could have puffs of smoke drifting up from a chimney and getting bigger as they rise.
Maybe you can think of something more original. (You can remove the moon or leave it there. It's there mostly so that you would see some animation when you run the original program. If you want, you can add other individual objects, but it is not required. Position is not the only thing that can be animated. You can animate size. You can animate color by varying the red, green, or blue components of the color. Animating the alpha value of a color can make something fade away over time.)