Cpsc 235 - Copyright (C) 2003 Katrin Becker Last Modified September 8, 200510:44 PM

Beginning Java - Part II
Creating and Running Programs, Simple I/O, Program 1, Program 2, Javadoc

Creating and Running Programs

Java distinguishes between an "application", which is a stand-alone program, and an "applet", which is a program that is executed as part of a (usually) web-page. For now, we will concentrate on applications. The basic "main program" consists of a single class definition in one physical file. Usually, the class and the file will have the same name. So the program "calc.java" will contain the class called "calc". It must be defined as follows:
public class calc
{
/*** all the stuff [ EVERYTHING ] goes here ***/

} // end class calc

Inside this "main" class there must be at least one "method" (function) and it must be called "main" and it must be defined as follows:
public static void main( String[] args)
{
/*** your main program goes here ***/
} // end main

So, together, they look like:

public class calc
{
public static void main( String[] args)
{
/*** your main program goes here ***/

} // end main

} // end class calc

It is traditional for classes to be organized as follows:
inside file: XXXX.java

/* declare needed library routines here */

/** javadoc documentation goes here */
public class XXXX
{
/* all public methods (functions) */
/* all protected methods (functions) */
/* all private methods (functions) */
   
/* all public attributes (variables) */
/* all protected attributes (variables) */
/* all private attributes (variables) */

} // end class XXXX




"Simple" I/O using the Package tio

The text introduces a relatively simple I/O package for doing terminal I/O. Java I/O can be awkward. The package consists of a number of classes but the ones we are most interested in at the present are: ReadInput and FormattedWriter
Package TIO: Class Hierarchy
class java.lang.Object
class tio.Console
class tio.ReadInput
class java.lang.Throwable (implements java.io.Serializable)
class java.lang.Exception
class java.lang.RuntimeException
class tio.ReadException
class java.io.Writer
class java.io.PrintWriter
class tio.FormattedWriter
class tio.PrintFileWriter

In order to use the package, we must import it into our program like this:

// put this at the beginning of the file
// outside of the class definition
import tio.*;

'tio' is the name of the package and we are saying we want access to all of it. If we have the package locally in our working directory, that is all we need to do. The same holds true for any standard java packages as well since the system has a set of rules for how to find these things. To make this work, what you need is a local copy of the directory called "tio" which contains all the .class files for that package.

Roughly speaking, Console.in corresponds to standard input and Console.out corresponds to standard output. Both exist throughout the life of our program and once we import TIO, we don't need to declare them explicitly. When we say import tio.*;, all of the 'componenets' of TIO come along for the ride.


Let's look at the functions we can use from this package. In ReadInput, we have:

return type

boolean

hasMoreElements()

Check to see if there are any non-white space characters left in the input.

int

readChar()

Read the next character.

NOTE: the following routines don't skip white space. They assume we are at the start of the entity we want to read. (they choke if they are currently pointing to whitespace). They assume they are at the end of the thing they are trying to read when they reach whitespace.

double

readDouble()
Attempt to interpret the next white-space delimited input characters as a double [big real]

float

readFloat()
Attempt to interpret the next white-space delimited input characters as a float [regular real]

int

readInt()
Attempt to interpret the next white-space delimited input characters as an int [integer]

String

readLine()
Read the next complete input line up to the newline character

long

readLong()
Attempt to interpret the next white-space delimited input characters as a long [big integer]

String

readWord()

Read the next white-space delimited string.


. In FormattedWriter, we have:
printf(String s) Print a string in a field of the current width using the current padding character and justification.
The next set of methods convert their parameter to a string and then pass that to printf(String s)

printf(boolean value)

printf(char value)

printf(char[] value)
printf(double value) Print a Double in a field of the current width, with the current number of digits to the right of the decimal point and using the current padding character and justification.
printf(float value)
Print a float in a field of the current width, with the current number of digits to the right of the decimal point and using the current padding character and justification
printf(int value)
printf(long value)
printf(Object value)
The next set of methods use printf() followed by a call to println() to add the newline to the formatted output from printf().
printfln(boolean value)
printfln(char value)
printfln(char[] value)
printfln(double value)
printfln(float value)
printfln(int value)
printfln(long value)
printfln(Object value)
printfln(String s)
setDigits(int places) Set the number of digits to be printed to the right of the decimal point in floating point values.
setJustify(int leftOrRight) Set the justification to be LEFT or RIGHT.
setPadChar(char pad) Set the character to be used in padding.
setWidth(int width) Set the ouput field width.

Program 1 file: SimpleInput.java
// SimpleInput.java - reading numbers from the keyboard
import tio.*; // use the package tio
class SimpleInput {

  public static void main (String[] args) {

    int width, height, area;

    System.out.println("type two integers for" +
              " the width and height of a box");
    width = Console.in.readInt();
    Console.in.readChar(); // skip single space inbetween
    height = Console.in.readInt();
    area = width * height;
    System.out.print("The area is ");
    System.out.println(area);
   }
}

Program 2; file: Ring.java
// Ring.java - arrays and output
import tio.*; // use the package tio
class Ring {
  public static void main (String[] args) {
   String Song[] = {"One","Ring","to","Rule", // 0-3
                    "them","all","find","bring", //4-7
                    "and","in","the","darkness", // 8-11
                    "bind","\n"}; // 12-13
   int sing[] = {0,1,2,3,4,5,13,
                 0,1,2,6,4,13,
                 0,1,2,7,4,5,13,
                 8,9,10,11,12,4,13};

   final int MAX = 27;

   for (int i = 0; i < MAX; i++)
   {
     System.out.print(Song[sing[i]]);
     System.out.print(' ');
   }
   System.out.println();
  }
}

Javadoc

/** ... */
extracted from:
package
public class
interface
method
variable
constant
TAGS:
@since text // "since" entry
@depreciated text // old java; should suggest a replacement
@see link // package.class#feature label
// <a href=" .... ">label</a>
// "text"
Class and Interface:
@author
@version
Method:
@param var descr.
@return descr.
@throws class descr.



Cpsc 235 - Copyright (C) 2003 Katrin Becker Last Modified September 8, 200510:44 PM