Beginning Java

Unlike other programming languages, Java lives and breathes on the web.
Primary and current sources of documentation are available on-line.
Follow the Links link . . .

Our Goal:
Get this example Java code up and running using a visual Java debugging tool ...

///////////////////////////////////
// File: question/QuestionTest.java
package question;

abstract class Question
{
  public Question( String _text )
  {
    theText = _text;
  }
  public abstract void askTheUser();
  protected String theText;
}

class YesNoQuestion extends Question
{
   public YesNoQuestion( String _text )
   {
     super( _text );
   }
   public void askTheUser()
   {
     System.out.println( theText );
     System.out.println( "YES or NO ...?" );
   }
}

class FreeTextQuestion extends Question
{
   public FreeTextQuestion( String _text )
   {
      super( _text );
   }
   public void askTheUser()
   {
     System.out.println( theText );
     System.out.println( "Well...?  What’s the answer...?" );
   }
}

public class QuestionTest
{
  public static void main( String[] args )
  {
    Question[] questions = getQuestions();
    for( int i = 0; i < questions.length; i++ )
    {
      questions[ i ].askTheUser();  //  !!!!!!!!!!!
    }
  }
  private static Question[] getQuestions()
  {
    Question[] qs = new Question[ 2 ];
    qs[0] = new YesNoQuestion( new String( "Are you happy?" ) );
    qs[1] = new FreeTextQuestion( new String( "Why or why not?" ) );
    return qs;
  }
}
// -- End of File.

1) Put the source code (copy - paste it out of this HTML page) into a file called QuestionTest.java.  There is only one public class in the file, and it is called QuestionTest; Java demands that every file has exactly one public class, and that the name of the file matches the name of the class.

2) Create a root directory for all of your Java code, I'll assume for the sake of having an example, that your root directory is C:\java.

3) Create a directory for the question package: C:\java\question.  Java packages are hierarchical in the same way that file system directory structures are hierarchical; Java package structures must have parallel directory structure.  In this case, there is a question package, so we need a question directory.

4) Put the file QuestionTest.java in the C:\java\question directory.

5) Download the Java Development Kit (JDK) version 1.3.1 (Java 2 Platform Standard Edition) from the web: http://java.sun.com/j2se/1.3/
Install it.  I’ll assume that you installed it into a directory named C:\jdk1.3.1.
Make sure your PATH environment variable has: C:\jdk1.3.1\bin

6) Make sure that your CLASSPATH environment variable has: C:\java; From there, the Java system will be able to find the question package and the classes contained within it.

In general, if your PATH and CLASSPATH environment variables are not set up right, your JDK will not work properly.  Setting them is a bit different on each platform:

On Windows 95/98:  Open the autoexec.bat file and make sure that:  The PATH variable includes: C:\JDK1.3.1\bin and the CLASSPATH variable includes: C:\java

On Windows NT:  Right click on the My Computer icon, and select 'Properties.' Click on 'Environment.'  The System Properties window appears.  Select PATH from the list of System Variables and ensure that it includes:  C:\JDK1.3.1\bin  Then select CLASSPATH from the list of System Variables and ensure that it includes: C:\java  Then click 'Set.' Then click 'Apply.'

On UNIX and LINUX: Ensure that your 'cshrc' or 'profile' file includes the paths in its PATH and CLASSPATH definitions.  For example, in cshell and bourne shell, there should be lines like the following (additional directory paths can also appear in the parentheses, with spaces separating the paths from one another):  set PATH = (JDK1.3.1/bin) and set CLASSPATH = (java)

On the MAC: Click HERE, and follow the instructions, in lieu of steps 7 & 8 below.

7) Using a DOS or UNIX shell, go to the directory: C:\java\question; type "javac -g QuestionTest.java"  to compile the .java file (with debug turned on: “-g”).  If you have the JDK installed correctly, this will work and will create several .class files, one for each class in the file.  javac is the java compiler program.

 8) Once you have the .class files in C:\java\question, then go to C:\java and type "java question.QuestionTest" to invoke the Java virtual machine and run QuestionTest.main().  Note that Java uses the . instead of a / or \ for the path name.  You should see some interesting output...

9) Get a debugging tool.  The debugger that comes with the JDK works fine, but does not have a visual user interface…  It really helps a lot when developing code to use a visual debugging tool.

Here is some help on Using Java on CU lab machines.

-- A free, quality visual Java debugging tool: JBuilder Foundation.
This tool provides a complete integrated development environment (IDE) for Java.

-- Use any other convenient Java visual debugging tool.

-- Note: You do not really need any other features provided by the IDE (Integrated Development Environment).  All you need is the JDK commands and a visual debugger.

When your visual debugger is installed correctly you should be able to set breakpoints in the code and to see the values of variables at run-time.

10) Step through the code in the debugger as it runs, line by line.  Pay special attention to what happens when you “step into” questions[ i ].askTheUser();  Hint - it is a polymorphic method call.