Review Olymic Rings with Shifting Colors; more on drawing; introduction to arrays or lists; subscripts or indexes
Rewrite Olympic.java so that the colors of the rings shift constantly.
The key to doing exercise 1 is to combine the random number generator with a call to Color with random arguments in the range of 0-255, or to use a random selection from an array of colors. Let us look at the second option.
The standard colors we can use in java are:
//File: Olympic2.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Olympic2 extends Applet
{
// An array of colors to use at random
Color [] myColors = { Color.black, Color.blue, Color.cyan,
Color.darkGray, Color.gray, Color.green,
Color.lightGray, Color.magenta, Color.orange,
Color.pink, Color.red, Color.yellow};
// Use init to set to background white
// to contract with the colors of the rings
public void init()
{
setBackground (Color.white);
}
//The paint() method is called by the browser whenever the applet must be redrawn
public void paint (Graphics g)
{
g.setColor (
myColors[(int)(Math.random()*(myColors.length-1.0+.5))]);
g.drawOval (10, 10, 75, 75);
g.setColor (
myColors[(int)(Math.random()*(myColors.length-1.0+.5))]);
g.drawOval (95, 10, 75, 75);
g.setColor (
myColors[(int)(Math.random()*(myColors.length-1.0+.5))]);
g.drawOval (180, 10, 75, 75);
g.setColor (
myColors[(int)(Math.random()*(myColors.length-1.0+.5))]);
g.drawOval (53, 42, 75, 75);
g.setColor (
myColors[(int)(Math.random()*(myColors.length-1.0+.5))]);
g.drawOval (137, 42, 75, 75);
try
{
Thread.sleep(500); // sleep for .5 sec
}
catch (InterruptedException t)
{
}
repaint(); // calls update() to CLEAR THE WINDOW
// then calls paint()
}
}
There is a subtle error in the Olympic2 code. Look carefully at how the rounding was done. The .5 should have been added after multiplying by Math.random(), not before. The result is a slight bias towards the bottom bin. There is no way to find and remove this bug by watching the problem run. You have to read and understand the code to find it. Many bugs are like this. That is why is not sufficient to debug code by simply trying it and fixing it until it seems to work.
Change the animations in Surprise2.java so that the circle grows larger and then grows smaller and disappears. Spend some time on this one.
Change the animations in Surprise3.java so that as the circle grows larger and then grows smaller and disappears, a label is applied which says "Small", "Medium", "Large", "XL", "XXL", as appropriate. Hint: it can help to use an array. Spend some time on this one.
The answers to the other two exercises build on each other. We will only examine the combined answer:
The hair has been drawn as a beard. That is because java uses a default coordinate system which is upside-down compared to the one commonly used in when laying out drawings by hand or drawing graphics in a physics class. The point with the coordinates (0,0) is at the top left and increasing y-coordinates go down the screen. This arises from the natural conflict between laying out images, for which it is common to place (0,0) at the bottom left corner, and laying out text, in which the first character of the first line is normally placed at the top left. Unless we introduce special transformations, when writing java, we have to use the text-oriented convention.
On most computers, the applet will show a disturbing flicker of partially drawn images. In the next applet, we cure the inverted image by changing a + to a -, and we cure the flicker by drawing using a Graphics object in memory and then transferring all the pixels at once to the displayed Graphics object:
We began with a motivational example. Suppose you want to calculate the average of a list of numbers (int or double) followed by a calculation of the standard deviation. You quickly realize that by the time you're ready to do the standard deviation, you only have access to the last number in the list. The solution is provided by arrays since they allow you to name a collection of memory locations with one identifier. Subscripts or indexes then allow you to access each individual memory location. The following example illustrates this.
//File: NumberList.java
//This class will gather together in one place
//functions (or methods) which process lists of
//numbers (int or double).
import iostuff.*;
public class NumberList
{
//The following two methods calculate the average of a list
//of int's or double's respectively
public static int average (int [] a)
{
int sum=0;
for (int k=0;
k<a.length; k++)
{
sum = sum + a [k];
}
return sum/a.length;
}
public static double average (double
[] a)
{
double sum=0;
for (int k=0;
k<a.length; k++)
{
sum = sum + a [k];
}
return sum/a.length;
}
}
01: //File: Average2.java
02: import CSLib.*;
03: import java.text.DecimalFormat;
04: public class Average2
05: {
06: public static void main (String [] args)
07: {
08: InputBox in;
09: OutputBox out;
10: double number [];
11: int n;
12:
13: in = new InputBox();
14: out = new OutputBox();
15:
16: in.setPrompt ("How many numbers? ");
17: n = in.readInt();
18:
19: number = new double [n];
20:
21: //Inputs the numbers into the array
22: for (int k=0; k<n; k++)
23: {
24: in.setPrompt("Next score please: ");
25: number [k]= in.readInt();
26: }
27:
28: //The following code displays the list to the screen
29: for (int k=0; k<number.length; k++)
30: {
31: out.println (number[k]);
32: }
33: out.println();
34:
35: double avg = NumberList.average(number);
36:
37: DecimalFormat output = new DecimalFormat("0.00");
38: out.println ("The average is: " + output.format(avg));
39: }
40: }
For those using older versions of the text, this is a client program:
//The following code displays
the list to the screen
for (int k=0;
k<number.length; k++)
{
System.out.println
(number[k]);
}
System.out.println();
double avg =
NumberList.average(number);
DecimalFormat output = new
DecimalFormat("0.00");
System.out.println ("The average is:
" + output.format(avg));
}
}
Note that arrays "know" their length. That is, it is not necessary to pass the number of items as a parameter to any of the methods in NumberList. Instead, when that information is required, the length of the array is retrieved from the array itself; e.g., a.length or number.length.
Lab Exercise 1. Add a method that displays a list of int's to the NumberList class. The signature for the method will look like:
public static void display (int [] a)
HINT: This method should incorporate the section of Average.java (above) that "displays the list to the screen".
Lab Exercise 2. Add a method that calculates the standard deviation of a list of int's to the NumberList class. The signature for the method will look like:
public static int sd (int [] a, int average)
Feel free to consult Prof. Steinmetz's version if you get exasperated.
Adapted from Bill Steinmetz's course summary by H. J. Bernstein, 12 Nov 01
We follow up here