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:

  1. Click the "shutdown" button to pop-up the Debian logout window.
  2. Select "Reboot" and click the "OK" button.
  3. The computer will scroll a bunch of text as it reboots. Eventually it will come to a blue screen with a menu on it. Quickly, select "Windows NT" from this menu using the down-arrow key.
  4. Once the system has completely loaded Windows, hold down the <Ctrl>, <Alt>, and <Del> keys to pop-up the Windows logon window
  5. Select the "CNS.LAB" Domain
  6. In the Window logon window enter the username "dodea" and password "dodea". Do NOT change the password!
Once you're logged on, create a subdirectory in this shared account to store your files on the Z: drive.
  1. Double-click on the "My Computer" icon.
  2. One of the "disks" that is mounted when you log on to Windows is your account on the CNS server. This will be labeled something like "dodea on 'Student' (Z:)" Double click the icon for the Z: drive.
  3. Double-click on the "CS Participants" directory stored on this drive. Create a new directory (folder) by right-clicking the mouse in the window of the "CS Participants" directory. Select "New" then "Folder" from the pop-up menu. Name this new folder using your last name.
  4. Right-click on the "Lab1" subdirectory in CS Participants\Java and select "Copy"
  5. Right-click and "Paste" the "Lab1" subdirectory into the new folder of your last name.
Part B: Writing your first Java program

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:

  1. Select the "Save" item from the "File" menu associated with the MyFirstProgram window (the disk icon will do the same thing). Java requires that the name of the file be the same as the class contained in the file. Thus, you must save this file with the name MyFirstProgram.java.
  2. Select the "Compile" item from the "Compile" menu (the green "plus" icon will do the same thing). This translates your program from Java into a more primitive language (the "bytecode") that is easier for the computer to understand. If you typed one or more grammatical or typographical errors, then jGRASP will display semi-meaningful error messages in the small "Compiler Messages" window running along the bottom of the screen (see below for more debugging hints). If you had compiler errors, fix the errors and repeat the save and compile steps. The "Compiler Messages" window will display the message "---- jGRASP: operation complete." when no errors are detected.
  3. Select the "Run" item from the "Run" menu (the red "running stick figure" icon will do the same thing). If all goes well, this should open up a new window with the title "Hello World!" at the top. NOTE: you must close this window before jGRASP will allow you to compile your program again.
Finding and fixing compiler errors can be difficult. A description of the compiler errors can be found by scrolling up in the "Compiler Messages" window. Unfortunately, the descriptions presented are not always that clear. If you cannot decipher the error messages you receive, ask an instructor for help. We would suggest always focusing on the first (top-most) error message since the first error can sometime cause the compiler to become confused and generate many erroneous error messages after the first one.

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.