Java Lab Manual (Aids)
Java Lab Manual (Aids)
Java programming
LAB MANUAL
Regulation : R22/JNTUH
Academic Year : 2023-2024
II B. TECH I SEMESTER
2. Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance,
Polymorphism and Abstraction]
// Encapsulation: Using private fields and public methods to access them
class BankAccount {
private double balance;
// Inheritance
SavingsAccount savingsAccount = new SavingsAccount(5000, 0.05);
savingsAccount.applyInterest();
System.out.println("Savings Account Balance: " + savingsAccount.getBalance());
// Polymorphism
Shape shape = new Circle(5.0);
System.out.println("Area of the Circle: " + shape.calculateArea());
// Abstraction
// Shape shape = new Shape(); // Error: Cannot instantiate an abstract class
}
}
OUTPUT:
3. Write a Java program to handle checked and unchecked exceptions. Also, demonstrate the usage
of custom exceptions in real time scenario.
PROGRAM:
// Custom Checked Exception
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
class BankAccount {
private double balance;
try {
account.withdraw(1500); // This will throw InsufficientBalanceException
} catch (InsufficientBalanceException e) {
System.out.println("Error: " + e.getMessage());
}
try {
account.deposit(-500); // This will throw InvalidInputException
} catch (InvalidInputException e) {
System.out.println("Error: " + e.getMessage());
}
5. Write a Java program to demonstrate the working of different collection classes. [Use package
structure to store multiple classes].
PROGRAM:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
// Demonstrating HashMap
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
System.out.println("HashMap Elements: " + hashMap);
// Demonstrating HashSet
HashSet<String> hashSet = new HashSet<String>();
hashSet.add("Red");
hashSet.add("Green");
hashSet.add("Blue");
System.out.println("HashSet Elements: " + hashSet);
}
}
OUTPUT:
6. Write a program to synchronize the threads acting on the same object. [Consider the example of
any reservations like railway, bus, movie ticket booking, etc.]
PROGRAM:
import java.util.concurrent.locks.ReentrantLock;
class TicketBooking {
private int availableSeats;
private final ReentrantLock lock;
public TicketBooking(int totalSeats) {
availableSeats = totalSeats;
lock = new ReentrantLock();
}
customer1.start();
customer2.start();
customer3.start();
}
}
OUTPUT:
8. Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the
digits and for the +, -,*, % operations. Add a text field to display the result. Handle any possible
exceptions like divided by zero
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public CalculatorFrame() {
setTitle("Simple Calculator");
setLayout(new BorderLayout());
setSize(300, 400);
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
num1 = 0;
operator = "";
isNewInput = true;
}
if (isNewInput) {
display.setText("");
isNewInput = false;
}
if (input.matches("[0-9]")) {
display.setText(display.getText() + input);
} else if (input.equals("C")) {
num1 = 0;
operator = "";
display.setText("");
} else if (input.matches("[/+\\-*]")) {
num1 = Double.parseDouble(display.getText());
operator = input;
isNewInput = true;
} else if (input.equals("=")) {
try {
double num2 = Double.parseDouble(display.getText());
double result = calculate(num1, num2, operator);
display.setText(String.valueOf(result));
isNewInput = true;
} catch (NumberFormatException e) {
display.setText("Error");
} catch (ArithmeticException e) {
display.setText("Divide by zero");
}
}
}