0% found this document useful (0 votes)
5 views

Java-2 Lecture1

The document provides a comprehensive overview of computer programming using Java, covering topics such as the introduction to Java, program execution stages, general structure of a Java program, comments, escape sequences, variables, data types, user input, operators, type casting, and conditional statements. It also includes a series of exercises designed to reinforce the concepts discussed, encouraging practical application of programming skills. Overall, it serves as a foundational guide for beginners in Java programming.

Uploaded by

rayanghalib24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java-2 Lecture1

The document provides a comprehensive overview of computer programming using Java, covering topics such as the introduction to Java, program execution stages, general structure of a Java program, comments, escape sequences, variables, data types, user input, operators, type casting, and conditional statements. It also includes a series of exercises designed to reinforce the concepts discussed, encouraging practical application of programming skills. Overall, it serves as a foundational guide for beginners in Java programming.

Uploaded by

rayanghalib24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

COMPUTER

PROGRAMMING
Level (1) - IT

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


1
GENERAL REVIEW FOR
COMPUTER PROGRAMMING
USING JAVA

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

❑ Program Execution Stages:


▪ Write the Source Code: Written in files with the .java extension.
▪ Compilation: Converts source code into bytecode using the Java
compiler (javac).
▪ Execution: Bytecode is executed using the JVM.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


3
Overview

❑ General Structure of a Java Program

▪ ClassName: The class name must match the file name.


▪ main: The starting point of program execution.
▪ System.out.println(): Used to print text to the console.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


4
Overview

❑ Comments in Java
▪ Single-line comments: Start with // .

▪ Multi-line comments: Enclosed between /* and */.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


5
Overview

❑ 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

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


6
Overview

❑ Variables
▪ Declaring a Variable:

▪ Variable Naming Rules:


• A variable name must begin with a letter (A-Z or a-z), an underscore (_) or ($).
• Variable names cannot contain spaces or special symbols like ( !, +, #).
• Reserved keywords in Java (e.g., class, void, int,….) cannot be used as variable
names.
• Variable names are case-sensitive.
• Use names that clearly indicate the purpose of the variable.
• Variable names must not be duplicated within the same block, method, or class
scope.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


7
Overview

❑ Data Types in Java


Type Size (Bytes) Example
byte 1 127, -128
short 2 32000
int 4 2147483647
long 8 9223372036854775807
float 4 3.14f
double 8 3.14159
char 2 'A', '9'
boolean 1 true, false

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


8
Overview

❑ User Input

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


9
Overview

❑ 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

Assigns the same value


Chained Assignment
to multiple variables.
= x = y = z = 7;

Increases or decreases
x++; // x = x + 1
Increment/Decrement the value of a variable ++, --
by 1. x--; // x = x - 1

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


10
Overview

❑ 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 <=

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


11
Overview

❑ 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

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


12
Overview

❑ 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:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


13
Overview

❑ 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.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


14
Overview

❑ 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.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


15
Overview

❑ Conditional Statements
▪ if Statement:

▪ if-else Statement:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


16
Overview

❑ Conditional Statements
▪ if-else if Statement:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


17
Overview

❑ Conditional Statements
▪ switch Statement:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


18
Overview

❑ Conditional Statements
▪ Nested if Statement:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


19
Overview

❑ 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 ?

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


20
Overview

❑ 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 ?

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


21
Overview

❑ 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.

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


24
Overview

❑ for Loop
▪ Syntax:

▪ Example:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


25
Overview

❑ for Loop
▪ Iterating Backward: Print numbers from 5 to 1

▪ Incrementing by More Than 1: Print even numbers from 2 to 10

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


26
Overview

❑ for Loop
▪ Using Multiple Variables: Iterate using two variables

▪ Nested for Loop: Print a multiplication table

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


27
Overview

❑ for Loop
▪ Prime Numbers in a Range: Find and print prime numbers
between 1 and 20

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


28
Overview

❑ while Loop
▪ Syntax:

▪ Example:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


29
Overview

❑ do-while Loop
▪ Syntax:

▪ Example:

𝓔𝓷𝓰 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪


30
Overview

THE END

31
𝓔𝓷𝓰
𝓔𝓷 𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭
𝓜𝓸𝓱𝓪𝓶𝓶𝓮𝓭 𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
𝓜𝓾𝓽𝓱𝓪𝓷𝓷𝓪
31

You might also like