Object-Oriented Programming Lab 1: Java, How To Program: Ton Duc Thang University Faculty of Information Technology
Object-Oriented Programming Lab 1: Java, How To Program: Ton Duc Thang University Faculty of Information Technology
OBJECT-ORIENTED PROGRAMMING
LAB 1: JAVA, HOW TO PROGRAM
I. Objective
After completing this first lab tutorial, you can:
• Setup the Java Environment, compile and run the Java program.
• Use the basic statements: conditional statements, loop statements, method in Java.
As of 2016, Java is one of the most popular programming languages in use, particularly for client-server
web applications, with a reported 9 million developers. The language derives much of its syntax from
C and C++, but it has fewer low-level facilities than either of them.
• Download and install the Java SE Development Kit. You are recommended to use Java version
8. If you use another version, please be careful with the syntax. But all of your works when you
are doing the exams or the exercises will be assigned with Java 8.
o Then, open the command prompt (CMD) window and type java -version. If you get
the message that provides the information about the installed Java version, that means
you have successfully set up the JDK and Java Path.
• For Java editor, we strongly recommend Notepad++ for your very beginning course of Java.
Beside that, you can use Visual Studio Code or Sublime Text. After completing this course, you
can use other Java editors, e.g., Netbeans, Eclipse, IntelliJ IDEA.
• Save as MyFirstProgram.java.
• Open the command prompt window (CMD) and navigate to the directory where you save the
file.
• Type javac MyFirstProgram.java and press Enter to compile the source code.
• Now, you will see the "Hello World!" string on the screen.
1. Basic syntax
In java, you should keep in mind the following points:
• Case Sensitivity: Java is case sensitive, which means identifier Hello and hello would have a
different meaning in Java.
• Class Names: For all class names, the first letter should be in Upper-Case. If several words are
used to form the name of the class, each inner word's first letters should be in Upper-Case.
Example: class MyFirstProgram
• Method Names: All method names should start with a Lower-Case letter. If several words are
used to form the names of the method, then each inner word's first letter should be in Upper-
Case.
Example: public void methodName()
• Program File Name: Name of the program file has to exactly match the class name.
Example: class MyFirstProgram -> MyFirstProgram.java
• public static void main(String args[]): Java program processing starts from the
main() method which is a mandatory part of every Java program.
2. Java Identifiers
The identifier is the name used for classes, variables, and methods. To name an identifier in Java, you
should remember the following points:
• All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore
(_)
• After the first character, identifiers can have any combination of characters.
3. Java Modifiers
There are two categories of modifiers in Java:
4. Comments in Java
Java supports both single-line and multiple-line comments, and they are similar to C and C++. All
characters available inside any comments are ignored by the Java compiler.
V. Data Types
In this first lab tutorial, we only focus on primitive data types. For the user-defined data types, we will
discuss later in this course.
// MyFirstProgram.java
public class MyFirstProgram
{
public static void main(String[] args)
{
int a = 5, b = 10;
int sum = a + b;
System.out.println("a + b = " + sum);
}
}
• Arithmetic operators
• Relational operators
• Bitwise operators
• Logical operators
• Assignments operators
• Misc. operators
In this lab tutorial, we discuss arithmetic operators, relational operators, logical operators, and
assignment operators. For the others, you can read in the textbook.
1. Arithmetic operators
Arithmetic operators are used in the mathematical expression in the same way that they are used in
algebra.
Operators Example
a + b will give 30
+ (addition)
a – b will give
- (subtraction)
a * b will give 200
* (multiplication)
a / b will give 2
/ (division)
a % b will give 0
% (modulus)
b++ will give 21
++ (increment)
b-- will give 19
-- (decrement)
2. Relational operators
The followings are the relational operations supported by Java language.
Operators Example
(a == b) is false
== (equal to)
(a != b) is true
!= (not equal to)
(a > b) is false
> (greater than)
(a < b) is true
< (less than)
(a >= b) is false
> = (greater than or equal to)
(a <= b) is true
<= (less than or equal to)
3. Logical operators
The followings are the logical operators supported by Java language.
Operators Example
(A || B) is true
|| (logical or)
!(A && B) is true
! (logical not)
4. Assignment operators
Operators Example
C = A + B will assign value of A + B into C
=
C += A is equivalent to C = C + A
+=
C -= A is equivalent to C = C - A
-=
C *= A is equivalent to C = C * A
*=
C /= A is equivalent to C = C / A
/=
C %= A is equivalent to C = C % A
%=
• while loop
1. while loop
A while loop statement in Java programming language repeatedly executes the target statement as long
as a given condition is true.
Example:
2. for loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be
executed a specific number of times. A for loop is useful when you know how many times a task will
be repeated.
Example:
Example 1:
IX. Methods
A Java method is a collection of statements that are grouped together to perform an operation. The
syntax is as follows:
modifier returnType nameOfMethod (Parameter List)
{
// statements
}
Example:
According to the above example, findMax is the static method. So in the main function, you can call
directly without creating a new object. You will learn more about the method in Lab 4.
X. Text Output
Here is a list of the various print functions that we use to output statements:
• print(): This method in Java is used to display a text on the console. This text is passed as the
parameter to this method in the form of String. This method prints the text on the console and
the cursor remains at the end of the text at the console. The next printing takes place from just
here.
Example:
class Demo {
public static void main(String[] args)
{
System.out.print("Hello! ");
System.out.print("Hello! ");
System.out.print("Hello! ");
}
}
Output:
• println(): This method in Java is also used to display a text on the console. It prints the text
on the console and the cursor moves to the start of the next line at the console. The next printing
takes place from the next line.
Example:
class Demo {
public static void main(String[] args)
{
System.out.println("Hello! ");
System.out.println("Hello! ");
System.out.println("Hello! ");
}
}
Output:
Hello!
Hello!
Hello!
• printf(): This is the easiest of all methods as this is similar to printf in C. Note that
System.out.print() and System.out.println() take a single argument, but printf() may take
multiple arguments. This is used to format the output in Java.
Example:
class Demo {
public static void main(String args[])
{
int x = 100;
System.out.printf("Printing simple" + " integer: x = %d\n",x);
float n = 5.2f;
n = 2324435.3f;
To use the Scanner class, create an object of the class and use any of the available methods found in the
documentation.
Method Description
Example:
import java.utils.Scanner;
class MyFirstProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
XII. Exercises
1. Write a program to print your name, date of birth, and student ID.
2. Write a program to compute the area of a rectangle with a height and base provided by user.
1
(𝑎𝑟𝑒𝑎 = 2 × 𝑏𝑎𝑠𝑒 × ℎ𝑒𝑖𝑔ℎ𝑡)
3. Write a function to convert the temperature from Fahrenheit to Celsius and a function to
convert the temperature from Celsius to Fahrenheit.
8. Write functions to calculate the below formulas with n is provided by user (each formula is one
function):
a. 𝑆 = 1 + 2 + 3 + ⋯ + 𝑛
b. 𝑃 = 1 × 2 × 3 × … × 𝑛
c. 𝑆 = 1 + 21 + 22 + ⋯ +2𝑛
1 1 1 1
d. 𝑆 = +4+ + ⋯+
2 6 𝑛
e. ∑𝑛𝑖=1 𝑖 2
9. Write a function to find the first digit and a function to find last digit of a number.
10. Write a function to calculate sum of digits and a function to calculate product of digits of a
number.
11. Write a function to return the remainder of a division.
-- THE END --