Advanced Java Decision Making.docx
Advanced Java Decision Making.docx
2024. The latest long-term support (LTS) version is Java 21, released in September
2023. Java 24 is expected to be released in March 2025.
1. Decision-Making Statements
Used to execute code based on conditions.
2. Looping Statements
Used to repeat a block of code.
3. Jump Statements
Used to transfer control to another part of the code.
Example:
public class TernaryExample {
public static void main(String[] args) {
int age = 18;
String result = (age >= 18) ? "Eligible to vote" : "Not eligible";
System.out.println(result);
}
}
Output:
Eligible to vote
Example:
public class EvenOdd {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
System.out.println(i + " is Even");
} else {
System.out.println(i + " is Odd");
}
}
}
}
Output:
1 is Odd
2 is Even
3 is Odd
4 is Even
5 is Odd
Example:
public class LogicalOperators {
public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;
if (age >= 18 && hasLicense) {
System.out.println("You can drive");
} else {
System.out.println("You cannot drive");
}
}
}
Output:
You can drive
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("i = " + i);
}
}
}
Output:
i = 1
i=2
Example:
public class SwitchExpression {
public static void main(String[] args) {
int day = 3;
String dayType = switch (day) {
case 1, 7 -> "Weekend";
case 2, 3, 4, 5, 6 -> "Weekday";
default -> "Invalid";
};
System.out.println(dayType);
}
}
Output:
Weekday
If else if
System.out.println("Grade: A");
System.out.println("Grade: C");
System.out.println("Grade: D");
} else {
System.out.println("Grade: F (Fail)");
1. Encapsulation
Definition: Wrapping data (variables) and methods (functions) into a single unit (class). It
protects data using private access and provides access via public methods.
✅ Example:
class Student {
name = n;
return name;
}
public class Main {
s.setName("Amit");
🔸 Output:
Student Name: Amit
🔷 2. Abstraction
Definition: Hiding internal implementation details and showing only essential features
using abstract classes or interfaces.
CopyEdit
void sound() {
System.out.println("Dog barks");
a.sound();
🔸 Output:
Dog barks
🔷 3. Inheritance
Definition: One class (child) inherits properties and methods of another class (parent)
using the extends keyword.
✅ Example:
class Vehicle {
void start() {
System.out.println("Vehicle starts");
void speed() {
System.out.println("Car is running fast");
🔸 Output:
Vehicle starts
🔷 4. Polymorphism
Definition: Ability of an object to take many forms — method overloading and method
overriding.
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
System.out.println(m.add(10, 20));
🔸 Output:
30
30
void sound() {
void sound() {
System.out.println("Cat meows");
}
a.sound();
🔸 Output:
Cat meows
WORKOUT
Write a Java program to check whether a number is positive or negative using if statement.
Write a Java program to check whether a person is eligible to vote (age ≥ 18) using if-else.
Write a Java program to find the largest of two numbers using if-else.
Write a Java program to determine whether a number is even or odd using if-else.
Marks ≥ 90: A
Marks ≥ 80: B
Marks ≥ 70: C
Marks ≥ 60: D
Below 60: F
Write a Java program to check if a number is divisible by both 5 and 11 using if-else-if.
Write a Java program using nested if to check:If a number is positive, check if it's even or
odd.
Write a Java program using nested if to check login credentials:If username is correct, then
check if password is correct.
9. Write a Java program using switch to print the name of the day based on
number (1–7):
● Take two numbers and an operator (+, -, *, /), and perform the operation.
11.Write a Java program using switch to display the number of days in a month
(use case for months).
12.Write a Java program using switch to display season for a given month
number:
● 12, 1, 2 = Winter
● 3, 4, 5 = Spring
● 6, 7, 8 = Summer
● 9, 10, 11 = Autumn
Create an object
// Define a Car class
class Car {
void startEngine() {
// Main class
myCar.startEngine();
// Define a class
class Student {
// Attributes
String name;
int age;
void displayInfo() {
// Main class
s1.name = "Anu";
s1.age = 21;
s1.displayInfo();