0.1
Creating
and Running a Java Program under JDK1.2.1
The labs at Heinz are equipped with the JDK
compiler and the documentation may be accessed via Sun’s Java site on the World
Wide Web. These initial steps are a guideline to installing the JDK compiler
and documentation on a home computer.
What are the steps in creating and executing a
Java program from a MS Windows machine?.
0) First,
you need to obtain a copy of the JDK1.2.1 compiler. This compiler is available
on the web site http://java.sun.com/products/jdk/1.2/download-windows.html.
Before proceeding, take some time to read the materials on that site. There are
installation instructions that you should read before actually doing the
download. After reading the material on downloading, choose the continue option
under the Download JDK 1.2.1 Software for Windows 95 / 98 / NT 4.0 (Intel
Platform). After the download (which may take over an hour from a home PC)
use the My Computer icon and look under the directory c:\MY DOWNLOAD FILES and
double click on the downloaded file (this file will have a .exe extension).
This will cause the .exe file to expand
into several files. If the download and file expansion were both successful,
you should have a directory called JDK1.2.1 on the root directory of your C:
drive. Again, using the MY Computer icon, you should see C:\jdk1.2.1 listed as
one of the many directories on your C: drive.
1) Next,
you might want a copy of the documentation that is provided with the compiler.
(If you are short of disk space, you might choose not to download this file. In
this case you will still have access to the documentation via the World Wide
Web.)The web site mentioned in step 0 has an option that reads “Download JDK 1.2.1 Documentation”.
Choose the Browse and Download documentation option and then download
one large bundle in HTML format.
2) If
you do not already have a copy of WinZip (used to decompress compressed files)
you now may want to download a copy of WinZip. At the time of this printing, an
evaluation version of this program can be found at the web site -
http://www.winzip.com/winzip/download.html
3)
Once the WinZip program is downloaded and then
run on the documentation file (jdk1_2_1-doc.zip) you should have a directory
called docs under the directory c:\jdk1.2.1. The docs directory should contain
a file called index.html. If you double-click on this file, your browser will
run and present you with file-based and internet-based documentation. Either of
these may be used when connected to the internet. Only the file-based
documentation may be used when not connected.
4) As
described in the installation instructions on Sun’s web site, you will need to
edit your Autoexec.bat file so that the jdk1.2.1 directory is in your command
path. This file should be found at the root of your C drive. To edit the file,
choose Start/Programs/Accessories/NotePad. Then, use the file menu to open the
file autoexec.bat on your C: drive. Add the following line to the file. Do not
delete any similar lines that may already be present:
path=c:\jdk1.2.1\bin;%path%
5) Reboot
your computer to install jdk1.2.1 in your command path.
6) You
will need to open two windows in Windows ’95. One will be for editing your Java
programs and another will be for executing the compiler and the java virtual
machine. At home, I use Notepad to edit my Java programs and an MSDOS window to
execute commands.
7) The
MSDOS window can be opened by selecting Start/Programs/MSDOS Prompt.
8) The
MSDOS window can be toggled between a full screen view and a window by pressing
ALT+Enter. To quit the MS-DOS window click the X or type exit at the command
prompt. The size of the print within the MS-DOS window and the size of the
MS-DOS window itself can be modified by selecting the A symbol on the top right
of the window. With some experimentation this window works well in the Windows
environment.
// Sums.java // Compute a sum of integers import java.io.*; public class Sums { public
static void main(String[] args) throws IOException,
NumberFormatException { int cnt = 0; int sum = 0; int start = 0, stop = 0; System.out.print("The
sum will be computed\n\n"); System.out.print("Input
start <return> stop integers with start <= stop: "); // DataInputStream objects read primitive Java
types //
We pass the current InputStream to the constructor DataInputStream in = new
DataInputStream(System.in); //
read a line until we have a newline and/or carriage return //
may throw IOException String
s = in.readLine(); //
create a new Integer object from the string s //
trim() strips whitespace from the start and end of the string //
constructor may throw
NumberFormatException start
= new Integer(s.trim()).intValue(); s
= in.readLine(); stop
= new Integer(s.trim()).intValue(); int
j = start; while(j
<= stop) { cnt
= cnt + 1; sum
= sum + j; j++; } System.out.println(start
+ "+...+" + stop + " = "+ sum); System.out.println("Enter a <return>
to quit"); String hold = in.readLine(); } } |
Figure 0.1 |
9) Using Notepad,
enter the program in Figure 0.1 and call it Sums.java (the file name must be
the same name as the public class). Save this file under a new directory called
firstjava. You can make a new directory by using the MY Computer icon. Compile
the program into Java machine language with the command javac Sums.java entered
in the MS-DOS Window. Your current directory should be firstjava. The javac
command should create a new file called Sums.class. Check and see if Sums.class
is present. Next, run the program using the command java Sums. If you have
errors, go back into Notepad and fix them. If you have errors you will need to
repeat the steps to edit, compile and run the program.
When
the program runs you will be prompted for two integers separated by a
<return>. The program computes the sum of all the integers between and
including these two. You need to make sure that the second integer is greater
than the first. After the sum is computed, hit return.
The
sum will be computed
Input
start <return> stop integers with start <= stop: 1
100
1+...+100
= 5050
Enter
a <return> to quit
10) Repeat step 9 for the interest Program 0.2
that computes the value of money . Call the new directory Secondjava and call
the program file “Interest.java”.
// A first Java program to compute the value of
an investment // before inflation. import java.io.*; public class Interest { public
static void main(String[] args)throws IOException,
NumberFormatException { // define variables double interestRate, money; int years;
// prompt and read initial investment from user System.out.println("Enter initial
investment");
DataInputStream in = new DataInputStream(System.in); String s = in.readLine(); money = new Double(s.trim()).doubleValue();
// prompt and read rate of return from
user System.out.println("Enter rate of
return");
s = in.readLine(); interestRate = new
Double(s.trim()).doubleValue(); // prompt and read the number of years
of investment
System.out.println("Enter number of years"); s = in.readLine(); years = new
Integer(s.trim()).intValue(); // compute and then display the value
of investment before inflation int y = 1; while(y <= years) { money = money + money * interestRate; y++; } System.out.print("After " +
years + " years"); System.out.println(" the
investment (before inflation) will be ");
System.out.println(money); System.out.println("Enter a
<return> to quit"); String hold = in.readLine(); } } |
Figure 0.2 |
11) Fill out the worksheet and hand it in to your
instructor when you are finished.
Lab 0 Activities Sheet
Name____________________________ Date _____________________________
Directions: Carry out each activity, answer each
question in the space provided, and turn these sheets in to your instructor.
Note: Neatness of presentation counts for 2
points. Use a stapler, be orderly, and make ‘penciled in’ notations if and when
appropriate.
1) Run
the first program (Sums.java) several times.
List the output you see on the screen when entering the inputs listed
below:
User Input
Output
1 <return> 100
99<return> 100
1 <return> 10000
1 <return> 200000
2) Now,
in the Notepad editor window, change the type of the variable sum from int to
double. Run the program again with the same numbers.
User Input Output
1 <return> 100
99<return> 100
1 <return> 10000
1 <return> 200000
3) Briefly
explain why there are differences between the tables in steps 1 and 2. Has
there been an improvement? In what way ?
4) Open
the interest program in Notepad. List
every variable and object used in the program (hint: look for their
definitions, variables have types and objects have classes ).
5) Run
Interest.java. Enter 100 dollars for 10
years at a 6% interest rate. What is
the final value after 10 years ?
6) Run
the program Interest.java. Enter a percent sign with an interest rate rather
than a decimal fraction ,e.g. , 10% rather than 0.10. Does the program still
work? What happens?
7) Assuming
an interest rate of 6%, in what first year will $100 be more than doubled in
value?
8) Suppose we wish to modify the program so
that so that it prints the value of our
investment during each year of the investment. Try moving the System.out.print
statements from their position at the bottom of the loop to some position
within the loop. Experiment.
Print a copy of your new program after you have it working.
9) Run
the new Interest.java (the one created in step 8). Enter 100 dollars for 10 years at a 6% interest rate. Click the Mark button on the MS-DOS window(this
is the screen where the output is displayed) and then select all the text that
is on the screen. (Click and drag the mouse.) Then click the Copy button that is next to the Mark button. Close this screen, run
another copy of the Notepad program and Paste
(look under the Edit menu) the
output onto this new text file screen. Select File-Print and print the table to
the printer. Turn in this print out with your Lab0 activities sheet.
10) On
pages 20 and 21 of the Java Gently text, there is a program that draws
the olympic rings in a graphics window. Enter the program using Notepad, save
it in an appropriate directory, compile and run the program. Then, modify it so
that the six rings appear in the middle of the screen. Turn in a
printout of this new Rings.java. When you compile and run, use the following
command from the MS-DOS window:
javac
Rings.java
java
Rings
If you are using
the c:\ users directory at the Heinz School, make sure to copy your java
program files (.java) to a floppy disk and then delete all other files from the
c:\users directory.