Practical 1
1. WAP to print Hello World
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
Output:
1. WAP to perform Addition of numbers
import java.util.Scanner;
public class AddNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = scanner.nextInt();
System.out.println("Enter the second number:");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
}
Output:
2. WAP to calculate Simple Interest
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the principal amount:");
double principal = scanner.nextDouble();
System.out.println("Enter the rate of interest (percentage):");
double rate = scanner.nextDouble();
System.out.println("Enter the time in years:");
double time = scanner.nextDouble();
double interest = (principal * rate * time) / 100;
System.out.println("The simple interest is " + interest);
Output:
3. WAP to calculate Perimeter of rectangle
import java.util.Scanner;
public class PerimeterRectangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of the rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of the rectangle:");
double width = scanner.nextDouble();
double perimeter = 2 * (length + width);
System.out.println("The perimeter of the rectangle is " + perimeter);
Output:
Practical no. 2
1:-WAP in java to print even and odd number between 100 to 150 using for
loop.
public class Main {
public static void main(String[] args) {
System.out.println("Even numbers between 100 to 150:");
for (int i = 100; i <= 150; i++) {
if (i % 2 == 0) {
System.out.println(i);
System.out.println("\nOdd numbers between 100 to 150:");
for (int i = 100; i <= 150; i++) {
if (i % 2 != 0) {
System.out.println(i);
Output:-
2. WAP in java to print Fibonacci series upto 100 using while loop.
public class Main {
public static void main(String[] args) {
int first = 0;
int second = 1;
int next = 0;
System.out.println("Fibonacci series up to 100:");
System.out.print(first + ", " + second);
while ((next = first + second) < 100) {
System.out.print(", " + next);
first = second;
second = next;
Output:-
3. WAP in java to print prime number between 1 to 50
public class Main {
public static void main(String[] args) {
System.out.println("Prime numbers between 1 and 50:");
for (int i = 2; i <= 50; i++) {
if (isPrime(i)) {
System.out.print(i + ", ");
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
return true;
Output:-
4. WAP in java to check whether number is palindrome or not.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int number = scanner.nextInt();
System.out.println("The number " + number + (isPalindrome(number) ? " is a palindrome" : "
is not a palindrome"));
public static boolean isPalindrome(int number) {
int reversedNumber = 0;
int originalNumber = number;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
return originalNumber == reversedNumber;
Output:-
5. WAP in java to check whether number is armstrong or not.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int number = scanner.nextInt();
System.out.println("The number " + number + (isArmstrong(number) ? " is an Armstrong
number" : " is not an Armstrong number"));
public static boolean isArmstrong(int number) {
int originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
return number == result;
Output:-
1. Java Array Program to Find the Largest Element in an Array
public class FindLargestElement {
public static void main(String[] args) {
int[] numbers = {11, 40, 15, 25, 60, 30};
int largest = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
System.out.println("The largest element in the array is: " + largest);
Output:
2. Java Array Program to Copy All the Elements of One Array to Another Array
public class CopyArrayElements {
public static void main(String[] args) {
int[] sourceArray = {1, 2, 3, 4, 5};
int[] destinationArray = new int[sourceArray.length];
for (int i = 0; i < sourceArray.length; i++) {
destinationArray[i] = sourceArray[i];
System.out.print("Elements in the destination array: ");
for (int i = 0; i < destinationArray.length; i++) {
System.out.print(destinationArray[i] + " ");
Output:
3. Java Array Program to Check Whether Two Matrices Are Equal or Not
import java.util.Scanner;
public class MatrixEquality {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int columns = sc.nextInt();
int[][] matrix1 = new int[rows][columns];
int[][] matrix2 = new int[rows][columns];
System.out.println("Enter the elements of the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix1[i][j] = sc.nextInt();
}
System.out.println("Enter the elements of the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix2[i][j] = sc.nextInt();
boolean isEqual = true;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (matrix1[i][j] != matrix2[i][j]) {
isEqual = false;
break;
if (!isEqual) {
break;
if (isEqual) {
System.out.println("The matrices are equal.");
} else {
System.out.println("The matrices are not equal.");
sc.close();
Output:
4. Java Array Program to Find the Transpose
public class TransposeArray {
public static void main(String[] args) {
int[][] originalArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int rows = originalArray.length;
int cols = originalArray[0].length;
int[][] transposedArray = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposedArray[j][i] = originalArray[i][j];
System.out.println("Original Array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(originalArray[i][j] + " ");
System.out.println();
System.out.println("Transposed Array:");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transposedArray[i][j] + " ");
System.out.println();
Output:
5. Java Array Program to Search an Element in an Array
import java.util.Scanner;
public class SearchElementInArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 5, 45, 50, 30};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the element to search for: ");
int target = scanner.nextInt();
scanner.close();
boolean found = false;
int position = -1;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == target) {
found = true;
position = i;
break;
if (found) {
System.out.println("Element " + target + " is found at position " + position + " in the array.");
} else {
System.out.println("Element " + target + " is not found in the array.");
Output:
6. Java Array Program for Bubble Sort
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {62, 24, 35, 22, 26, 10, 80};
int n = arr.length;
System.out.println("Original Array:");
printArray(arr);
bubbleSort(arr);
System.out.println("\nSorted Array:");
printArray(arr);
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
Output:
Practical 4
1. Java Program to Count Number of Objects Created for Class Passing and
Returning Objects in Java
class ClassCounter {
private static int classCount = 0;
ClassCounter() {
classCount++;
}
static int getClassCount() {
return classCount;
}
}
class ClassCreator {
static ClassCounter createCounter() {
return new ClassCounter();
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Class Counter Count Before Object Creation: " +
ClassCounter.getClassCount());
ClassCounter counter1 = ClassCreator.createCounter();
System.out.println("Class Counter Count After First Object Creation: " +
ClassCounter.getClassCount());
ClassCounter counter2 = ClassCreator.createCounter();
System.out.println("Class Counter Count After Second Object Creation: " +
ClassCounter.getClassCount());
ClassCounter counter3 = ClassCreator.createCounter();
System.out.println("Class Counter Count After Third Object Creation: " +
ClassCounter.getClassCount());
}
}
Output:-
2. Java Program to Illustrate Use of Methods in a Class. Create different
methods to perform different arithmetic operations.
public class Calculator {
// Method to add two numbers
public static int add(int num1, int num2) {
return num1 + num2;
}
// Method to subtract two numbers
public static int subtract(int num1, int num2) {
return num1 - num2;
}
// Method to multiply two numbers
public static int multiply(int num1, int num2) {
return num1 * num2;
}
// Method to divide two numbers
public static double divide(int num1, int num2) {
if (num2 == 0) {
throw new IllegalArgumentException("Divisor cannot be zero");
}
return (double) num1 / num2;
}
public static void main(String[] args) {
System.out.println("Addition: " + add(10, 20));
System.out.println("Subtraction: " + subtract(100, 50));
System.out.println("Multiplication: " + multiply(6, 7));
System.out.println("Division: " + divide(100, 10));
}
}
Output:-
3. Java Program to Create a Method without Parameters and with
Return Type. Create method to calculate the volume of a cuboid
which takes the dimensions length, breadth and height as input and
return the volume as output back to the main method.
public class Cuboid {
// Method to calculate the volume of a cuboid
public static double calculateVolume(int length, int breadth, int height) {
return length * breadth * height;
}
public static void main(String[] args) {
int length = 5;
int breadth = 4;
int height = 3;
double volume = calculateVolume(length, breadth, height);
System.out.println("The volume of the cuboid is: " + volume);
}
}
Output:-
4. Java Program to Find Area of Square, Rectangle and Circle using
Method Overloading.
public class Shapes {
// Method to calculate area of square
public static double calculateArea(int side) {
return side * side;
}
// Method to calculate area of rectangle
public static double calculateArea(int length, int breadth) {
return length * breadth;
}
// Method to calculate area of circle
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
int side = 5;
int length = 4;
int breadth = 3;
double radius = 2;
double squareArea = calculateArea(side);
double rectangleArea = calculateArea(length, breadth);
double circleArea = calculateArea(radius);
System.out.println("The area of the square is: " + squareArea);
System.out.println("The area of the rectangle is: " + rectangleArea);
System.out.println("The area of the circle is: " + circleArea);
}
}
Output:-
Practical No. 5
1. Create a abstract class employee, having its properties & abstract
function for calculating net salary and displaying the information.
Drive manager & clerk class from this abstract class & implement
the abstract method net salary and override the display method.
import java.util.ArrayList;
abstract class Employee {
private String name;
private int age;
private double salary;
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
public String getName() {
return name;
public int getAge() {
return age;
public double getSalary() {
return salary;
// abstract method for calculating net salary
public abstract double calculateNetSalary();
// display method for showing employee information
public void display() {
System.out.println("Employee Name: " + getName());
System.out.println("Employee Age: " + getAge());
System.out.println("Employee Salary: " + getSalary());
System.out.println("Employee Net Salary: " + calculateNetSalary());
class Manager extends Employee {
private double bonus;
public Manager(String name, int age, double salary, double bonus) {
super(name, age, salary);
this.bonus = bonus;
@Override
public double calculateNetSalary() {
return getSalary() + bonus;
@Override
public void display() {
super.display();
System.out.println("Manager Bonus: " + bonus);
class Clerk extends Employee {
private int productivity;
public Clerk(String name, int age, double salary, int productivity) {
super(name, age, salary);
this.productivity = productivity;
@Override
public double calculateNetSalary() {
return getSalary() - (getSalary() * productivity / 100);
@Override
public void display() {
super.display();
System.out.println("Clerk Productivity: " + productivity);
public class Main {
public static void main(String[] args) {
Manager manager = new Manager("Naveen", 21, 50000, 10000);
Clerk clerk = new Clerk("Naveen", 21, 30000, 50);
ArrayList<Employee> employees = new ArrayList<>();
employees.add(manager);
employees.add(clerk);
for (Employee employee : employees) {
employee.display();
System.out.println();
}
Output:-
2. Write a Java program to create a class known as "BankAccount" with
methods called deposit() and withdraw(). Create a subclass called
SavingsAccount that overrides the withdraw() method to prevent
withdrawals if the account balance falls below one hundred.