19 Mei 2009

Getting to know Java Programming

GETTING TO KNOW YOUR PROGRAMMING ENVIRONMENT

2.1 Objectives

In this section, we will be discussing on how write, compile and run java programs. There are two ways of doing this, the first one is by using a console and text editor. The second one is by using NetBeans which is an Integrated Development Environment or IDE.

At the end of the lesson, you should be able to:

· Create a Java program using text editor and console in the Linux (Ubuntu Dapper) environment

· Differentiate between syntax-errors and runtime errrors

· Create a Java program using NetBeans

2.2 Introduction

An IDE is a programming environment integrated into a software application that provide a GUI builder, a text or code editor, a compiler and/or intepreter and a debugger.

This tutorial uses ubuntu Dapper as the operating system. Make sure that before you do this tutorial, you have installed Java and NetBeans in your system. Before going into details, let us first take a look at the first Java program you will be writing.

2.3 My First JAVA Program

public class Hello

{

/**

* My first java program

*/

public static void main (String[] args) {

//prints the string “Hello world” on screen

System.out.println(“Hello world”);

}

}

Before we try to explain what the program means, let’s first try to write the this program in a file and try to run it using a Text Editor and console. For this example, we will using a text editor to edit the Java source code. You will also need to open the Terminal window to compile and execute Your Java programs.

Step 1 : Start the Text Editor, to start the Text Editor, click on Applications -> Accessories -> Text Editor

Step 2 : Open Terminal, to open terminal in linux, click on Applications -> Accessories -> Terminal

Step 3 : Write the code in Text Editor and save your Java Program, and type your filename as “Hello.java”

Step 4 : Compiling your program, the next step is compiling your program, go to the terminal window we jusat opened a while ago. And then we go to the directory that containing file that we just created. To compile a Java program, we type in the command: javac [filename]. So in this case, type in : javac Hello.java

During compilation, javac adds a file to the disk called Hello.class

Step 5 : Running the program

To run your program, type in the command: java [filename without the extension], so int the case of our example, type in : java Hello

You can see on the screen that you have jusat run your first Java Program that prints the message, “Hello World”.

2.4 Errors

What we’ve shown so far is a Java Program wherein we did’nt encounter any problems in compliling and running. However, this is not always the case. As what we have discussed in the first part of this course, we usually encounter errors along the way.

As discussed before, there are two types of errors. The first one is a compile time error or also called syntax error. The second one is the runtime error.

a. syntax error

Syntax error are usually typing eroors. You may have misspelled a command in Java or forgot to write a semi-colon at the end of a statement. Java attempts to isolate the error by displaying the line of code and pointing to the first incorrect character in that line. However, the problem may not be at the exact point.

Other common mistakes are in capitalization, spelling, the use of incorrect special characters, and omission of correct punctuation.

Let’s take a example, our Hello.java program wherein we intentionally omit the semicolon at one statement and we try to type the incorrect spelling of a command. See the error messages generated after compiling the program. The first error message suggest that there is an error in line 6 of your program. It pointed to the next word after the static, which should be spelled as static.

The second error message suggests that there is a missing semicolon after your statement.

As a rule of thumb, if you encounter a lot of error messages, try to correct the first mistake in a long list, and try to comppile the program again. Doing so may reduce the total number of error dramatically.

b. runtime error

runtime error are errors that willnot display until you run or execute your program. Even programs that compile successfully may display wrong answers if the programmer hasn’t thougt through the logical processes and structures of the program.

2.5 Using NetBeans

Now that we’ve tried doing our programs the compilated way, let’s now see how to do all the processes we’ve described in the previous sections by using just one application.

In this part of the lesson, we will be using NetBeans, which is an Integrated Development Environment or IDE. An IDE is a programming environment integrated into a software application that provides a GUI builder, a text or code editor, a compiler and/or interpreter and a debugger.

Step 1 : Run NetBeans, there are two ways to run NetBeans. One is through command-line using terminal, or by just clicking on the shortcut button found on the desktop.

Step 2 : Make a project, let’s first make a project. Click on file-> New Project. After doing this, a New Project dialog will appear. Now click on Java Apllication and click on the Next button. Browse the address that we will save our project then. And type “Hello” as the main class name, and then click Finish button.

Step 3 : Type in your program, before typing in your program. NetBeans automatically creates the basic code for your Java program. You can just add your own statements to the generated code.

Step 4 : Compile your program, to compile your program, just click on build -> Build Main Project. Or, you could also use the shortcut button to compile your code.

If there are no errors in your program, you will see a build successful mesaage on the output window. And, after that Run your Program, then the output of your program is displayed in the output window.

Tidak ada komentar:

Posting Komentar