Computer Science 105 Lab 1: WRITING AND RUNNING JAVA PROGRAMS
Due: January 25, 2002

Objectives and Overview: In this lab, you will become familiar with the process of typing, editing, compiling, and running a simple Java program using CodeWarrior Java on an iMac computer in the Searles 128 lab. The following program appears on page 26 of your text.
//********************************************************************
//  Lincoln.java       Author: Lewis and Loftus
//
//  Demonstrates the basic structure of a Java application.
//********************************************************************

public class Lincoln
{
   //-----------------------------------------------------------------
   //  Prints a presidential quote.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}

Step 1 - Creating a Project and a Java Program

CodeWarrior is a software environment that supports Pascal, C, C++, Java, and Assembly language programming.  In this course, we will write and run Java application programs and applets using CodeWarrior.  An application program is one that runs in a conventional way, while an applet is one that runs on a Web bruwser.  For the time being, we will concentrate on designing application programs.  Later in the semester, we will design applets.

Files and Folders Used by CodeWarrior

CodeWarrior allows large programs to be written as one or more files. When you first start a program in CodeWarrior there are three different kinds of files to keep in mind:

These files are all kept in a "project folder."  For example, myProject is the name of your project folder, while myProject.mcp is the name of the corresponding project file.  The project folder is a regular Macintosh folder that holds all the required files for a single programming project.

To simplify project creation, there is a generic project called myProject on the server Course Libraries -> Computer Science.  Locate this project by double-clicking on the Computer Science folder and then the CS105 (Tucker) folder inside it.  Enter your Bowdoin login name and password to gain access to the contents of this course folder.  Now drag a copy of the folder myProject to your iMac desktop.

Notice that there are other folders inside the CS105 (Tucker) folder:

Open the Project

Open the folder myProject on the desktop and double-click on the file myProject.mcp.  The following window should appear:

This project has a folder called System Classes. If you click on the triangle on the left, you will see two library files "Classes.zip" and "swingall.jar" and a Java source file "keyboard.java".

Just to make sure you understand the difference between a project folder, a project file, and a Java program, take a minute to look at the contents of the folder myProject.  It should contain the following files:


 
 
  Create Your Own Program for This Project

You now need to create a separate file that contains the program for your project. Under the File menu, select the
File --> New Text File option. This will open a new window with the heading untitled. Use the Save As command under the File menu to give the program file a name and store it inside the myProject folder on your diskette. This dialog box is illustrated below.

You must choose a program name that ends with the suffix ".java " (like "Lincoln.java" in the picture above).

Now add this new program file o the project myFirst.mcp, using the following steps:

  • Select the Add Files... command in the Project menu. A dialog box will appear and the name of the program Lincoln.java will be in the list for the myProject folder.
  • Select that program, and then click Add to add this program file to the project.
  • Finally, select myProject Debug Settings under the Edit menu, and a settings window will appear. You should make the following change before closing this window:  select Java Target and type Lincoln as the Main Class.
  • Now click "Save" to save this change. Your resulting project window should now look like this:


     

    Type the Program Text

    Go back to the program window by double clicking on the file name Lincoln.java in this project window. You are now ready to type a program, such as the simple Java program shown at the beginning of this document.

    You should periodically use the Save command under the File menu while you are typing a program. This precaution gives you added insurance against unexpected (though infrequent) system failures. When you are finished typing, save the complete program.

    Compile and Run the Program

    Once you have completed and saved your program, it needs to be compiled (checked for syntactic errors) before it can run. This is done by selecting the Make option in the Project menu. If your program contains any syntactic errors (that is, grammatical, spelling, or punctuation errors), a window will open and identify the nature and location in the program of each error.

    To run the program, select the Run command under the Project menu. (A speedy way to compile and run a program is to use the cloverleaf-m and cloverleaf-r commands in place of Make and Run , respectively.) These two steps start the separate application called MRJApplication, which will in turn open the new window and run your program, as shown below:

    The window Java Console is generally used for typing input and displaying output while the program runs. Notice that it can be resized in the usual manner (use the mouse to grab and drag the lower righthand corner).  For this particular example, there is no input and the output is a two-line message.

