The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION II - A Stand-alone JAVA Program

Step 3: Classes for Encapsulation


Let's first have a class.

A class is used as the ENCAPSULATION block in JAVA. It begins with the opening curly brace { and ends with the closing curly brace }. That is the equivalent of BEGIN and END in other languages.

Other blocks in JAVA also use the curly braces as delimiters. For instance, a method is defined also within curly braces, as well as constructs, such as 'if' statements and 'for' loops.

See a JAVA class below. The class name is Exercise.

class Exercise
{
    //
    // in here we can have a method defined or attributes
    //
}

COMMENTING-OUT code:

Notice that the double forward slahes '//' are used to make comments on any code line. If we want to comment out a complete block, then we can use the '/*' at beginning and '*/' at the end.

Suppose we have to comments lines 100 to 120 (perhaps we are testing, and there is code there that we don't want to use temporarily), then the code may be something like this:
097     .....
098     .....
099     .....
100     /* if (flag)
101      {
102                  e=new Track();
103                 ....
104                 ....
105      }
.
.
.
120     */
121     .....
122     .....
In code above lines 100 through 120 are commented out. That's the block between the /* and */ .

Click here to go to the JAVA Mini-Course page

The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION II - A Stand-alone JAVA Program

Step 4: The main Method


Well, now that we have a class, let's then have the main method in the class. The main method uses some modifiers which will be discussed later. The name of this main method is 'main'. The modifiers are: public, static, and void.

Any JAVA method will have a set of parenthesis used for parameter passing. In the case of the main method the parameter passed comes from the command line, and it's an array of the String class.

Here below see the class Exercise with the main method defined in it.

class Exercise
{
    public static void main(String[] args)
    {
        // here the main method is defined. (code, attributes, etc.)
    }
}

Click here to go to the JAVA Mini-Course page

The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION II - A Stand-alone JAVA Program

Step 5: Printing-out to the Standard Console


In order to have an output on our video console, we can invoke a JAVA method that is used to do exactly that. It's the "println()" method referenced by the "out" field of the System class. The out field is an outstream of the "PrintStream" class.

To invoke this method we write: System.out.println(x);

// where x can be a primitive data type or String.
Notice that the String that we want to be outputted to the video terminal is within the set of parenthesis. That String is the parameter passed to that method.

For example to display "Hello" to the console we write: System.out.println("Hello");

To continue on our example, let's have this output method invoked from withint the main method. When this code is compiled, and executed, it will display "Hello" on your console.

class Exercise
{
    public static void main(String[] args)
    {
        System.out.println("Hello");
    }
}

A pair of curly braces is used to encapsulate the class, and another pair is used to delimit the main method.

The 'ln' in 'println' stands for line. It places a carriage return/line feed by the end of the string displayed.

If we write 'print' without the 'ln' at the end, that's another output method which also can be used. That method print() does not display a carriage return/line feed by the end of the string.


Semi-colon to end a line of code

Another important observation is the fact that after we write a line of code, we end it with a semi-colon ";" .

In above example the line of code is "System.out.println("Hello");"

Click here to go to the JAVA Mini-Course page

The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION II - A Stand-alone JAVA Program

Step 6: Methods for Keyboard Input


JAVA has I/O methods for input as well as output. The I/O methods are contained in classes which are part of the IO package of JAVA.

We already have seen the method utilized for console output. That is, System.out.println(x); where x can be a String, or any primitive type.

Many methods contained in classes of the IO package raise Exceptions. Invoking methods that raise Exceptions may be handled in different ways. We will see later in Section XII of this Mini-Course, Exception Handling and we will have a better idea of the way Exceptions can be handled in JAVA.

For Keyboard Input we will use some methods contained in the Stream classes which are part of the IO package and will need to handle IOException. We will see the Stream classes in later Section.

However, for the time being we can utilize a class that we developed that handles those raised IOExceptions, and can be used for Keyboard Input. That class will let you enter all data primitives and their corresponding arrays (except array of chars), as well as any String. More about String and arrays in a later Section.

We name this class, Input.class and you can save it by clicking your mouse right button on it. If you want to use it, make certain that Input.class is in the same directory where your other JAVA files are.


Input.class methods and examples

In the Table given below you find the various methods that can be used from your JAVA program, when you are using our Input.class. An example is given for each method. Notice that any method used is preceded by the class name Input and a dot. This is because all methods of this class are class methods. ('static') We'll get into class methods in a later Section of this Mini-course.

For instance, to invoke the method that lets you enter an 'int' from the keyboard, you can write:

int myInteger = Input.enterInt(); // where enterInt() is the method.

Class Method

Entry type

Example

Comments

enterInt()

int

int year = Input.enterInt();

An int is entered.

enterChar()

char

char letter = Input.enterChar();

A char is entered.

enterShort()

short

short month = Input.enterShort();

A short is entered.

enterByte()

byte

byte number = Input.enterByte();

A byte is entered.

enterLong()

long

long atoms = Input.enterLong();

A long is entered.

enterFloat()

float

float value = Input.enterFloat();

A float is entered.

enterDouble()

double

double amount = Input.enterDouble();

A double is entered.

enterBoolean()

boolean

boolean flag = Input.enterBoolean();

A boolean is entered.
(true or false).

enterString()

String

String name = Input.enterString();

A String is entered.

In a later Section we'll provide the methods (also in this Input.class) that can be utilized to enter arrays.

Click here to go to the JAVA Mini-Course page