A Java Course Outline
Using the Java By Dissection book
by Ira Pohl and Charlie McDowell

 



Basic Java programs on the Mac

Introduction

Java is touted as the next big thing. From enabling dynamic web content to creating simple user interfaces to creating database front ends, Java seems to be here to stay. Javas popularity is in no small part also due to its ease of use and accessibility, these qualities also lend themselves to basic programming courses, such as those given to first year CS students or in the courses given to natural science and business students wanting some programming know-how.

Javas cross-platform nature is also not lost on professors outlining new courses. Why spend three weeks of the semester teaching natural science or business students the Unix they will need to get their programs up and running when they can use a graphical development environment on an OS they are already familiar with (such as Mac OS). These some of the reasons are why we are beginning to see more and more beginning programming course being taught in Java.

Teaching you how to program in Java is beyond the scope of this article. If you are trying to learn Java, there are many great resources on the web and in bookstores. It has been said, almost in jest, that there are currently more books on how to program in Java then there are Java programs. One good source is at Apple Computers developer website as well as others I will link to at the end of this article. If you already know C or C++, the one set of books I can really recommend is OReillys Java In A Nutshell and Java Examples In A Nutshell.

What you will need: In order to do your Java homework on the Macintosh, you will need:
  1. A Power Macintosh computer with at least 64 MB RAM (more is better) running at least MacOS 8.1
  2. A copy of Metrowerks Codewarrior for Java Student edition. This should have come with your ADC student membership (you are a member, arent you? If not, it is worth the small price asked, if only for the Codewarrior CD). Otherwise, you can use any Codewarrior with Java from Pro4 or greater. All examples will be shown with the academic version of Pro 4, though it should be almost exactly the same as in Pro 5.
  3. Macintosh Runtime for Java (MRJ) whatever the latest version happens to be.
  4. The MRJ SDK from Apple
  5. An assignment that you need to do for class. I will provide one if you need something to focus you, or you haven't gotten your first assignment yet.
Lets Go!

I am going to give myself a hypothetical first assignment for a java programming course. This will be easier than most, but it will give you a very basic foundation from which to grow.

The Assignment...

You will write a Java program that takes one or fewer arguments. The program, based on the presence or absence of that one command-line argument will either print out "Hello world" or "Hello <argument>," this assignment will familiarize you with your development environment.

Now, I am assuming that you have Codewarrior installed and running at this point... if not, I wait... OK, are you ready? The first thing to do is to create a new project. Choose the Application template to start with. Ive named mine JavaTest. The project window will open and will come with an initial source file called TrivialApplication.java. This is a simple Hello World class, that is much like how we want ours to end up. But for now, we will create a new document. And enter in the code for our homework.

/*
	MyTestClass.java
	ęDoug Whitmore 1999
	
	A simple Hello World type of program for java
*/

public class MyTestClass
{
	public static void main(String [] args)
	{
		if (args.length != 0)
		{
			System.out.println("Hello " + args[0]);
		}else
		{
			System.out.println("Hello world!");
		}
	} //end main()
}//end class

As you can see, this is a really simple application. However, this will give you a good jump start in using Codewarrior and the Macintosh for doing Java homework. This is interesting, because on the Mac, we dont have a command line. Even Windows has a command line in DOS. Later we will deal with that issue. For now, add your new window to your project. Your project window should look like this now:

project window

If we were to build this project as is, it would not run our main but the one from TrivialApplication.java. In order to change that, we need to go to Project Preferences and change a setting. Go ahead and open the Project Settings window and go to the Java Target pane. Change the Main Class text field to be the name of the class that has your main method, in our example it is MyTestClass. This is equivalent to the argument you give to the java program when you invoke it on the command line. As an example, if I were to run this class under a command line, I would type java MyTestClass . Here is how your preferences should look after this:

main class name edited


Go ahead and build and run the app. Youll notice that a console comes up and prints out "Hello World!". The first part of our program works fine. Now we need to figure how to test it with a command line argument. If we were in a command line environment like Unix or DOS, we would simply type java MyTestClass Doug to test the program. Its not exactly that easy with the Macintosh, but close. Go back to the same preference panel as before. Now we will be editing the other text field, giving it our command line argument[s]. It should look as the following picture:

 

parameter edited

 

What you should end up with after running it like this is the following picture:

running

 

Congratulations! Youve completed your first assignment. Now, this next step isnt as important with Java as it is with other languages. But I always sugegest very strongly that you test your creation on the same type of machine that it will be graded on. Nothing is worse than being told that the program you stayed up all night completing didnt work because some difference between your setup and the TAs caused it not to build or run correctly. At least for your first few assignments, DO NOT SKIP THIS STEP. Very few TAs will be as kind as I am with students in understanding this sort of thing... and I only tolerated it once per student. So make sure you test it on the schools system before you turn it in finally. If you dont, it will bite you, I promise.


The next step...

Now that you have your first assignment done, what is the next thing that usually happens in a beginning programming course? Accepting input. One of my major beefs with Java is its lack of a simple input method. What I want is a method as simple as "System.out.println" is for output. Luckily, most professors realize this, and many will make classes for doing this available to you. But how do you use them in your Macintosh project? In your class labs, you usually just need to include the path to the new classes in your classpath on the command line. In MacOS, it is even easier, though not obvious if you are used to this paradigm of classpath.

For my example, I will be using a set of classes from professor Pohl, for whom I was the TA last year. He calls the package tio but is looking for a better name. For the homework, I will be following this exercise:

Using the tio package for input, write a program that asks the user how old they are. Accept the integer they give you, and do some math on it (given that this year is 1999 or 2000) to return to the user what year they were born in. You do not have to account for months in this, just an approximation of the results is necessary.

Lets go ahead and use the same project from last time, just adding the new source file which I will call "MyTestClass2.java" The code I entered in follows, notice the addition of the import tio.*; call. Later I will show you how to resolve that import. For now, here is the code:

/*
	MyTestClass2.java
	ęDoug Whitmore 1999
	
	A simple Input test program for java
*/

import tio.*;

public class MyTestClass2
{
	public static void main(String [] args)
	{
		System.out.println("How old are you? ");
		int age = Console.in.readInt();
		
		int year = 1999 - age;
		
		System.out.println("That means you were born in " + year);
		
	} //end main()
}//end class

Now add that to your project. If you try to build the app now, Codewarrior will give you an error about not being able to find the package "tio." In any other programming environment, you would need to find the classpath, find the classes ro zip file to add, and then type that path into the classpath... I think thats a big pain. What you should do now, is download the classes or zip file from your professors web site. Put them in a reasonable place. I give each new package its own folder in my projects directory. Now, add them to your project! Its that easy. Here is an example of how my project looked after adding the classes I needed from the professor.

Added classes

Almost done, now I can compile it fine, but when I run it, it gives me some sort of strange end of file exception (ExceptionEOF). Whats going on here? The simple answer is that there is one more preference to set. We need to set it to take input from the console window. You do that by changing the "redirect stdin" flag to redirect from the console window. The panel should end up looking like this (this is from Pro 5, the previous were from Pro 4):

stdin settings

 

Now that you have finished these, go forth and do your homework on your Mac! Please dont hesitate to ask me any questions you may have, just dont ask me to do your homework for you. Ill be more than happy to answer all of your questions about doing your homework on the Mac, for other help, I charge ;)

Good Luck!

The original Webmaster for this page was Doug Whitmore and he owns the copyright 1999 for it. If you want to use this page for your site, please contact Doug at dwhitmore@acm.org

Feel free to report any site problems to the Webmaster, Debra Dolsberry.