Lab 1
Lab 1
Lab 1
Let us begin by writing our first Java program that prints a message "Hello, world!" to the display
console, as follows:
Hello, world!
Step 1: Write the Source Code: Enter the following source codes using a programming
text editor (such as TextPad or NotePad++ for Windows or gedit for UNIX/Linux/Mac); or an
Interactive Development Environment (IDE) (such as Eclipse or NetBeans).
Do not enter the line numbers (on the left panel), which were added to help in the explanation.
Save the source file as "Hello.java". A Java source file should be saved with a file extension of
".java". The filename shall be the same as the class name - in this case "Hello".
1 /*
2 * First Java program, which says "Hello, world!"
3 */
4 public class Hello { // Save as "Hello.java"
5 public static void main(String[] args) {
6 System.out.println("Hello, world!"); // print message
7 }
8}
Step 2: Compile the Source Code: Compile the source code "Hello.java" into portable
bytecode "Hello.class" using JDK Compiler "javac". Start a CMD Shell (Windows) or Terminal
(UNIX/Linux/Mac) and issue this command:
There is no need to explicitly compile the source code under IDEs (such as Eclipse or
NetBeans), as they perform incremental compilation implicitly.
Step 3: Run the Program: Run the program using Java Runtime "java", by issuing this
command:
Take note that the Run command is "java Hello" without the ".class" extension.
On IDEs (such as Eclipse or NetBeans), right-click on the source file Run As... Java
Application.
/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the compiler.
But they provide useful explanation and documentation to your readers (and to yourself three
days later). There are two kinds of comments:
1. Multi-line Comment: begins with /* and ends with */, and may span more than one lines (as
in Lines 1-3).
2. End-of-line Comment: begins with // and lasts until the end of the current line (as in Lines 4
and 6).
The basic unit of a Java program is a class. A class called "Hello" is defined via the keyword
"class" in Lines 4-8. The {...} is the body of the class. The keyword public will be discussed later.
In Java, the name of the source file must be the same as the name of the public class with a
mandatory file extension of ".java". Hence, this file MUST be saved as "Hello.java".
Lines 5-7 defines the so-called main() method, which is the starting point, or entry point for
program execution. The {...} is the body of the method which contains programming statements.
System.out.println("Hello, world!");
In Line 6, the statement System.out.println("Hello, world!") is used to print the message string
"Hello, world!" to the display console. A string is surrounded by a pair of double quotes and
contain texts. The text will be printed as it is, without the double quotes.
2. Java Terminology and Syntax
Comments: A multi-line comment begins with /* and ends with */. An end-of-line comment
begins with // and lasts till the end of the line. Comments are NOT executable statements and
are ignored by the compiler. But they provide useful explanation and documentation. Use
comments liberally.
Whitespaces: Blank, tab, and newline are collectively called whitespace. Extra whitespaces
are ignored, i.e., only one whitespace is needed to separate the tokens. But they could help you
and your readers better understand your program. Use extra whitespaces liberally.
Case Sensitivity: Java is case sensitive a ROSE is NOT a Rose, and is NOT a rose. The
filename is also case-sensitive.
You can use the following template to write your Java programs. Choose a meaningful
"Classname" that reflects the purpose of your program, and write your programming statements
inside the body of the main() method. Don't worry about the other terms and keywords now. I
will explain them in due course.
You can use System.out.println() or System.out.print() to print message to the display console:
System.out.println(aString) (Print-Line) prints the given aString, and brings the cursor to
the beginning of the next line.
System.out.print(aString) prints aString but places the cursor after the printed string.
Hello, world!Hello,
world!Hello, world!
Exercise 1
1. Print each of the following patterns. Use one System.out.println(...) statement for each
line of outputs.
* * * * * ***** * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * ***** *
1/*
2 * Sum five numbers and print the result
3 */
4public class FiveNumberSum { // Save as "FiveNumberSum.java"
5 public static void main(String[] args) {
6 int number1 = 11; // Declare 5 int variables to hold 5 integers
7 int number2 = 22;
8 int number3 = 33;
9 int number4 = 44;
10 int number5 = 55;
11 int sum; // Declare an int variable called sum to hold the sum
12 sum = number1 + number2 + number3 + number4 + number5;
13 System.out.print("The sum is "); // Print a descriptive string
14 System.out.println(sum); // Print the value stored in sum
15 }
16}
declare five int (integer) variables called number1, number2, number3, number4, and number5;
and assign values of 11, 22, 33, 44 and 55 to the variables, respectively, via the so-
called assignment operator '='. You could also declare many variables in one statement,
separating with commas, e.g,
int number1 = 11, number2 = 22, number3 = 33, number4 = 44, number5 = 55;
int sum;
declares an int (integer) variable called sum, without assigning an initial value.
computes the sum of number1 to number5 and assign the result to the variable sum. The
symbol '+' denotes arithmetic addition, just like Mathematics.
System.out.print("The sum is ");
System.out.println(sum);
Line 13 prints a descriptive string. A String is surrounded by double quotes, and will be printed
as it is (but without the double quotes). Line 14 prints the value stored in the variable sum (in
this case, the sum of the five numbers). You should not surround a variable to be printed by
double quotes; otherwise, the text will get printed instead of the value stored in the variable.
Exercise
1. Modify the above program to print the product of 5 integers. Use a variable called product
to hold the product result, and * for multiplication.
2. Follow the above example, write a program (called RectangleComputation) to print the
area and perimeter of a rectangle, given its length and width in doubles.