Java-2 Lecture1
Java-2 Lecture1
PROGRAMMING
Level (1) - IT
2
𝓔𝓷𝓰
𝓔𝓷 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭
𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
2
Overview
❑ Introduction to Java
▪ Java is an Object-Oriented Programming (OOP) language.
▪ Platform Independent thanks to the Java Virtual Machine (JVM).
▪ Robust, secure, and used for various applications such as web,
mobile, and enterprise systems
❑ Comments in Java
▪ Single-line comments: Start with // .
❑ Escape Sequences
▪ Definition: Used to represent special characters in strings.
Sequence Description
\n Newline
\t Tab
\" Double quotation mark
\\ Backslash
\’ single quotation mark
\b backspace
\r carriage return
\f form feed
❑ Variables
▪ Declaring a Variable:
❑ User Input
❑ Operators in Programming
▪ Assignment Operators:
Assignment Type Description Operators Example
Assigns a value to a
Simple Assignment
variable.
= Int x = 5;
Combines an operation
Compound Assignment
with the assignment.
+=, -=, *=, /=, %= x += 3; // x = x + 3
Increases or decreases
x++; // x = x + 1
Increment/Decrement the value of a variable ++, --
by 1. x--; // x = x - 1
❑ Operators in Programming
▪ Arithmetic Operators: Operation Symbol
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
▪ Comparison Operators:
Operation Symbol Operation Symbol
Equal to == Less than <
Not equal to != Greater or equal >=
Greater than > Less or equal <=
❑ Operators in Programming
▪ Logical Operators:
Operation Symbol
AND &&
OR ||
NOT !
A B A && B A || B !A
True True True True False
True False False True False
False True False True True
False False False False True
❑ Operators in Programming
▪ Operator Precedence:
1. Parentheses ( ).
2. Unary Operations: ++, --, +, -, !.
3. Multiplication/Division Operations: *, /, %.
4. Addition/Subtraction Operations: +, -.
5. Relational Operations: ==, !=, >, <, >=, <=.
6. Logical AND/OR Operations: &&, ||.
▪ Example:
❑ Type Casting
▪ Implicit Casting:
▪ Automatic conversion of a smaller data type into a larger data type.
▪ Explicit Casting:
▪ Manual conversion of a larger data type into a smaller data type.
❑ Type Casting
▪ Implicit Casting:
▪ Automatic conversion of a smaller data type into a larger data type.
▪ Explicit Casting:
▪ Manual conversion of a larger data type into a smaller data type.
❑ Conditional Statements
▪ if Statement:
▪ if-else Statement:
❑ Conditional Statements
▪ if-else if Statement:
❑ Conditional Statements
▪ switch Statement:
❑ Conditional Statements
▪ Nested if Statement:
❑ Exercises
1. Write a program to print your name, age, and favorite programming language
using Escape Sequence ?
2. Write a program to declare two integer variables, assign values to them, and
print their sum, difference, and product ?
3. Prompt the user to enter their weight (in kilograms) and height (in meters).
Calculate and print their Body Mass Index (BMI) using:
𝑤𝑒𝑖𝑔ℎ𝑡
BMI = ?
ℎ𝑒𝑖𝑔ℎ𝑡2
4. Write a Java Program to Check if a Number is Even or Odd ?
5. Write a program to calculate the area of a circle when the radius is entered by
the user ?
6. Write a program to find the largest of three numbers entered by the user ?
7. Write a program that takes a student's score and prints the grade ?
8. Write a program that performs addition, subtraction, multiplication, or division
based on user input ?
❑ Exercises
9. Write a program to check if a given year is a leap year. A year is a leap year if: It
is divisible by 4 but not by 100, or It is divisible by 400?
10. Write a program to classify a number as:
- Positive or Negative - Even or Odd ?
11. Write a program to calculate simple interest:
𝑃𝑟𝑖𝑛𝑐𝑖𝑝𝑎𝑙×𝑅𝑎𝑡𝑒×𝑇𝑖𝑚𝑒
Simple Interest =
100
Prompt the user for the principal amount, rate of interest, and time ?
12. Write a program that asks the user for a username and password, use if-else to
check if the entered values match pre-defined ?
13. Write a program to accept three side lengths of a triangle. Determine if the
triangle is: - Equilateral (all sides equal), - Isosceles (two sides equal), -
Scalene (no sides equal) ?
14. Write a program that accepts a number from 1 to 7 and prints the corresponding
day of the week using the switch statement ?
❑ Exercises
15. Write a program that accepts three angles of a triangle and checks if the triangle is
valid. A triangle is valid if the sum of its angles is 180 degrees ?
16. Write a program that checks if a given number is positive, divisible by 5, and less
than 100 using logical operators ?
17. Create a program that accepts principal, rate, and time, and calculates compound
interest::
𝑅𝑎𝑡𝑒 𝑇𝑖𝑚𝑒
Compound Interest = 𝑃𝑟𝑖𝑛𝑐𝑖𝑝𝑎𝑙 × 1 + – Principal ?
100
18. Write a program that asks the user for their birth year and calculates their age ?
19. Create a program using a switch statement to simulate a traffic light. Input values:
1: "Stop“ 2: "Ready“ 3: "Go" ?
20. Write a Java program to calculate the water bill based on the following conditions:
• First 50 cubic meters: $0.50/m³.
• Next 50 cubic meters (51-100): $0.75/m³
• Above 100 cubic meters: $1.00/m³.
• Fixed charge: $5.
𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
22
Overview
❑ Exercises
21. Write a program to check whether a given character is:
• First 100 kWh: $0.25/kWh.
• Next 100 kWh (101-200): $0.50/kWh.
• Above 200 kWh: $0.75/kWh..
• Fixed charge: $10.
22. Create a program to calculate the final price after a discount:
If the purchase is:
• ≤$100: No discount.
• Between $100 and $500: 10% discount.
• ≥$500: 20% discount
23. Calculate a bonus for employees based on the following:
If the employee has worked for more than 5 years:
• And their salary is < $50,000: Bonus = 20% of salary.
• Otherwise, Bonus = 10% of salary.
24. Write a program to check whether a given character is:
- A vowel - A consonant. - A digit
𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
23
Overview
❑ Loops in Java
▪ Loops allow repeated execution of a block of code until a specific
condition is met.
▪ Importance of loops:
• Reduces code redundancy
• Reduces code redundancy
▪ Types of loops in Java:
• for loop: Used when the number of iterations is known
beforehand.
• while loop: Used when the condition for iteration is evaluated
at the start of the loop.
• do-while loop: Ensures the code block is executed at least
once before checking the condition.
❑ for Loop
▪ Syntax:
▪ Example:
❑ for Loop
▪ Iterating Backward: Print numbers from 5 to 1
❑ for Loop
▪ Using Multiple Variables: Iterate using two variables
❑ for Loop
▪ Prime Numbers in a Range: Find and print prime numbers
between 1 and 20
❑ while Loop
▪ Syntax:
▪ Example:
❑ do-while Loop
▪ Syntax:
▪ Example:
THE END
31
𝓔𝓷𝓰
𝓔𝓷 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭
𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
31