Comp
110
Lab
Project #1
8/24/2009
The
purpose of this assignment is to learn how to create, compile and execute java
programs in the environment provided.
Don’t worry too much about the syntax of the Java code. The meaning of all the keywords used and the
use of various delimiters and symbols will be explained over the next several
class sessions. Just concentrate on the
process you must follow to create java programs, to get them compiled, and to
execute (run) them. For each of the
problems below show the instructor the successful results you obtained before
going on to the next problem. If you
have any difficulty or have questions ask the instructor for help.
Problem
1: Create a simple application called “Hello,
world!” that does nothing but print out the statement “Hello, world!” when
executed. This is about the simplest
java program we can create. Once the
program is created, compile it. If you
encounter errors in the compilation process, fix the errors and try to compile
it again. Continue this process until it
compiles correctly. Finally execute the
program and check to see that it correctly prints the statement “Hello, world!”. To accomplish this
follow the following steps:
1.
Open
the jGRASP application (double click on the jGRASP icon on the desktop)
2.
Type
the following text into the window provided:
public class Hello {
public static void
main(String [] args) {
System.out.println("Hello, world!");
}
}
3.
Save
the file using the save command in the File menu.
4.
Compile
the source file by clicking the compile icon (the plus sign above the window
where you typed your program). If you
typed the program correctly, this step should complete with no error messages
displayed in the output window at the bottom of the screen. If error messages are shown, you need to
correct the errors in the file and then try to compile the program again. Repeat this process until the source file
compiles correctly.
5.
Execute
the program created by clicking the run icon (the person running). You should see the message “Hello, world!”
displayed in the window at the bottom.
Problem
2: Create a slightly more complex version of the
program written above in which the user will interact with the program by
typing in their name and receiving a customized greeting. Repeat the steps from Problem 1, except name
this program CustomHello, and name the source file CustomHello.java. The file to be entered is as follows:
import java.io.*;
public class CustomHello {
public static void main(String [] args)
throws Exception {
BufferedReader in =
new BufferedReader(new
InputStreamReader(System.in));
String myname = null;
System.out.print("Please
enter your name: ");
myname = in.readLine();
System.out.print("Hello,
" + myname + "!");
}
}
Problem
3: This problem demonstrates how to input
numerical information into a Java application.
It asks the user to enter two integers, adds them together, and prints
out the result. Repeat the steps used in
the previous two problems except call this program Addition and name the source
file Addition.java. This program is as
follows:
import java.io.*;
public class Addition {
public static void main(String [] args)
throws Exception {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
String s1 = null, s2 = null;
int n1, n2, sum;
System.out.print("Enter the
first integer: ");
s1 = in.readLine();
n1 = Integer.parseInt(s1);
System.out.print("Enter the
second integer: ");
s2 = in.readLine();
n2 = Integer.parseInt(s2);
sum = n1 + n2;
System.out.print("The sum of
the values is " + sum);
}
}