You are on page 1of 10

Philippine Science High School System

COMPUTER SCIENCE 4 – Learning Guide

Subject Code CS4 Computer Science 4


Module Code 1.0 Introduction to Java
Lesson Code 1.1 Java Environment, Development Process, Hello World
Time Frame 30 minutes

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

Create/Modify Source Code

public class HelloWorld {


public static void main (String[ ] args) { Saved on
System.out.println("Hello World!"); the disk
}
Source Code
}

Bytecode (generated by the compiler for JVM to Compile Source Code


read and interpret) Ex. javac HelloWorld.java

If compile errors occur
Method HelloWorld()
0 aload_0 Stored
… on disk
Bytecode
Method void main (java.lang.String[])
0 getstatic #2 …

Run Bytecode
Ex. java HelloWorld

“Hello World” is displayed on the console

Hello World! Result

If runtime errors or incorrect result

Figure 1.0 Java program-development process


Java source-code file are created using any text editor or Integrated Development Environment (IDE)-will
be discussed later. Let’s discuss how a Java program is created, compiled and run from a command
window using the text editor-Notepad++. As shown in Figure 1.1, the source file ends with the extension
.java and have the same exact name as the public class name. The source file for the source-code is
HelloWorld.java, since the public class name is HelloWorld and must be saved inside C:\Java\bin
directory.

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.

Figure 1.3 Java source code is translated into bytecode

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

Creating a Java Project


Before you can create Java programs, you need to first create a project to hold Java programs and all
supporting files. A project is created only once. Here are the steps to create a Java project:

1. Choose File, New Project to display the New Project dialog box, as shown in Figure 1.5.

Figure 1.5 The New Project Dialog

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

Figure 1.6 The New Java Application dialog

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.

Figure 1.7 The New Java project named Quarter1Project is created.

Creating a Java Class


After a project is created, you can create Java programs in the project using the following steps:
1. Right-click the Quarter1Project node in the project pane to display a context menu. Choose New,
Java Class to display the New Java Class dialog box, as shown in Figure 1.8.

CS 4 Page 5 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide

Figure 1.8 The New Java Class dialog

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

Figure 1.9 Template for the source code HelloWorld.java

CS 4 Page 6 of 10
Philippine Science High School System
COMPUTER SCIENCE 4 – Learning Guide

Compiling and Running a Class


To run HelloWorld.java, right-click HelloWorld.java to display a context menu and choose Run File, or
simply press Shift+F6. The output is displayed in the Output pane, as shown in Figure 1.10.

The Run File command automatically compiles the program if the program has been changed.
Edit pane

Output/ Console
pane

Figure 1.10 Edit and output pane of Netbeans IDE

Simple Java Program – Hello World!

Ever wondered how the simple java program that displays Hello World! on the console works? Let me
explain the codes per line.

1. public class HelloWorld {


2. public static void main(String[] args) {
3. // Display message Hello World!
4. System.out.println("Hello World!");
5. }
6. }

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.

Types of comment in Java


(1) Line comment – preceded by two slashes (//)
(2) Block comment or paragraph comment – enclosed between /* and */
Examples of comments
1. // This program displays Hello World!
2. /* This program displays Hello World! */
3. /* This program
4. * displays Hello World! */

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.

1. public class HelloWorld { Class block


2. public static void main(String[] args) {
3. System.out.println("Hello World!");
Method block
4. }
5. }

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

Sample Dialog and Output

3. Submit the .java file.

To guide you in this non-graded activity, I have provided you my program.

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

References: using APA format (7th edition)


1. Fudge, M. (2012, August 21). Learn Programming in Java – Lesson 00: Getting Set-up for
Learning Java. [Video File]. Retrieved from
https://www.youtube.com/watch?v=B8gMEV8GKbg
2. Liang, Y. D. (2015). Introduction to Java Programming Comprehensive Version (10 th Ed.).
New Jersey: Prentice Hall.
3. LinkedlnLearning. (2011, September 27). Computer Programming: What is object-oriented
language? Lynda.com overview [Video file]. Retrieved from
https://www.youtube.com/watch?v=SS-9y0H3Si8&t=236s
4. Savitch, W. (2012). Java: An Introduction to Problem Solving and Programming (6 th Ed.).
New Jersey: Prentice Hall.
5. Sung, H. (2013, February 20). How to use NetBeans for Java programming. [Video File].
Retrieved from https://www.youtube.com/watch?v=8w9WfM6KYa4
6. www.tutorialspoint.com. 2020 . Java Tutorial. [online] Available at
<https://www.tutorialspoint.com/java/index.htm> [Accessed 14 July 2020]

Prepared by: Prepared by:

Dianne Rose A. Bangloy Pablo Viloria


SST II SST V
PSHS- CVC PSHS- CARC

CS 4 Page 10 of 10

You might also like