CS124, Fall 2001
Lab 4: For Loops (and Some Graphics)

In this lab, you will be writing a couple of programs that use for loops (as well as other features of Java that we covered in the past, of course). There is also a section that continues the study of applets by looking at some of the built-in commands for drawing text and geometric figures in an applet.

All the files that you need for this lab can be found in the directory /home/cs124/lab4. (They are also linked to this page so that you can read them on-line if you want.) To begin the lab, you should copy this directory into your home directory. You can do this using the GUI or with the command line

cp  -r  /home/cs124/lab24  .

The rest of this lab assumes that you are working in your copy of the lab4 directory, so you will need to say cd lab4 to change into that directory.

The lab report for this lab is due next Wednesday, September 26.

Exercise 1: Adding with a for Loop

Write a complete Java program that will add up some numbers entered by the user and print out the total. The numbers can be real numbers (of type double). Start by asking the user how many numbers she has. Then read in the numbers one at a time. Each time you read a number, add it to a variable named, for example, total. Use a for loop to read the correct number of values. You could do this program with a while loop, but you are required to use a for loop. Here is what the program might look like when it is run, with the user's input shown in red:

           Hi! I will add up some numbers for you.
           How many numbers do you have?  4
           Enter your numbers one at a time, pressing return after each number:
           The sum of your numbers is:  154.231

Make a print-out of your program and turn it in as part of your lab report next Wednesday. Remember that any program that you write for this course should follow the rules of good programming style!

Exercise 2: Maximizing with a for Loop

The file ThreeN.java contains the source code for generating a 3*N+1 sequence and counting the number of terms in the sequence. Suppose that I want to know the length of the longest 3*N+1 sequence among all values of N from 1 to 10000. Write a program that will figure this out and print the answer. The program should print out a message something like "The longest sequence has 37 terms." (This is not the correct answer.) You might want to write your program this by editing the file ThreeN.java.

The original program gets the number N from the user. You don't want to do this. You want to test each value of N from 1 to 10000. Also, you don't want to print out anything except for the final answer. Use a for loop to process all the possible values of N. You have to keep track of the largest sequence length that you have seen. Use a variable for this. Every time you find a sequence length that is bigger than the value in the variable, change the value in the variable. At the end of the loop, the variable will contain the overall maximum value, which is what you want to output.

Make a printout of your program. On your printout, write down the answer that is printed when you run the program. Turn in the printout as part of your lab report.

Exercise 3: Introduction to Applet Graphics

Section 3.7 of the text introduces applets and applet graphics. (It also talks about animation, but we won't be covering that in this lab.) For this exercise, you will create an applet that draws some simple figure on the screen. Do this by editing the file Draw1.java. The original version of this file is an applet that draws a red disk on a blue background, along with some white text:

You can change the background color of the applet by modifying the statement setBackground(Color.blue) in the source code. Other colors that you can use are: Color.red, Color.green, Color.yellow, Color.cyan, Color.magenta, Color.black, Color.white, Color.gray, Color.lightGray, Color.darkGray, Color.pink, and Color.orange.

To change the content of the applet, replace the lines

          g.drawString("Here's a big red dot:", 10, 15);

with a series of commands that draw something more interesting. You can assume that the applet is 200 pixels wide and 200 pixels high. Here are some commands that you can use for drawing:

g.setColor(c), is called to set the color that is used for drawing. The parameter, c is an object belonging to a class named Color. The specified color is used for all drawing operations up until the next time setColor is called. (If you don't set a color at the beginning of the paint() method, the drawing color will be black.)

g.drawRect(x,y,w,h) draws the outline of a rectangle. The parameters x, y, w, and h must be integers. This draws the outline of the rectangle whose top-left corner is x pixels from the left edge of the applet and y pixels down from the top of the applet. The width of the rectangle is w pixels, and the height is h pixels. If w and h have the same value, this will draw a square.

g.fillRect(x,y,w,h) is similar to drawRect except that it fills in the inside of the rectangle instead of just drawing an outline.

g.drawOval(x,y,w,h) draws the outline of an oval. The oval that is drawn is the one that just fits inside the rectangle that would be drawn by g.drawRect(x,y,w,h). If w and h have the same value, this will draw a circle.

g.fillOval(x,y,w,h) is similar to drawOval except that it fills in the inside of the oval instead of just drawing an outline.

g.drawString(message,x,y) draws the text of the message, which must be a value of type String. Parameters x and y give the position of the string in the applet. The left end of the base of the message will be at the point x pixels from the left edge of the applet and y pixels down from the top of the applet. Note that this is similar to System.out.println(message), except that you have to say where in the applet the message should appear.

Ordinarily, an applet is displayed on a Web page. However, there is a program named appletviewer that allows you to see the applets in an HTML file without using a Web browser. This command plays the same role for applets that the java command plays for standalone applications. This means that the "edit/compile/run" cycle for the applet looks like:

              nedit  Draw1.java
              javac  Draw1.java
              appletviewer  Draw1.html

Note that you have to apply appletviewer to an html file. I have provided the file Draw1.html for you.

When you have a picture that you like, print out your Draw1.java file. Turn in this file as part of your lab report. You should post your applet on the web by copying the files Draw1.html and Draw1.class into your www directory. (You can edit the Draw1.html file if you like.) Your applet will then be available on the web at a URL of the form http://math.hws.edu/user/username/Draw1.html.

Some Fun

This is not a required part of the lab.

I mentioned the "fortune" program in a previous lab. Some people like to have this program run automatically, every time they open a new command-line window. This puts a random quote in the window. There is a file named ".bashrc" in your home directory. (The period at the beginning of the name makes this a hidden file that is not included in ordinary directory listings.) This file contains commands that are automatically executed when a command-line window is opened. You can add new commands at the end of this file.

Try it! Make sure you are working in your home directory. Edit the .bashrc file, using the command nedit  .bashrc for example. At the end of the file, add a line containing the command fortune. Save the file. The next time you open a command-line window, you should see a randomly selected fortune!.

David Eck (eck@hws.edu), 20 September 2001