When you write a Java program, you will need to first create a project and then create one or more files that contain the text of that program. These files are all kept in the project folder. After you complete the development of your program, there will be additional files inside the project folder:
(Once you have a project defined and saved, you can just double click on that project file (with the .µ suffix) to restart CodeWarrior; this step is only necessary the first time you use CodeWarrior).
When CodeWarrior starts, the screen should have a menu bar that looks like this:
Optionally, you may open the "toolbar" by selecting Show floating toolbar under the Window/Toolbar menu. The toolbar contains shortcuts for many of the menu items, and it looks like this:
You might want to look at some of the items available on the pulldown menus, just to familiarize yourself with them.
Now click once on the right-pointing triangle next to Java, click once on Java Application to select it, and then hit the OK button, as shown below:
Now you'll get a dialog box similar to the following:
Be sure that your own diskette has been inserted in the drive on the Mac. Now use the folder names at the top of the window in the figure above to select your diskette (the one shown above is named "untitled"), where you want your work to be saved. A common mistake is to leave your work on the computer's own disk drive instead of taking it away on your own diskette -- be careful to avoid this.
Now name the project (the one shown above is named Lab1.µ, and the character µ is formed by typing option-M on the keyboard).
Save the project by clicking the Save button. After you've done this, a window like the one below will appear:
The figure above also shows that the newly created project has two parts, called Sources and Classes. If you click on the triangles on the left of these two parts, you willsee that the Java source program "TrivialApplication.java" and the libraries "classes.zip" and "System.in.zip" are prepackaged inside this project. Later, we will see how your own program can replace this Java source program
Just to make sure you understand the difference between a project folder, a project file, and a Java program, take a little detour to look at the contents of your floppy disk. It should contain a project folder called "Lab1" and double-clicking on that folder should show the following files:
You must choose a program name that ends with the suffix ".java " (like "myfirst.java" in the picture shown above).
Now we must add the program file we just created to the project Lab1.µ, and remove the TrivialApplication.java program that is currently there as a placeholder.
To add your program file, select TrivialApplication.java in the project window, and then select the Add Files... command in the Project menu. A dialog box like the one shown below will appear, and the name of the program myfirst.java will appear in the list. Clicking Add and then Done will add this program file to the project.
Now select TrivialApplication.java and click Remove Selected Items in the Project menu to delete this sample program that came with the project originally. Finally, select TrivialApplication Settings under the Edit menu, and a settings window will appear. You should make three changes in this window:
import java.io.*; import com.mw.io.*; public class MyFirst { public static void main(String args[]) { SystemInput sysIn = new SystemInput(); // open the input window try { ReadStream r = new ReadStream(); // create an input stream int n = 0; System.out.println( "Enter an integer in the System.in window"); n = r.readInt(); // read an input integer for (int i=n; i>0; i--) System.out.println(i); System.out.println( "Welcome to CodeWarrior Java!" ); sysIn.dispose(); // close the input window } catch (Exception e) { System.out.println("Exception " + e.toString() + " occurred"); sysIn.dispose(); } } }If this is your first program, be very careful to copy all the punctuation, tabs, and spacing exactly as they are shown.
As you type, notice that some program elements appear in different colors (comments appear in red and reserved words appear in blue); this is often helpful in finding and correcting syntax errors, and generally makes the program more readable.
It is important to know that Java is a case-sensitive language, so that capital letters are different from their lowercase counterparts. Ror example, the variable names MYNAME, MyName, and myname have distinct meanings in a program if they all appear. Reserved words in Java (the words that appeared in blue in the program you just typed) are always written in lowercase. Comments can have any mixture of upper and lowercase characters, and begin with the characters //.
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, save the complete program.
To run the program, first double-click the System.in.zip line in the project window. Second, select the Run command under the Project menu. (Note that a speedy way to make and run a program is to use the cloverleaf-m and cloverleaf-r commands for Make and Run, respectively.) These two steps start the separate application Metrowerks Java, which will in turn open the two new windows shown below:
The upper window, System.in is for typing input to the program. The other, Java Output, is for displaying output by the program. Notice that each of these windows can be resized in the usual manner (use the mouse to grab and drag the lower righthand corner).
For this particular example, you need to type an integer into the System.in window in order for the program to complete. For instance, if you choose to type the integer 5 in the System.in window, the Java Output window will look like the following when the program run is completed:
This means that the MetroWerks Java application is still running from your previous test run of the program, and must first be terminated (select Quit under File) before restarting it for your next run.
Try returning to the editor and creating an error in the text of your program, and try compiling and running the program again. Notice that a window with an error messages appears, and that double-clicking on the error message in that window takes you to the line in your program where the error occurs (the underlined word locates the position within that line of the error itself).
Can you figure out the meaning of the error message in light of the change you made to the program? The error you have introduced into your program is an example of a syntax error; the semantics (meaning) of the program may be as you intended, but your program does not follow all the Java syntactic requirements that are needed for the compiler to translate the program into an exacutable machine language program.
Before you print, you might want to check the Chooser (from the apple menu) to see that the printer is set correctly to print in the lab you're working in. In many labs, 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.
If you've been working on the desktop, don't forget to copy your files onto your own floppy disk.
To submit your program electronically, open the appropriate course folder for your class and drag your program file to the Drop Box folder that appears there. To find the course folder, look in the Courseware folder on the harddrive. (Opening one of the course folders causes a new icon to appear on your desktop.)