• Nem Talált Eredményt

Writing Text-Based Application Using Structured Approach

In document Programming Languages (Pldal 28-33)

WRITING JAVA PROGRAMS 1.4

1.4.1 Writing Text-Based Application Using Structured Approach

Apart from application, another type of Java program is Applet. Applet is a web-enabled program that will be introduced in Topic 9 because it is easier to understand applets after we have discussed features such as inheritance and methods. The examples of text-based application and Java applet will be illustrated in the next sections. Frame is not covered in the syllabus. Thus, we will skip any elaboration about Frame in this module.

Application Applet

Text-based application

Applet is a web-based program which supports GUI

Frame Java Program

Frame is non-Web stand alone GUI program

Figure 1.3: Types of Java program

1.4.1 Writing Text-Based Application Using Structured Approach

Text-based applications is stand alone program that can be written using structured approach or object-oriented approach. In this section we will discuss of how you can write text-based application using structured approach.

Program 1.1 shows an example of text-based application written using structured approach.

Program 1.1: Hello.java Line Number

1 // this program displays “Hello world”

2 class Hello{

3 public static void main (String args[]) { 4 System.out.println(“Hello world!”);

5 } 6 }

In structured approach, all the statements and instructions are dump into main method. Sometime, extra methods could be added to perform specific tasks. No object(s) is /are created when writing Java programs using structured approach.

The Program 1.1 above is described below.

(a) Line 1

Line 1 in the program is comments written by the programmer. It helps others to understand the nature of the program. Thus, the program becomes more readable.

(b) Line 2

Line 2 declares the name of the class as Hello.

(c) Line 3

Line 3 is where the program starts to execute. In this class, a method called main() contains the statements to display the „Hello world!‰ string. The main() method must be present in all Java applications because it is the first method that will be executed when an application runs (i.e. It is where the program starts to execute). The Java technology interpreter must find this defined exactly as given or it refuses to run the program. The following describes each element of line 3:

(ii) public: The method main() can be accessed by anything, including the Java interpreter.

(iii) static: This keyword tells the compiler that the main() method is usable in the context of the class Hello. No object is needed to execute static methods. (We will learn about object in Topic 5).

(iv) void: This keyword indicates that the method main() does not return any values (i.e.: There is no return keyword in the main() method).

String[ ] args: This method declares the single parameter to the main() method. The name of the parameter is args with the type of String array.

(d) Line 4

The statement System.out.println(“Hello world!”) in Line 4 in the body of the method will instruct the computer to display the message

„Hello world‰ upon execution.

(e) Line 5, 6

Line 5 and 6 contain two braces to close the method main() and class Hello respectively.

How to compile and execute Java application?

The following activity will guide you to compile and execute Program 1.1:

ACTIVITY 1.1

The purpose of this exercise is for you to compile and execute Program 1.1. It is compulsory for you to follow all the steps to ensure that you can learn how to compile and execute Java program using JDK.

STEP 1

By using notepad in Windows, type program 1.1 (without the line numbers). Then save it in a file with the name Hello.java in the correct directory (For JDK, the program is to be saved in jdk1.x.x/bin directory unless you have given a new name to this directory).

Note: x.x in jdk1.x.x refers to the jdk version installed in your computer. Latest jdk use abbreviation j2sdk1.x.x

STEP 2

Now enter the DOS window (also known as Command Prompt or C Prompt) and type DOS command cd:\ to enter directory C:\

Then, you need to enter to the directory that stores the program that you have typed in step 1.Use the DOS command cd to enter the correct directory namely jdk1.x.x/bin

STEP 3

Compile the Java program using the instruction javac Hello.java in the DOS environment. Example is shown below:

C:\jdk1.x.x\bin>javac Hello.java

If the error message is displayed, go to Step 4. If no error message was not displayed, go to Step 5.

STEP 4

Go back to notepad and open the file that you have typed in Step 1 (and at the same time do not close DOS window). Do the necessary correction in your program. Make sure you save your file after the corrections. Now go back to DOS window and repeat Step 3.

STEP 5

If there is no error, the compiler will generate the bytecode file ă that is Hello.class. Now you are ready to execute your program. In order to do this, type the command java Hello in DOS Window as shown below:

C:\jdk1.x.x\bin>java Hello

The program generates the output given below. It displays the string sent as a parameter to the System.out.println() method - the first statement in the main() method.

Take note that this instruction consists of two parts. The first part (java) refers to the Java runtime interpreter. The second part (Hello) refers to the class name which has the main() method that is to be executed by the interpreter.

1.4.2 Writing Text-Based Application Using

In document Programming Languages (Pldal 28-33)