Professional Documents
Culture Documents
TARGET
By the end of this learning guide, the students should be able to demonstrate how to create,
compile and run a Java program.
HOOK
Have you ever written a program related to real-life objects using object-oriented programming? Or have
you ever used different problem solving techniques connecting your everyday problems with concepts in
the object-oriented paradigm?
This course will teach you how to be equipped with object-oriented approach in designing computer
program. We will be using Java since it is one of the most in-demand and useful programming
languages in today’s trend.
To give you a head start, I provided you a link to Computer programming: what is object-oriented
language? Lynda.com. (LinkedInLearning, 2011, 5:29)
https://www.youtube.com/watch?v=SS-9y0H3Si8&t=236s
IGNITE
The Java program development involves create-compile-execute process. You need to create a java
program and compile before it can be executed. Programs with compile errors must be fixed and then
recompiled. Programs with runtime errors or a program that do not produce correct result, must be
modified, recompiled and executed again. This process is repeated until program meets the program
specification as explained in Figure 1.0.
CS 4 Page 1 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
Run Bytecode
Ex. java HelloWorld
Source file
class name
CS 4 Page 2 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
Figure 1.1
From the command window, theAjava
Javasource
source-code file HelloWorld.javaisiscompiled
code HelloWorld.java created using Notepad++
by java compiler-javac,
and if there aren’t any syntax errors, the compiler generates bytecode file with .class extension, and this
file is executed using the java command.
Figure 1.2 shows how to use javac command for compiling HelloWorld.java. The compiler
generates the HelloWorld.class file, and this file is executed using the java command.
Compile
Show files
bytecode
Run
Output
Figure 1.2 Compiling Java source code HelloWorld.java and executing bytecode
Figure 1.3 explains how a java source code is translated into a bytecode, then finally executed by JVM..
This bytecode is interpreted by the Java Virtual Machine (JVM). The JVM executes the Java code along
with the code in the library.
The Java language is a high-level language, while Java bytecode is a low-level language which is similar
to machine instructions but is architecture neutral and can run on any platform and operating system that
has Java Virtual Machine (JVM). The JVM is an interpreter that translates the individual instructions in the
bytecode into the target machine language code one at a time rather than the whole program as a single
unit. Figure 1.4 shows a java bytecode can be executed on any computer with JVM
CS 4 Page 3 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
Figure 1.4 Java bytecode can be executed on any computer with a Java Virtual Machine
Popular Java Editors
In the previous example, you used two programs: a 1) text editor to create and a 2) command window to
compile and execute java program. Integrated Development Environment (IDEs) are provided for
developing java programs quickly, in one graphical user interface you can edit, compile, build, debug you
program and even do online help. Popular IDEs for Java include JCreator, Netbeans, and Eclipse. Let’s
discuss how we create a project, a class, compile, and run a class using the Netbeans IDE.
If you want to have a familiarity with the Netbeans environment, click on the link below and it will direct
you to How to use NetBeans for Java programming (Sung, 2014, 7:13)
https://www.youtube.com/watch?v=8w9WfM6KYa4
1. Choose File, New Project to display the New Project dialog box, as shown in Figure 1.5.
2. Select Java in the Categories section and Java Application in the Projects section and click Next
to display the New Java Application dialog box, as shown in Figure 1.6.
CS 4 Page 4 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
3. Type Quarter1Project in the Project Name field. Uncheck Create main Class.
4. Click Finish to create the project, as shown in Figure 1.7.
CS 4 Page 5 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
2. Type HelloWorld in the Class Name field. Leave the Package field blank.
3. Click Finish to create the HelloWorld class and to generate the template for the source code
HelloWorld.java, as shown in the Figure 1.9
CS 4 Page 6 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
The Run File command automatically compiles the program if the program has been changed.
Edit pane
Output/ Console
pane
Ever wondered how the simple java program that displays Hello World! on the console works? Let me
explain the codes per line.
Line 1 –defines a class, in this case the class name is HelloWorld. Every Java program must have at least
one class.
CS 4 Page 7 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
Line 2 – defines the main method. Java starts executing from the main method. A method can contain
statements.
Line 3 – is a comment; it provides explanation for the next line. Comments are not programming statements
and are ignored by compiler.
Line 4 – contains the System.out.println statement. This statement displays the String Hello World! on
the console. Statements in Java ends with a semicolon (;) known as statement terminator.
public, class, static and void are reserved words, or keywords in Java. They have a specific meaning to the
compiler and cannot be used for other purposes in the program.
To group program components, a pair of curly braces ({ })must be used. In the following code, it presents
the class block – groups the data and methods of the class and method block that groups statements in the
method.
NAVIGATE
It’s now time to enhance your programming skills. Try to work on this non-graded activity following this
classname format: Ex1_Section_LastNameFirstName.java.
Exercise #01: Using Netbeans construct a Java program that will display your name, grade/section
and address.
Program Specifications:
1. At the topmost part of your program put the following comments:
/* Name: <Last Name, First Name Middle Initial>
Section: <Your Section>
Date Started: <mm/dd/yyyy>
Date Finished: <mm/dd/yyyy>
*/
2. Follow the format on the sample dialog and output.
CS 4 Page 8 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
1. /* Name:
2. * Section:
3. *
4. * Date Started:
5. * Date Finished:
6. * */
7. public class Ex1_LastnameFirstname {
8.
9. public static void main(String[] args) {
10. // TODO Auto-generated method stub
11. System.out.println("Name: ");
12. System.out.println("Grade / Section: ");
13. System.out.println("Address: ");
14. }
15. }
KNOT
In summary,
o You can write java programs using text editor (Notepad++) or IDE such as JCreator, Netbeans
and Eclipse.
o Java source code file usually has .java extension, compiled by java compiler that generates java
bytecode (with .class extension) that can be executed to any platform or Operating with Java
Virtual Machine.
o Java bytecode is low-level machine similar to machine instructions
o Java Virtual Machine is a program that interprets java bytecode.
o A java program have at least one class.
o Command- java compiles java program
o Command- java executes a bytecode
o program execution starts in main method that contain statements.
o Comment provides explanation for statements.
o Reserve words or keywords in java have specific meaning and cannot be used for other purpose
o Curly braces ({}) are used to group program components
CS 4 Page 9 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide
CS 4 Page 10 of 10