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
• Download and install the Java SE Development Kit. If you are using Microsoft Windows 64-bit,
we recommend that you should install both 32-bit and 64-bit of Java SE Development Kit (JDK).
o Then, open 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.
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 comment 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. With int a = 10 and int b = 20:
Operators Example
2. Relational operators
The followings are the relational operations supports by Java language. With int a = 10 and int b = 20:
Operators Example
3. Logical operators
The followings are the logical operators supported by Java language. With boolean A = true and boolean
B = false:
Operators Example
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 useful when you know how many times a task is to be
repeated.
Example:
Example:
Example 1:
IX. Array
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of
the same type. To declare an array, we have 4 possible ways:
dataType[] arrayName;
dataType arrayName[];
dataType[] arrayName = new dataType[arraySize];
dataType[] arrayName = {value0, value1, ..., valueK};
Example:
X. 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:
public class MyFirstProgram
{
public static int findMax(int a, int b)
{
return (a > b)? a: b;
}
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);
• 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;
}
}
Output:
XIII. 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 width provided by user.
3. Write a program to convert the temperature from Fahrenheit to Celsius. (oC = (oF - 32) / 1.8)
4. Write a program to check whether a year is a leap year or not. (leap year is exactly divisible by
4, except for years that are exactly divisible by 100, but these centurial years are leap years if
they are exactly divisible by 400).
9. Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and
Computer. Calculate percentage and grade according to following:
11. Write a function to calculate sum of digits and a function to calculate the product of digits of a
number.