1.
Electricity Bill program in Java
import java.util.*;
class ElectricityBill {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Consumer No: ");
int cno = sc.nextInt();
sc.nextLine();
System.out.print("Enter Consumer Name: ");
String name = sc.nextLine();
System.out.print("Enter Connection Type (domestic/commercial): ");
String type = sc.nextLine();
System.out.print("Enter Previous Reading: ");
double prev = sc.nextDouble();
System.out.print("Enter Current Reading: ");
double curr = sc.nextDouble();
double units = curr - prev, amt = 0;
if (type.equalsIgnoreCase("domestic")) {
if (units <= 100) amt = units * 1;
else if (units <= 200) amt = 100 * 1 + (units - 100) * 2.5;
else if (units <= 500) amt = 100 * 1 + 100 * 2.5 + (units - 200) * 4;
else amt = 100 * 1 + 100 * 2.5 + 300 * 4 + (units - 500) * 6;
} else {
if (units <= 100) amt = units * 2;
else if (units <= 200) amt = 100 * 2 + (units - 100) * 4.5;
else if (units <= 500) amt = 100 * 2 + 100 * 4.5 + (units - 200) * 6;
else amt = 100 * 2 + 100 * 4.5 + 300 * 6 + (units - 500) * 7;
System.out.println("\n--- Electricity Bill ---");
System.out.println("Consumer No : " + cno);
System.out.println("Consumer Name : " + name);
System.out.println("Type : " + type);
System.out.println("Units Used : " + units);
System.out.println("Total Amount : Rs. " + amt);
---
Sample Output
Enter Consumer No: 101
Enter Consumer Name: Hemalatha
Enter Connection Type (domestic/commercial): domestic
Enter Previous Reading: 1200
Enter Current Reading: 1350
--- Electricity Bill ---
Consumer No : 101
Consumer Name : Hemalatha
Type : domestic
Units Used : 150.0
Total Amount : Rs. 275.0
2.Contact Management System using ArrayList
import java.util.*;
class ContactList {
public static void main(String[] args) {
ArrayList<String> contacts = new ArrayList<>();
contacts.add("Hemalatha");
contacts.add("Priya");
contacts.add("Meena");
System.out.println("All Contacts: " + contacts);
// Search a contact
String search = "Priya";
if (contacts.contains(search))
System.out.println(search + " found in list.");
// Update a contact
contacts.set(1, "Kaviya");
System.out.println("After update: " + contacts);
// Delete a contact
contacts.remove("Hemalatha");
System.out.println("After delete: " + contacts);
---
Sample Output
All Contacts: [Hemalatha, Priya, Meena]
Priya found in list.
After update: [Hemalatha, Kaviya, Meena]
After delete: [Kaviya, Meena]
3.Calculate Employee Salaries by Applying the Principles of Inheritance
class Employee {
String name, empId;
double basicPay;
Employee(String n, String id, double bp) {
name = n;
empId = id;
basicPay = bp;
void display() {
System.out.println("Name: " + name);
System.out.println("Emp ID: " + empId);
System.out.println("Basic Pay: " + basicPay);
class Programmer extends Employee {
Programmer(String n, String id, double bp) {
super(n, id, bp);
void calculateSalary() {
double da = 0.97 * basicPay;
double hra = 0.10 * basicPay;
double pf = 0.12 * basicPay;
double club = 0.10 * basicPay;
double gross = basicPay + da + hra;
double net = gross - pf - club;
display();
System.out.println("Designation: Programmer");
System.out.println("Gross Salary: " + gross);
System.out.println("Net Salary: " + net);
class EmployeeSalary {
public static void main(String[] args) {
Programmer p = new Programmer("Hemalatha", "E101", 20000);
p.calculateSalary();
---
Sample Output
Name: Hemalatha
Emp ID: E101
Basic Pay: 20000.0
Designation: Programmer
Gross Salary: 43400.0
Net Salary: 41000.0
4A.Implement the Stack Abstract Data Type (ADT) for Text Editor Application using an
Interface”
import java.util.*;
interface StackADT {
void push(String text);
String pop();
class TextEditor implements StackADT {
Stack<String> stack = new Stack<>();
String content = "";
public void push(String text) {
stack.push(content);
content += text;
public String pop() {
if (!stack.isEmpty())
content = stack.pop();
else
System.out.println("Nothing to undo!");
return content;
void display() {
System.out.println("Current Text: " + content);
public class EditorMain {
public static void main(String[] args) {
TextEditor editor = new TextEditor();
editor.push("Hello ");
editor.push("World!");
editor.display();
System.out.println("Undoing last action...");
editor.pop();
editor.display();
}
---
Sample Output
Current Text: Hello World!
Undoing last action...
Current Text: Hello
4B.Construct a Robust Payroll System with Exception Handling”
import java.util.*;
class PayrollSystem {
Stack<Double> history = new Stack<>();
void calculateSalary(double basicPay) {
try {
if (basicPay <= 0)
throw new ArithmeticException("Invalid Basic Pay!");
double da = 0.5 * basicPay;
double hra = 0.2 * basicPay;
double pf = 0.12 * basicPay;
double gross = basicPay + da + hra;
double net = gross - pf;
history.push(net);
System.out.println("Gross Salary: " + gross);
System.out.println("Net Salary : " + net);
catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
void undoLast() {
if (!history.isEmpty()) {
System.out.println("Undo last salary: " + history.pop());
} else {
System.out.println("No previous records to undo.");
public class PayrollMain {
public static void main(String[] args) {
PayrollSystem p = new PayrollSystem();
p.calculateSalary(20000);
p.calculateSalary(-5000); // Invalid input
p.undoLast();
---
Sample Output
Gross Salary: 34000.0
Net Salary : 31600.0
Error: Invalid Basic Pay!
Undo last salary: 31600.0
5.Calculate Area of Different Shapes by Implementing Abstract Class and Method”
abstract class Shape {
int a, b;
abstract void printArea();
class Rectangle extends Shape {
Rectangle(int x, int y) {
a = x;
b = y;
void printArea() {
System.out.println("Area of Rectangle: " + (a * b));
class Triangle extends Shape {
Triangle(int x, int y) {
a = x;
b = y;
void printArea() {
System.out.println("Area of Triangle: " + (0.5 * a * b));
class Circle extends Shape {
int r;
Circle(int x) {
r = x;
void printArea() {
System.out.println("Area of Circle: " + (3.14 * r * r));
public class ShapeMain {
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 10);
Triangle t = new Triangle(4, 6);
Circle c = new Circle(3);
r.printArea();
t.printArea();
c.printArea();
---
Sample Output
Area of Rectangle: 50
Area of Triangle: 12.0
Area of Circle: 28.26
6.File Handling for Student Record Management System
import java.io.*;
import java.util.*;
class StudentRecord {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File file = new File("students.txt");
FileWriter fw = new FileWriter(file, true); // append mode
BufferedReader br = new BufferedReader(new FileReader(file));
System.out.println("1. Add Record");
System.out.println("2. View Records");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
sc.nextLine(); // clear buffer
switch (ch) {
case 1:
System.out.print("Enter Student Name: ");
String name = sc.nextLine();
System.out.print("Enter Roll No: ");
int roll = sc.nextInt();
fw.write("Name: " + name + ", Roll: " + roll + "\n");
fw.close();
System.out.println("Record Added Successfully!");
break;
case 2:
System.out.println("\n--- Student Records ---");
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
br.close();
break;
default:
System.out.println("Invalid choice!");
---
Output:
Case 1 – Adding Record
1. Add Record
2. View Records
Enter your choice: 1
Enter Student Name: Hemalatha
Enter Roll No: 101
Record Added Successfully!
Case 2 – Viewing Records
1. Add Record
2. View Records
Enter your choice: 2
--- Student Records ---
Name: Hemalatha, Roll: 101
7.Multithreading (Odd and Even Numbers)
class OddThread extends Thread {
public void run() {
System.out.println("Odd Numbers:");
for (int i = 1; i <= 10; i += 2)
System.out.println(i);
class EvenThread extends Thread {
public void run() {
System.out.println("Even Numbers:");
for (int i = 2; i <= 10; i += 2)
System.out.println(i);
public class OddEvenThread {
public static void main(String[] args) {
new OddThread().start();
new EvenThread().start();
---
Output (Example):
Odd Numbers:
Even Numbers:
10
8.Generic Function to Find Maximum Element
class GenericMax {
// Generic method to find maximum
public static <T extends Comparable<T>> T findMax(T a, T b, T c) {
T max = a;
if (b.compareTo(max) > 0) max = b;
if (c.compareTo(max) > 0) max = c;
return max;
}
public static void main(String[] args) {
System.out.println("Max Integer: " + findMax(10, 25, 15));
System.out.println("Max Float: " + findMax(3.5f, 7.2f, 5.9f));
System.out.println("Max String: " + findMax("Apple", "Mango", "Banana"));
---
Output:
Max Integer: 25
Max Float: 7.2
Max String: Mango
9.Student Course Registration System
import java.sql.*;
class StudentRegistration {
public static void main(String[] args) {
try {
// 1. Load JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. Connect to database
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/studentdb", "root", "password");
// 3. Insert student record
Statement stmt = con.createStatement();
String sql = "INSERT INTO student VALUES (1, 'Hemalatha', 'Java Programming')";
stmt.executeUpdate(sql);
System.out.println(" Student record inserted successfully!");
// 4. Retrieve data
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3));
// 5. Close connection
con.close();
} catch (Exception e) {
System.out.println(" Error: " + e);
}
}
---
Output:
Student record inserted successfully!
1 Hemalatha Java Programming