Lab 0: Variables and loops
Objectives
-
to become familiar with the Forte programming environment.
-
to gain a better understanding of while loops.
-
to practice writing simple interactive programs.
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.
- 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.
- 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.
- 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.
- 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.
- Create your file: (each program)
Now choose New... from the File menu.
This brings up a dialog box.
- In the ``Select a template'' section, double-click on Classes and
click on Empty. Then click the Next button at the dialog box's bottom to
go to the next step.
- In the Name text field, type ``Hello''
(the name of the class you're writing for this part of this lab).
Also, in the ``Please select a package...'' section, select
Lab0 from under your CS160 folder.
Finally, click Finish at the dialog box's bottom.
- 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.
- Run the program:
Right-click in the editor window and select Execute.
- 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.
- 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.
- Quit the program: From the program's File menu, select Quit.
- 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.
- Notice the line ``package Lab0;''. You'll
always want a line like this in each class you write using Forte,
based on the lab you're currently doing.
- The method name here is main(), and it takes a parameter
String[] args. This is the method that Java will automatically
begin when it runs a class.
- The program uses the IO.readInt() method to
read a number from the user. You'll need this method and the
corresponding methods for the other types for Part B.
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.