0% found this document useful (0 votes)
17 views

PF101: Laboratory - 1 Getting To Know The Programming Environment

Uploaded by

james blake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

PF101: Laboratory - 1 Getting To Know The Programming Environment

Uploaded by

james blake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

PF101:

Laboratory_1

Getting to know the programming environment


• In this module we are going to know the programming
environment of Java. We are also going to discuss on
how to write, compile and run Java programs using
Netbeans IDE (Integrated Development Environment) and
Text Editor.
At the end of this lesson you should be able to:
- Write simple Java code in Netbeans.
- Compile and run Java code
- Differentiate the three types of error, the syntax errors,
runtime errors and logical errors.
A. The “Hello World!” application, your first Java
Program
• In this section we are going to write, compile and run a
Java program in Text Editor and Command Line. We are
going to create a simple program that will display the
words “Hello World!”. Below is the source code of our first
program:
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
Before we can run any Java program, we will need the
following:
- Java Standard Edition (SE) Development Kit 8 (JDK8).
- A text editor, an example of this is Notepad.
Let’s start creating the “Hello World” program. The
following are the steps to create the program:
Errors

• The example source code that was provided in this


module is free from error, if it happens that you still
encountered error during compilation aside from
unrecognized JavaC you probably got a syntax error and
this section will be able to help understanding your error.
There are three types of error that you might encounter in
Java programming:
There are three types of error that you might encounter
in Java programming:
• Syntax errors
• Runtime errors
• Logical errors
Syntax Errors
• The errors occur when the syntax of the language is
violated, specifically when a word or symbol was not
correctly placed in an instruction or statement of the
program
• Misspelled keyword, variable and method names or
incorrect used of capitalization
• The figure above demonstrate a program that has syntax
errors. After compilation of the program, the compiler
encountered two errors. The first error message tells us
that we had an error on line 1 of our program and it is
pointed on the word publix, which is a misspelled word
for public.
The second error message suggests that we had an error in
line 6 and pointed in between of the words Public and
static, as you can see the first letter of the word Public is in
upper case and it is expected to be in lower case.
• Missing semi-colon at the end of the statement or
incorrect used of symbols.
violations are, first the use of comma (,) instead of dot(.)
and the second one is missing semi-colon at the end of the
statement. You can compile the program above to see the
generated error message.
• Brackets such as curly braces “{}”, parentheses “()“ and
square brackets “[]” is not properly matches. Never forget
to enclose your brackets, make it a habit to type the
brackets in pair (opening and closing bracket).
Runtime Errors

• Runtime errors only occur after compilation (when there is


no syntax error in the program) and running your
program. Runtime is when the program is running
therefore you can only encounter runtime error during
execution of the program or when you are using your
program. The common examples of these are, trying to
open a file, but the file doesn’t exist or is corrupted and
when you try to execute division by zero.
The figure above is a sample
program that has no syntax
error, but will produce runtime
error and it has a statement that
performs a division by zero. The
figure below shows that the
program was successfully
compiled and didn’t produce
syntax error, however when we
tried to run the program, we
encountered runtime error. The
message tells us that we are
executing division by zero,
which is not possible or
incorrect.
Logical Errors
• Logical errors also occur once the program is in use and
these errors will not interrupt the execution of the
program. Errors are those where the program is running
smoothly, but produces unwanted or unexpected result
from what you designed. The common examples of these
are:
• Incorrect use of mathematical operation like using of +
symbol for subtraction.
• Displaying the incorrect message.
• Using data from incorrect source
• The figure on the next slide is an example that
demonstrates a logical error.
• As you can see there is no error message being shown
or the program was not interrupted, however we can
notice that there is something wrong in the output, we
want the program to show the addition of “2” and “2” but
instead it displayed the concatenated number “22”.
• Logical errors are considered the most difficult type of
errors to fix because it is not clear where the errors
originate since it didn’t display any information about the
error.
Using NetBeans
• In this part of the lesson we are going to discuss writing
and compiling Java program using NetBeans. Netbeans
is an Integrated Development Environment or IDE, it is a
programming environment that provides comprehensive
facilities to programmers for Developing Java programs.
NetBeans contains a source code editor, GUI Builder,
Compiler, interpreter and a debugger.
• Step 1: Start NetBeans
• To open the NetBeans program, double click the
NetBeans shortcut icon in your desktop.
• The figure below shows the graphical user interface (GUI)
of the NetBeans IDE.
Step 2. Creating a project.
To create a project, in the IDE
menu bar choose File then click
New Project.
• After clicking the New Project, a New Project dialog bar
will show up. On the category list, select Java and on the
Project list, select the Java Application, then click the
Next button.
• In the New Application Dialog, do the following (as shown
in the figure below):
• In the Project Name field change the value to
JavaPrograms.
• Leave the default value of the Project Location, by
default our project is located in C:\Users\<user>\
Documents\NetBeansProjects directory and all the files
will be saved in C:\Users\<user>\Documents\
NetBeansProjects\JavaPrograms.
• Leave the Use Dedicated Folder for Sharing Libraries
unchecked
• On the Create Main Class field, enter “HelloWorld” and
then click the Finish button
• The Java Application project is now created and opened
in the IDE. The IDE will have the following components:
• Projects window displays all projects loaded in the IDE
and it is presented in a tree view. This window shows the
components of the project such as source files, libraries,
etc.
• Source Code Editor Window where you can write Java
source code .
• Navigator window where you can navigate elements
within the selected class.
• Step 3. Writing your program in NetBeans IDE.
Since you have left the Create Main Class checked in the
New Application dialog the NetBeans IDE have generated
HelloWorld.java file that contains code that is shown in
the source code editor window.
• Step 4. Compiling and Running your program in
NetBeans IDE.
NetBeans IDE has Compile on Save feature, it
automatically compiles the Java source file after saving.
Therefore, we don’t need to manually compile the project
in order to run it.
• NetBeans IDE has a real time syntax error checker, the
error icon (red glyph in the left margin) and red underline
on the statement will automatically show up if you typed a
misspelled keyword or forgot to enter the required symbol
(anything that violates the syntax).
• There are three ways to run your program in NetBeans
IDE
1.Select the Run on the menu bar then click Run Project.
2. Click the Run Icon on the Tool bar
3. Press F6
• The output of the program will be shown in the output
window of NetBeans IDE.
LESSON SUMMARY:
1. There are three types of error in java: syntax errors, run
time errors and logical errors.
2. NetBeans IDE is a programming environment that
provides comprehensive facilities to programmers for
Developing Java programs.

You might also like