RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Introduction to Java ---------------------------------------------------------------------------------------------------------------------- 3
Java Quick Start ---------------------------------------------------------------------------------------------------------------------------- 3
The main Method ------------------------------------------------------------------------------------------------------------------------- 3
What is an instruction in programing? ----------------------------------------------------------------------------------------------- 3
Online Compiler --------------------------------------------------------------------------------------------------------------------------- 4
Variable, Identifiers and Data Types-------------------------------------------------------------------------------------------------- 5
Declare Many Variables ----------------------------------------------------------------------------------------------------------------- 7
Java Assignment Operators ------------------------------------------------------------------------------------------------------------- 7
System.out.println() ---------------------------------------------------------------------------------------------------------------------- 8
System.out.print()------------------------------------------------------------------------------------------------------------------------- 8
Single-line Comments -------------------------------------------------------------------------------------------------------------------- 9
Braces ---------------------------------------------------------------------------------------------------------------------------------------- 9
Semicolon ----------------------------------------------------------------------------------------------------------------------------------- 9
Java Operators --------------------------------------------------------------------------------------------------------------------------- 10
Arithmetic Operators------------------------------------------------------------------------------------------------------------------- 10
Concatenation --------------------------------------------------------------------------------------------------------------------------- 11
Start a new program ------------------------------------------------------------------------------------------------------------------- 12
Exercise 1 ------------------------------------------------------------------------------------------------------------------------------------- 13
Home work----------------------------------------------------------------------------------------------------------------------------------- 14
Java User Input (Scanner Class) ------------------------------------------------------------------------------------------------------ 15
Input Types ------------------------------------------------------------------------------------------------------------------------------- 16
Some notes: ------------------------------------------------------------------------------------------------------------------------------ 17
Classwork ------------------------------------------------------------------------------------------------------------------------------------- 18
Exercise 2 ------------------------------------------------------------------------------------------------------------------------------------- 19
Exercise 3 ------------------------------------------------------------------------------------------------------------------------------------- 19
Informatics / 2024 - 2025
1
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 4 ------------------------------------------------------------------------------------------------------------------------------------- 20
Exercise 5 ------------------------------------------------------------------------------------------------------------------------------------- 21
Exercise 6 ------------------------------------------------------------------------------------------------------------------------------------- 22
Exercise 7 ------------------------------------------------------------------------------------------------------------------------------------- 23
Exercise 8 ------------------------------------------------------------------------------------------------------------------------------------- 25
Exercise 9 ------------------------------------------------------------------------------------------------------------------------------------- 26
Exercise 10 ----------------------------------------------------------------------------------------------------------------------------------- 27
Informatics / 2024 - 2025
2
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Introduction to Java
Java is a simple and yet powerful programming language and it is in many respects similar
to C++.
Where Java is used?
The programming language Java was developed by Sun Microsystems in the year 1995. Ear-
lier, it was only used to design and program small computing devices but later adopted as
one of the platform independent programming languages.
Java Quick Start
In Java, every application begins with a class name, and that class must match the
filename.
The file should contain a "Hello World" message, which is written with the following
code:
The main Method
The main() method is required and you will see it in every Java program:
public static void main(String[] args) {
What is an instruction in programing?
Instructions are simply order given to computer processor. It instructs computer to
carry out function such as calculation, formatting, etc.
A Program are simply set of instructions or computer code.
Informatics / 2024 - 2025
3
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Online Compiler
To access online Java go to:
- Java online programiz on Google
- Click on Online Java Compiler (Programiz)
Informatics / 2024 - 2025
4
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Variable, Identifiers and Data Types
A variable is a place where the program stores data temporarily. As the name implies
the value stored in such a location can be changed while a program is executing.
To create (declare) a variable, we need to give a name and a data type for it.
Data types
Every variable in Java has a data type which tells java what type of data it is going to store.
Java is rich in data types which allows the programmer to select the appropriate type needed to build
variables of an application like: byte, short, int, long, float, double, string, char…
The most important data types are:
Data Type Range
int For integer number (doesn’t accept fraction) such as 750.
-2,147,483,648 To 2,147,483,647
Numbers used in calculation
Double For decimal number (accept fraction) such as 1.23.
Numbers used in calculation
String For text values, such as name, address.
Text values can be any character like letters, numbers and special characters 'sym-
bols'. (Numbers used in string, cannot be used in calculation)
Declaring variables
Declaring a variable means assigning a name and a data type.
Syntax
Data type variableName ;
Example
int a;
double b;
String c;
Informatics / 2024 - 2025
5
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Examples
- Create a variable called name of type String and assign it the value "John":
String name = "John";
System.out.println(name);
- Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
System.out.println(myNum);
- You can also declare a variable without assigning the value, and assign the value
later:
int myNum;
myNum = 15;
System.out.println(myNum);
- Note that if you assign a new value to an existing variable, it will overwrite the
previous value:
Change the value of myNum from 15 to 20:
int myNum = 15;
myNum = 20; // myNum is now 20
System.out.println(myNum);
Informatics / 2024 - 2025
6
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Declare Many Variables
To declare more than one variable of the same type, use a comma-separated list:
Example
int x, y, z;
or
int x = 5, y = 6, z = 50;
Java Assignment Operators
The Java Assignment Operators are used when you want to assign a value to a varia-
ble.
The assignment operator denoted by the single equal sign =.
In a Java assignment statement, any expression or a value should be on the right
side, and the name of the variable should be on the left side
• All the text values (type string) should be written between quotations " " like
Subject="Java";
• Quotations are not acceptable for the type int or double like a=5*10, b=7.23;
Example
int a, c;
a = 10;
c = a*3;
double b = 10.5;
string name;
name="java";
Informatics / 2024 - 2025
7
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
System.out.println()
Inside the main() method, we can use the println() method to print a line of text to
the screen, or to print different text on different lines:
public static void main(String[] args) {
System.out.println("Hello World");
System.out.print()
Use System.out.print() to print statements, expressions or variables on the same line.
Example
System.out.println("Hello World");
Displays the sentence Hello world
System.out.print("Hello ");
System.out.println("World");
System.out.println(" ☺");
Displays the sentence Hello world
☺
System.out.println("Hello ");
System.out.println("World");
Displays the sentence Hello
World
System.out.println(5+3);
Displays the number 8
System.out.println("5+3");
Displays the text 5+3
Informatics / 2024 - 2025
8
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
int nb=10;
System.out.println(nb);
Displays the value of the variable nb which is 10
Double T=12.25;
System.out.println("Tax = " + T);
Displays the value of the variable T as the following form Tax = 12.25
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
Example
// This is a comment
System.out.println("Hello World");
This example uses a single-line comment at the end of a line of code:
System.out.println("Hello World"); // This is a comment
Braces
The curly braces {} marks the beginning and the end of a block of code.
Semicolon
In java, every instruction or command must end with semicolon ;
Example:
Width=5;
Height=10;
Area=width*height;
Informatics / 2024 - 2025
9
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Java Operators
Operators are used to perform operations on variables and values.
There are various arithmetic operators used in Java:
In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and an-
other variable:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by If x=3
1 X++ will be 4
-- Decrement Decreases the value of a variable by If x=3
1 X-- will be 2
Informatics / 2024 - 2025
10
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Concatenation
To combine both text and a variable, use the + character:
Example
String name = "John";
System.out.println("Hello " + name);
Output
Hello John
Example
String name = "Mark";
System.out.println("Hello " + name + ", Hope you are doing well");
Output
Hello Mark, hope you are doing well
Informatics / 2024 - 2025
11
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Start a new program
1. Click File
2. Select New project
3. Select Java Start (Under Categories)
4. Select Java Application (Under Projects)
5. Click Next
6. In the project name, type the name of the project (Calculation)
7. In the project Location, click Browse and select the folder you want to save in
(First Project)
8. Click Finish
Informatics / 2024 - 2025
12
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 1
Write a java program that calculates the square
of the integer number (5),
and then displays the result as follows:
Let's square a number
-------------------------
Number = 5
Square = 25
Informatics / 2024 - 2025
13
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Home work
Write a java program that calculates the sum of two integer numbers (a=10 and b=20),
then displays the result as follows:
first number 10
Second number 20
10 + 20 = 30
Answer 1
int a,b;
a=10;
b=20;
System.out.println("first number " + a);
System.out.println("Second number " + b);
System.out.println(a + " + " + b + " = " + (a+b));
Answer 2
int a,b,s;
a=10;
b=20;
s=a+b;
System.out.println("first number=" + a);
System.out.println("second number=" + b);
System.out.println(a + "+" + b + "=" + s);
Informatics / 2024 - 2025
14
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Java User Input (Scanner Class)
The Scanner class is used to get user input a value to a variable from the keyboard.
It is found in the package java.util.
To use the Scanner class, user needs to create an object of the class (in this example is called
scan).
How it works?
- User needs to import the scanner class to its project by writing the following sentence
before the public class HelloWorld as follows:
Import java.util.Scanner; S must be capital letter
- After that, user needs to create the object scan by writing the following sentence under
public static void main (string[] args) as follows:
Scanner scan=new Scanner (System.in); S must be capital letter
The code should look as follows:
import java.util.Scanner;
class HelloWorld{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name;
System.out.print("Enter username ");
name = scan.next();
System.out.println("Username is: " + name);
Informatics / 2024 - 2025
15
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Input Types
A data type is an attribute associated with a piece of data that tells a computer system how to in-
terpret its value
In the example above, we used the nextInt() method, which is used to read Strings. To
read other types, look at the table below:
Method Type Description Example
a=scan.nextInt(); Int Accepts Integer number. 750
Numeric data type for
numbers without fractions.
a= scan.nextDouble(); Double Accepts real number. 12.65
Numeric data type for
numbers with fractions.
name= scan.next(); String Accepts text without Lebanon, hello…
space.
Sequence of characters,
digits, or symbols—always
treated as text
Informatics / 2024 - 2025
16
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Some notes:
• Every line must end with semi columns;
• The letter S in red color, should be capital in the following instructions:
import java.util.Scanner;
Scanner scan = new Scanner (System.in);
• The letter I and D in the red color, should be capital letter in the following in-
structions:
o a=scan.nextInt();
o a=scan.nextDouble();
• You should never write codes after the final two braces } }.
• When you declare variables, the types int and double must be written in
small letters, and for the type String, only the letter S should be in capital
letter.
Informatics / 2024 - 2025
17
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Classwork
Write a java program that enters two integer numbers and then calculates the sum of
them.
Displays the result as follows:
Enter first number: 6
Enter second number 3
6+3=9
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int a,b;
System.out.print("Enter first number: ");
a=scan.nextInt();
System.out.print("Enter second number: ");
b=scan.nextInt();
System.out.println(a + " + " + b + " = " + (a+b));
}
}
Informatics / 2024 - 2025
18
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 2
Write a java program that enters the side of a square of type integer by the user, (use
scan.next…) then calculates and displays its area and its perimeter.
- Area = side*side
- Perimeter = side*4
The output of the program should be as the following form:
Enter the Side: 5
Area = 25
Perimeter = 20
int s,a,p;
System.out.print("Enter the Side: ");
s=scan.nextInt();
a=s*s;
p=4*a;
System.out.println("Area = " + a);
System.out.println("Perimeter = " + p);
Exercise 3
Write a Java program that enters the temperature of type double in Fahrenheit by the user,
and then convert it into Celsius.
- Celsius = (fahrenheit - 32)/1.8
The output of the program should be as the following form:
Enter temperature in Fahrenheit: 98.4
Celsius = 36.88
System.out.print("Enter the temperature in Fahrenheit: ");
double f = scan.nextDouble();
double c = (f - 32) / 1.8;
System.out.println("Celsius = " + c);
Informatics / 2024 - 2025
19
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 4
Write a java program that enters two numbers of type integer by the user, and then calcu-
lates and displays their sum and product.
The output of the program should be as the following form:
Enter number 1: 8
Enter number 2: 4
8 + 4 = 12
8 * 4 = 32
int a, b, s, p;
System.out.print("Enter number 1: ");
a = scan.nextInt();
System.out.print("Enter number 2: ");
b = scan.nextInt();
s = a + b;
p = a * b;
System.out.println(a + " + " + b + " = " + s);
System.out.println(a + " * " + b + " = " + p);
Informatics / 2024 - 2025
20
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 5
Write a java program that enters the name of the car (text), the rent price (inte-
ger) and the number of days (integer) and then calculates the amount.
- Amount = number of days * rent price.
The output of the program should be as the following form:
Enter the car: BMW
Enter the rent price: 20
Enter the number of days: 3
Amount = 60 $, of the car BMW
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner Scan= new Scanner(System.in);
String name;
System.out.print("Enter the car: ");
name=Scan.next();
int p,d,amount;
System.out.print("Enter the rent price: ");
p=Scan.nextInt();
System.out.print("Enter the number of days: ");
d=Scan.nextInt();
amount=p*d;
System.out.println("Amount= " + amount + "$, of the car " + name);
Informatics / 2024 - 2025
21
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 6
Write a java program that enters the name of a product (text), the price (integer) and
the quantity purchased (integer), then calculates the amount of it
- Amount = price * quantity
The output of the program should be as the following form:
Enter the name: Mars
Enter the price: 80000
Enter the quantity: 2
Amount of Mars is 160000
String name;
System.out.print("Enter the name: ");
name=Scan.next();
int p,q,amount;
System.out.print("Enter the price: ");
p=Scan.nextInt();
System.out.print("Enter the quantity: ");
q=Scan.nextInt();
amount=p*q;
System.out.println("Amount of " + name + " is " + amount);
Informatics / 2024 - 2025
22
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 7
Fill the empty line with the needed java codes.
1- Create a variable that accepts text values, named it as carName and assign the
value Volvo to it.
_____________ ______________ = ____________;
2- Create a variable that accepts integer number, named it as maxSpeed and assign
the value 120 to it.
_____________ ______________ = ____________;
3- Create a variable that accepts numbers with fraction, named it as Tax and assign
the value 75.23 to it.
_____________ ______________ = ____________;
4- Display the sum of 5 + 10, using two variables: x and y.
_____________ ______________ = ____________;
int y = 10 ;
System.out.println(x + y) ;
5- Create a variable called Z, assign x + y to it, and display the result as
Total = 15.75.
Double x = 5.50 ;
Double y = 10.25 ;
___________ ___________ = x + y;
System.out.println ( ________________________ );
6- which way is more efficient in java to create three variables of the same type:
a) int x=5; int y=6; int z=50;
b) int x=5, y=6, z=50;
c) int x=5, int y=6, int z=50;
Informatics / 2024 - 2025
23
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
6) Create a variable called Z, assign x + y to it, and display the result as
7 + 3 = 10.
int x = 7 ;
int y = 3 ;
________ = x + y;
System.out.println ( _________________ );
7- Which of the following codes is correct?
a) String address="Beirut";
b) String address=Beirut;
c) int address="Beirut";
8- Which of the following codes is correct?
d) int age=18;
a) int age="18";
b) string age=18;
9- Which of the following codes is correct?
a) int rate=7.3;
e) double rate=7.3;
b) string rate=7.3;
10- Which code is correct?
a) System.out.println("My name is ", name);
b) System.out.println("My name is " name);
f) System.out.println("My name is " + name);
11- Which symbol is used to write a comment?
a) ##
b) /
g) //
Informatics / 2024 - 2025
24
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 8
Answer with True or False.
1. The type int can accept fraction number such as 2.36. False
2. The type String accepts characters such as the name of a student. True
3. X++ is used to increment x by 2. False
4. If x=5, then x-- is 4. True
5. If name="Ray", then the instruction System.out.println ("Hello" + name); will dis-
play Hello Ray. True
6. If class="SE2", then the instruction System.out.println ("Hello class"); will display
Hello se2. false
Informatics / 2024 - 2025
25
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 9
What will the value of "X" be after the following code executes?
int a=10, b=20, x;
x=a+b; x=10+20=30
a=b; a=20 (the value 10 is removed)
x=x+a+b; x=30+20+20=70
Answer: 70
int a=10, b=20, x;
x=a+b; x=10+20=30
a=b; a=20 (the value 10 is removed)
a++; a=20+1=21
x=x+a+b; x=30+21+20=71
Answer: 71
int x;
x=10;
x=x*10; x=10*10=100
x++; x=101
x=x*10; x=101*10=1010
Answer: 1010
int a=5, b=5, x=10;
x=x+a+b; x=10+5+5=20
a=x; a=20
b=a; b=20
x=x+a+b; x= 20+20+20=60
Answer: 60
int a, x=10;
x--; x=9
a=x; a=9
x=a+x; x=9+9=18
a--; a=8
x=a+x; x=8+18=26
Answer: 26
Informatics / 2024 - 2025
26
RHHS – SE2 Java/ Part 1 JAVA – Programming Language
Exercise 10
Find and correct the mistakes in the below codes. Use short answers
1. int a ,b, c, d, e;
2. String name;
3. a=5;
4. x=4+a; int x
5. b=1.5; double b, or b=1
6. c=a 10; missing operator like + - * /
7. 25=e; e=25
8. System.out.display("c = " + c); . print
9. System.out.println(a + e = " + (a+e)); missing " "a + e = "
10. d=a;
11. System.out.println("d= " d); missing + ("d= " + d);
12. name="Java" missing ;
13. System.out.println(Thank you + name); missing “Thank you”
Informatics / 2024 - 2025
27