Prog Week 11
Prog Week 11
1
Fundamental Concepts of Java Programming Language
Java Installation
Java Standard Edition (SE) Development Kit is a software development
platform used in developing Java applications and applets. It includes an
interpreter or loader (java), Java Runtime Environment, a compiler (javac),
an archiver (jar) and document generator (javadoc) along with other tools
required in Java development.
To download and install Java Development Kit, follow these steps:
Step 1: Click this link or copy and paste it in your browser’s address bar:
http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-
138363.html
Step 2. You should see the screen below. Click the Download button under
Java. We will download Netbeans later.
Course Module
Figure 1. Download Java
Step 3. You will be redirected to this page. Scroll down until you see Java SE
Development Kit 8u11. Choose the compatible one for your operating
system.
Note: If you are using a 32-bit Windows OS, choose Windows x86.
Choose Windows x64 if you’re using a 64-bit Windows OS. Otherwise,
select the installer compatible for your operating system if it is Mac,
Solaris or Linux.
Step 4. Your download should start as soon as you click the link. Wait for it to
finish then click the file downloaded.
Step 5. If prompted to install the program, allow it. Then, you should see the
screen below. This is the beginning of the installation process.
This screen is basically a welcome note stating the version number of the JDK
you are installing. Click Next.
Programming NC IV
3
Fundamental Concepts of Java Programming Language
Step 6. Next is an option for you to install features of JDK and where to install
it. By default, it will be installed in Drive (C:) in your Programs folder. Leave
it as it is.
Click Next.
Course Module
The installer will be extracted. Wait for it to finish. The status is indicated by
the progress bar.
Step 7. Next, you will have an option to change the destination folder or
where Java will be installed. By default, it will be installed in Drive (C:) under
Program files folder.
Click Next.
Step 8. You should see this prompt if Java is installed successfully. If you
want to watch tutorials, scan documentation, developer guides and release
notes, click Next.
If not, click Close.
Course Module
Java Program Using CLI
Now it’s time to test the JDK we installed. Let’s try and run simple programs
with the all-time favorite “Hello, World!”.
Follow these steps carefully.
Step 1: Open a text editor like Notepad.
Step 2. Copy and paste the sample program below:
Step 3. Save it as Test.java. If you will notice, the class is also named Test.
Look at the program above.
public class Test
{
…
}
If you are going to change the name of the class, make sure to save it as the
filename as well followed by filename extension .java.
See the screenshot below. If you want to follow these steps EXACTLY, create
a folder in Drive (D:) and name it Java Projects.
Observe carefully. Java Projects folder has only ONE file right now and that
should be Test.java.
Step 4. Now it’s time to compile and run the program. Click the Start button,
type “cmd” and press Enter.
Course Module
Before we continue, take a look at your Java Projects folder, it should now
have two (2) files – Test.java (our code) and Test.class (what the compiler
added when we typed “javac Test.java”.
Step 6. While still in the CLI, type java Test and press Enter.
You should now see the output of the program “Hello, World!”
Open another CLI and repeat Steps 5-6. You should now have a different
output.
It should say “Hello, World! This is my first Java program. I’m a
programmer!”
Course Module
Javac Error
In case you get an error that says “javac is not recognized as an internal or
external command…”, we can fix that.
Step 2: Next, click the Start button then the Control Panel. Choose System
then go to Advanced Settings.
Step 3: You should see the screen below (right). Click “Environment
Variables” and you should see the screen at the right.
Select “Path” and click “Edit…”.
Step 5: Click OK. The error should have been fixed by now. Try running that
simple program again using the CLI.
Course Module
Netbeans Installation
Now, it’s about time we start writing, compiling and running our Java
programs using the Netbeans IDE.
What is an IDE? IDE or Integrated Development Environment is a software
program which has all the tools a programmer need to write and test codes
or programs.
Netbeans is an IDE for Java programming language. Though it’s true that you
can write Java programs using a Notepad, compile and run it using the CLI, it
will be troublesome if you are working on a big Java project.
Just imagine writing your final report or research using a Notepad, how sure
are you that when it is printed, it has the format that you need? Crazy, right?
Step 1. Click this link or copy and paste it in your browser’s address bar:
https://netbeans.org/downloads/
Step 2. You should see the screen below. Under Java SE (Standard Edition),
click the Download button.
Step 4. The installer should be configured now. Wait for this process to
finish.
Course Module
Step 5. You will be greeted with Netbeans’ welcome note where the version
is also stated.
Click Next.
Step 6. Scan the license agreement and if don’t have any problem with it,
click Next.
Course Module
The installation process will begin. Wait for it to finish.
Netbeans IDE
Like any other software, you must be familiar of the different parts of the
Netbeans IDE to fully utilize it.
Look at the screenshot below. There are 8 major parts that we will focus on.
The other windows / parts will be discussed and displayed in the later
modules.
Course Module
As for the basic parts of the Netbeans environment, take note of the
following:
1. Menu Bar – this is the main menu of the Netbeans IDE. Here you will find
menu items such as File, Edit, View, Navitage and more. Each item has a
set of sub-items which offer different options and actions.
2. Quick Launch Bar / Toolbar – the most commonly used actions are
displayed in this toolbar
3. Projects Windows – this is where you will see the project files, classes and
other Java files. Click the + or – sign beside each folder to collapse or
expand the list.
4. Navigator – this feature allows Java programmers to be oriented in source
code and conduct basic actions.
5. Working Area / Code editor – this is where you write codes
6. Assist Icons – icons that allow you to add comments and bookmarks, find
selected text or code and change indentation of codes.
7. Line Number – this tells you the line number of each line of code
8. Output Window – this is where the output is displayed
First, we have to create a Java Project. To do that, click File then New Project.
By default, Java is selected under Categories while Java Application is chosen
under Projects. Leave it as is.
Click Next.
Choose the location or folder of your new project and give it a name.
Click Finish.
Course Module
Automagically, you will be greeted with this code:
Don’t worry, it will do you no harm. The text in gray are just comments,
whatever you write in the comment section will not have an effect in your
program (we’ll discuss this later in detail).
The only thing that matters now is the code in the main class.
Type the following inside the main class as shown in the image below.
System.out.println(“Hello World”);
Now press F6 on your keyboard or click the green Run Project button.
You should see the output at the bottom of the Netbeans screen. Easy, right?
To run a class that is NOT the main class, press Shift + F6. Each project has its
own main class which is the default class that it runs. If you have other
classes in a project, press Shift + F6 instead of F6.
Programming NC IV
21
Fundamental Concepts of Java Programming Language
Syntax
You’ve probably seen this word when you typed something wrong on your
calculator “Syntax Error”.
We also have “syntax” when it comes to programming. Basically, syntax is
one of the building blocks of programming language – it is the spelling and
grammar of programming. You should follow it or else you’ll get an error.
Some points to remember when writing a Java program:
1. Java is case sensitive.
counter is different from Counter and COUNTER.
2. There are standard naming conventions that you should follow. This will
be discussed in the next section.
3. Each Java function and features has its own syntax to be followed like
declarations, expressions, conditional and looping statements and more.
Naming Convention
Programmers have to follow standard naming conventions so that their code
will be easy to read and understand. We have discussed this topic briefly in
properly documenting a program.
Let’s take a closer look at Oracle’s naming convention for Java.
Identifier Type Naming Rules Examples
Classes 1. A class should be a noun. class
2. It should be written in mixed case EmployeeRecord;
with the first letter of each word
capitalized. class Students;
3. Keep class name simple.
4. Use whole words and avoid
abbreviations.
Methods 1. A method should be a verb. getStudentName();
2. It should be written in mixed case runFast();
as well but the first letter is in
lower case.
3. First letter of internal words are
capitalized.
Variables 1. Variables should be written in int x;
mixed case with the first letter in int counter;
lower case. float
2. First letter of internal words are averageScore;
capitalized.
Course Module
3. Do not start the variable name with
an underscore _ or dollar sign $.
4. Use short but meaningful names.
5. One-character variable names
should be avoided except for
throwaway variable.
Constants 1. Constant’s name should be written static final int
in uppercase. CEILING_GRADE =
2. Words are separated with 100;
underscore _.
static final int
MAX_LENGTH = 99;
INCORRECT
someMethod(longExpression1, longExpression2
, longExpression3, longExpression4
, longExpression5);
CORRECT
someMethod(longExpression1, longExpression2,
longExpression3, longExpression4,
longExpression5);
CORRECT
average = (testScore1 + testScore2 + testScore3
+ testScore4) / 4;
Programming NC IV
23
Fundamental Concepts of Java Programming Language
CORRECT
public static void main(String [] args){
int a=30, b=50, c=20;
}
CORRECT
while (true){
...
}
CORRECT
sum = score1 + score2;
average = sum / 2;
Course Module
6. There should also be blank spaces between sections of a source file, class
and interface definitions, methods, local variables in a method, before a
block and between logical sections in a method.
7. Align braces appropriately. There should be a line break after the opening
brace and before the closing brace.
INCORRECT
public class HelloUni {
public static void main(String [] args){
System.out.print(“Hello Universe”);
}
}
INCORRECT
public class HelloUni { public static void main(String []
args){
System.out.print(“Hello Universe”);
} }
CORRECT
public class HelloUni {
public static void main(String [] args){
System.out.print(“Hello Universe”);
}
}
Glossary
Applet (n.) – in Java programming, an applet is a very small application with
one or few functions
Class (v.)– in Java, a class is defined as a blueprint where individual objects
are created
Method (n.)– a set of code grouped together to perform an operation
References
Gosling, J., Joy, B., Steele, G., Bracha, G. & Buckley, A. (2015). The Java
Language Specification. Java SE 8 Edition. California: Oracle America,
Inc.
Oracle. Naming Conventions. Available at:
http://www.oracle.com/technetwork/java/codeconventions-
135099.html