Lab 0: Variables and loops

Objectives

Part A. Programming environment

We use Forte for programming in this course and in later computer science courses. Forte (Community Edition) is a powerful programming environment available at no cost for most operating systems including Linux and Windows. It is available at Sun's Web site.

www.sun.com/forte/ffj
If you have a personal computer where you want write Java programs, Forte would be a good choice even if it weren't free.

Before we write our own programs, we need to gain a greater familiarity with using Forte. In this part of Lab 0, we'll use Forte to write and test a simple program. There is nothing to hand in for this part of the lab.

  1. Create your lab directory: (once per lab) Type the following at the Unix prompt.
    % getcs 160 X      (where X represents the lab number (it is 0 this time))
    
    This simply creates empty directories required for this lab. In some labs (but not this one) the command will also copy template files from which to start the lab material.

  2. Start Forte: (once per lab) Click on the K button in the lower left corner of the screen. From the K-menu, select the Forte submenu, and from that submenu select Forte4Java.

  3. Get Forte to finish starting: (once per lab) Have patience - it may take a couple of minutes. When/if you see an Automatic Update Check window, click No - you do not want new IDE updates. When you get a `Tip of the day' window, click Close.

  4. Open the current directory: (once this semester) In the window titled Forte(tm) for Java(tm), choose Mount Filesystem from the File menu. A dialog box will pop up. Type CS160 in the Directory text field, and click OK.

  5. Create your file: (each program) Now choose New... from the File menu. This brings up a dialog box.

  6. Enter the program: An editor window titled Hello will appear. Type the following.
    package Lab0;
    
    import csbsju.cs160.*;
    
    public class Hello {
        public static void main(String[ args) {
            IO.print("Hi! How old are you? ");
            int age = IO.readInt();
            IO.print("Next year, you'll be ");
            IO.println(age + 1);
        }
    }
    
    Type it exactly as above. Make sure it's identical.

  7. Run the program: Right-click in the editor window and select Execute.

  8. Debug the program: If you typed exactly what was above, the Output Window window will now display an error message. If you double-click on the message, it will highlight the offending line in the program.

    Correct the program. (In this case, the first error message suggests the correct change.) Then try running the program again.

  9. Test the program: If the program works now, the I/O window should pop up and ask you how old you are. Obediently respond with a number and press Enter.

  10. Quit the program: From the program's File menu, select Quit.

  11. Edit your program: The Forte window has several tabs along the bottom, reading ``Editing'', ``GUI Editing'', ``Browsing'', ``Running'', and ``Debugging''. Select the Editing tab. If you found some errors in your program while running it, you could change it at this point.

Observations

This program illustrates several new things about Java that you need to keep in mind to work with Forte.

Part B. Computing distance fallen

Write a Java class named DistanceFallen that reads a number t from the user and displays the distance it would fall in t seconds.

How long a fall? 2.0
You would fall 19.6 m.
Recall that the formula for the distance an item falls in t seconds is (1/2) g t2, where g is 9.8 m/s2.

Part C. Printing powers of two

Write a Java class, PowersOfTwo, that reads an integer n and displays the first n powers of two.

How many? 5
1
2
4
8
16

Part D. Printing a triangle

Write a Java class, Asterisks, that reads an integer n and prints a triangle of asterisks n on a side.

Width? 5
*****
****
***
**
*

Be sure to read the ``Submitting solutions'' section of this lab manual for instructions about how you will hand in your solutions to laboratories in this class.