    When finished running the application MRJApplication, select Quit under the apple menu; this will close the Java Console window and terminate the run.

    Print the Program

    To print, make sure that the window you want to print is at the front of the screen. This will usually be the program window, but sometimes you might want to print an output window. Then go to the File menu and select Print.  This will print a hard copy of your program text on the printer at the front of the lab in 128 Searles.

    If this doesn't work, check the Chooser (from the apple menu) to see that the printer is set correctly to print in the lab where you're working. It's also a good idea to make sure that the printer itself is turned on before you try to print.

    Some printers will allow you to change the print format to save some paper. Before you print, go to the File menu, and select Page Setup . The "2up" layout will print your program using half as much paper (but in a smaller font).  You can also use the Page Setup menu to print in landscape orientation (rotated 90 degrees), which can be helpful if your program has very long lines.

    Step 2 - Experiment with Program Errors

    Now try returning to the editor and creating a compile-time error in your program.  This is done by introducng an error in the program's syntactic structure, and is often called a syntax error). For example, remove the word System. from the line:
    System.out.println ("A quote by Abraham Lincoln:");
    and Make the program again.

    Notice that a window with error messages appears, and that double-clicking on an error message in that window indicates (by a red arrow) the line in your program where the error occurred (and the highlighted characters locate the position of the error within that line).

    The error you have introduced into your program is an example of a compile-time, or syntax error.  The semantics (meaning) of the program may be as you intended, but your program does not satisfy all the required Java syntactic constraints that are needed for the compiler to translate it into a runnable program in the machine language of the computer.  This is similar to writing a sentence in English and failing to capitalize the first word.

    Now correct this syntax error by retyping the text you removed.  Then add the following lines of code immediately before that line.

          System.out.print("Enter an integer: ");
             int n = Keyboard.readInt();
             for (int i=n; i>0; i--)
                System.out.println(i);
    and the following line at the top of the program:
    import cs1.Keyboard;
    Now when you run the program and the program asks you to enter an integer, try entering some nonnumber, like "Hello".   What happens?    You should get something like the following:

    Enter an integer: hello
    Error reading int data, MIN_VALUE value returned.

    This is called a runtime error.That is, although the program has no syntax errors, it is unable to run all of its instructions properly because an unexpected event occurred (in this case, the user typed a non-number when an integer was called for).

    Just because all the syntax errors and runtime errors are eliminated from the program, its output may still not be completely correct for the problem it is designed to solve.  In the above program, typing an integer when you run the program will produce an output like the following:

    Enter an integer: 10
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    A quote by Abraham Lincoln:
    Whatever you are, be a good one.

    Step 3 - Write Your Own Program

    Use the ideas in the above program to design and write a program that solves Programming Project 1.4 on page 48 of your textbook.  The program file should be named lab1<yourname>.java (e.g., lab1atucker.java ) and its class name should be lab1.

    Step 4 - Leave CodeWarrior and Submit Your Program

    To end your CodeWarrior session, select Quit under the File menu. Be sure that the latest changes in your
    program are saved before quitting.  To permanently save a copy of your entire project for later use, rename the project Lab1(say) and then drag it into your personal folder inside the CS105 (Tucker) --> Users folder.

    To submit your Java program electronically, first rename it so that you are identified as the author (that is, your file with a name like lab1atucker.java ).  open the CS105 (Tucker) folder and drag your Java program file  to the Drop Box that appears there.  Be careful not to drag the entire project folder into the Drop Box -- only the program file that has .java as its suffix should be submitted.

    Lab 1 Questions:

    1. What is the general difference between compile-time errors and run-time errors?  Give a different example of each type of error from the one suggested in Step 2, using this same program.
    2. Complete exercises 10, 14, 16, and 17 on pages 46-47 of your text.

    Lab 1 Deliverables:

    By 5:00pm on the due date, drag your completed Java program for Step 3 to the CS105 --> Drop Box and hand in a printed listing of that program along with your answers to the above questions.  Your written work can be turned in at the instructor's mailbox outside Searles 222. Note: Even though the lab programs for this course are submitted electronically, you should always keep a backup copy of your own work, so that if a file is lost you won't have to retype it from scratch.