DoDEA Laboratory # 1
An Introduction to Java and jGrasp
Objective: This lab is intended to introduce you to the tools with which you will be using as you learn to program in Java. The tasks you complete during this lab will be fairly simple. You will compose and edit a Java program in jGrasp, compile it, and see what happens when you run it. You will also get a chance to explore and develop a "new" program of your own.
Part A: Logging on to the network.
First you need to locate a computer in Wright 339. If it is not already booted to Windows, then reboot the computer to Windows as follows:
You are finally ready to start writing some Java code. To start the jGRASP environment click on the "Start" icon and select "Programming Languages" | Java | "jGRASP". It will take a few seconds to load jGrasp, but you should eventually see a window something like this:
Notice that the left hand side displays a view of the filesystem. Select the Z:\ drive, then navigate through this system until you locate the Lab1 directory you created earlier. At this point there should be only be one thing in this folder -- the "javabook" folder.
To open an editor window, select the "File" menu then "New File" | "Java." In this editor window you should type the following section of code. While there are certain things that you can modify in this code, we recommend that you copy this as closely as possible until you know a little more about the Java language and programming style. The only thing you should modify at this time is your name.
/*FILE: MyFirstProgram.java
* AUTHOR: YOUR_NAME
* DATE: 7/22/2002
* COMMENT: This program displays a window on the screen using the
* javabook package from An Introduction to Object-Oriented Programming
* with Java by C. Thomas Wu, McGraw-Hill.
*/
import javabook.*;
class MyFirstProgram {
public static void main (String[ ] args) {
MainWindow myWindow;
myWindow = new MainWindow("Hello World!");
myWindow.setVisible(true);
} // end main
} // end MyFirstProgram
Now you are ready to save, compile, and run your program and see if it works. To do this:
For example, if you forgot the semi-colon at the end of the "myWindow = new MainWindow("Hello World!")" instruction, then jGRASP would produce the following result in the "Compiler Messanges" window:
If you click on the text of one of the error messages displayed, jGRASP will highlight the part of your program in which the error was found in the upper (*.java) window. You can edit the text in this window to correct the error. Unfortunately, most compiler error messages are not this clear.
Part C: Modifying your program
Modify the previous program by inserting the following lines of code at the end of the "main" method (before the first '}').
//Creates a MessageBox
MessageBox greeting;
greeting = new MessageBox(myWindow);
greeting.show("Hello World!");
Notice that you should also update the "comment" section of the header to your program to reflect this addition. Follow the procedures you followed in Part B to save, compile, and run this program.
Congratulations, you have written a Java program! You are encouraged to continue experimenting with Java and jGRASP.
MainWindow Reference
Constructors:
public MainWindow ( )
public MainWindow ( String title ) // Creates Mainwindow with the specifed title
Methods:
public void setVisible ( boolean value ) // If value is true, the MainWindow is displayed.
MessageBox Reference
Constructors:
public MessageBox (MainWindow owner)
Methods:
public void show ( <type> value ) // Prints value to the MessageBox
The actual <type> may be boolean, char, double, int, long, String, or StringBuffer.