INDEX
S. No. Name of the program Page No. Signature
1. WAP to show simple java program. 2
2. WAP to use two classes. 3
3. WAP to implement IF statement. 4
4. WAP to implement IF-ELSE statement. 5
5. WAP to Else-IF ladder statement to 6-7
Check Division.
6. WAP for switch statement to identify 8-9
grades of student.
7. WAP to use While loop. 10
8. WAP to use Do-While loop. 11
9. WAP to implement For loop. 12
10. WAP to implement Nested For loop. 13-14
11. WAP to implement Method Overloading. 15
12. WAP to implement Method Overriding. 16-17
13. WAP to show Constructor. 18
14. WAP to show Constructor Overloading. 19
15. WAP to implement Single Inheritance. 20
16. WAP to implement Multilevel 21-22
Inheritance.
17. WAP to show use of this keyword. 23
18. WAP to show use of super keyword. 24-25
19. WAP to implement abstract method and 26
classes.
20. WAP to implement Interfaces. 27-28
Page 1 of 35
21. WAP to user define package. 29-30
22. WAP to create a Thread. 31
23. WAP of exception handling using try 32
catch block.
24. WAP to use finally statement. 33
25. WAP to throw an exception. 34
PROGRAM 1
WAP to show simple java program.
public class SimpleJavaProgram {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Output :-
Page 2 of 35
PROGRAM 2
WAP to use two classes.
class Message {
void show() {
System.out.println("Hello from Message class!");
}
}
public class Main {
public static void main(String[] args) {
Message msg = new Message();
msg.show();
}
}
Output :-
Page 3 of 35
Program 3
WAP to implement IF statement.
import java.util.Scanner;
public class IfStatement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num > 0) {
System.out.println("Number is positive");
}
sc.close();
}
}
Output :-
Page 4 of 35
Program 4
WAP to implement IF-ELSE statement.
import java.util.Scanner;
public class IfElseStatement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num >= 0) {
System.out.println("Positive Number");
} else {
System.out.println("Negative Number");
}
sc.close();
}
}
Page 5 of 35
Output :-
Program 5
WAP to else-if ladder statement to check division.
import java.util.Scanner;
public class Divisioncheck {
public Divisioncheck() {
}
public static void main(String[] var0) {
Scanner var1 = new Scanner(System.in);
System.out.print("Enter your marks: ");
int var2 = var1.nextInt();
if (var2 >= 60) {
System.out.println("First Division");
} else if (var2 >= 50) {
System.out.println("Second Division");
} else if (var2 >= 33) {
Page 6 of 35
System.out.println("Third Division");
} else {
System.out.println("Fail");
}
var1.close();
}
}
Output :-
Page 7 of 35
Page 8 of 35
Program 6
WAP for switch statement to identify grades of student.
import java.util.Scanner;
public class SwitchExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your percentage: ");
int percentage = sc.nextInt();
switch (percentage / 10) {
case 10:
case 9:
System.out.println("Grade: A (Excellent)");
break;
case 8:
System.out.println("Grade: B (Good)");
break;
case 7:
System.out.println("Grade: C (Average)");
break;
case 6:
System.out.println("Grade: D (Below Average)");
break;
default:
if (percentage >= 0 && percentage <= 59)
System.out.println("Grade: F (Fail)");
else
Page 9 of 35
System.out.println("Invalid percentage");
}
sc.close();
}
}
Output :-
Page 10 of 35
Program 7
WAP to use While loop.
import java.util.Scanner;
public class WhileLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the starting number: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number: ");
int end = scanner.nextInt();
while (start <= end) {
System.out.println(start);
start++;
}
scanner.close();
}
}
Output :-
Page 11 of 35
Program 8
WAP to implement do-while loop.
import java.util.Scanner;
public class DoWhileLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the starting number: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number: ");
int end = scanner.nextInt();
do {
System.out.println(start);
start++;
} while (start <= end);
scanner.close();
}
}
Output :-
Page 12 of 35
Program 9
WAP to implement for loop.
import java.util.Scanner;
public class ForLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the starting number: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number: ");
int end = scanner.nextInt();
for (int i = start; i <= end; i++) {
System.out.println(i);
}
scanner.close();
}
}
Output :-
Page 13 of 35
Program 10
WAP to implement nested for loop.
import java.util.Scanner;
public class NestedForLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
System.out.print("Enter the number of layers: ");
int layers = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
for (int k = 1; k <= layers; k++) {
System.out.print("* ");
}
System.out.print(" ");
}
System.out.println();
}
Page 14 of 35
scanner.close();
}
}
Output :-
Page 15 of 35
Program 11
WAP to implement Method Overloading.
public class OverloadingExample {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(double a) {
System.out.println("Double: " + a);
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
obj.show(10);
obj.show(10.5);
}
Output :-
Page 16 of 35
Program 12
WAP to implement Method Overriding.
class Vehicle {
void speed() {
System.out.println("The vehicle is moving.");
}
}
class Car extends Vehicle {
void speed() {
System.out.println("The car is moving at 100 km/h.");
}
}
class Bike extends Vehicle {
void speed() {
System.out.println("The bike is moving at 60 km/h.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myVehicle = new Vehicle();
myVehicle.speed();
Vehicle myCar = new Car();
myCar.speed();
Page 17 of 35
Vehicle myBike = new Bike();
myBike.speed();
}
}
Output :-
Page 18 of 35
Program 13
WAP to show Constructor.
class ConstructorExample {
ConstructorExample() {
System.out.println("Constructor is called");
}
public static void main(String[] args) {
new ConstructorExample();
}
}
Output :-
Page 19 of 35
Program 14
WAP to show Constructor Overloading.
class ConstructorOverloading {
ConstructorOverloading() {
System.out.println("Default Constructor");
}
ConstructorOverloading(int a) {
System.out.println("Parameterized Constructor: " + a);
}
public static void main(String[] args) {
new ConstructorOverloading();
new ConstructorOverloading(10);
}
}
Output :-
Page 20 of 35
Program 15
WAP to implement Single Inheritance.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat();
myDog.bark();
}
}
Output :-
Page 21 of 35
Program 16
WAP to implement Multilevel Inheritance.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping");
}
}
public class MultilevelInheritance {
public static void main(String[] args) {
Puppy myPuppy = new Puppy();
myPuppy.eat(); // Inherited from Animal
myPuppy.bark(); // Inherited from Dog
myPuppy.weep(); // Puppy class method
Page 22 of 35
}
}
Output :-
Page 23 of 35
Program 17
WAP to show use of ‘this’ keyword.
class ThisExample {
int a;
ThisExample(int a) {
this.a = a;
}
void display() {
System.out.println("Value: " + this.a);
}
public static void main(String[] args) {
ThisExample obj = new ThisExample(10);
obj.display();
}
}
Output :-
Page 24 of 35
Program 18
WAP to show use of ‘super’ keyword.
class Vehicle {
String type = "General Vehicle";
Vehicle() {
System.out.println("Vehicle constructor called");
}
void display() {
System.out.println("This is a vehicle");
}
}
class Car extends Vehicle {
String type = "Car";
Car() {
super();
System.out.println("Car constructor called");
}
void display() {
super.display();
System.out.println("This is a car");
}
Page 25 of 35
void showType() {
System.out.println("Child class type: " + type);
System.out.println("Parent class type: " + super.type);
}
}
public class SuperExample {
public static void main(String[] args) {
Car myCar = new Car();
myCar.display();
myCar.showType();
}
}
Output :-
Page 26 of 35
Program 19
WAP to implement abstract method and classes.
abstract class Animal {
abstract void makeSound();
void sleep() {
System.out.println("Animal is sleeping");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class AbstractExample {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
myDog.sleep();
}
}
Output :-
Page 27 of 35
Program 20
WAP to implement Interfaces.
interface Bank {
void deposit(double amount);
void withdraw(double amount);
}
class SavingsAccount implements Bank {
private double balance = 0;
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount + ", New Balance: " +
balance);
}
public void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient balance!");
} else {
balance -= amount;
System.out.println("Withdrawn: " + amount + ", Remaining Balance:
" + balance);
}
}
}
public class InterfaceExample {
Page 28 of 35
public static void main(String[] args) {
Bank myAccount = new SavingsAccount();
myAccount.deposit(5000);
myAccount.withdraw(2000);
myAccount.withdraw(4000);
}
}
Output :-
Page 29 of 35
Program 21
WAP to user-defined package.
import java.util.ArrayList;
class Book {
String title, author;
double price;
Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
void display() {
System.out.println(title + " by " + author + " - $" + price);
}
}
class User {
String name;
User(String name) {
this.name = name;
}
void borrow(Book book) {
Page 30 of 35
System.out.println(name + " borrowed: " + book.title);
}
}
public class Main {
public static void main(String[] args) {
Book book1 = new Book("Java Programming", "John Doe", 29.99);
Book book2 = new Book("Python Basics", "Jane Smith", 24.99);
ArrayList<Book> library = new ArrayList<>();
library.add(book1);
library.add(book2);
for (Book b : library) b.display();
User user1 = new User("Alice");
user1.borrow(book1);
}
}
Output :-
Page 31 of 35
Program 22
WAP to create a Thread.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread running: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
} } }
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
}}
Output :-
Page 32 of 35
Program 23
WAP of exception handling using try catch block.
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero!");
}
System.out.println("Program continues after exception handling...");
}
}
Output :-
Page 33 of 35
Program 24
WAP to use finally statement.
public class FinallyExample {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero!");
} finally {
System.out.println("Finally block executed!");
}
System.out.println("Program continues after exception handling...");
}
}
Output :-
Page 34 of 35
Program 25
WAP to throw an exception.
public class ThrowExample {
static void validate(int age) {
if (age < 18)
throw new ArithmeticException("Not valid age");
else
System.out.println("Welcome!");
}
public static void main(String[] args) {
validate(16);
}
}
Output :-
Page 35 of 35