OOP Lab Final Suggestions
OOP Lab Final Suggestions
1. Polymorphism
Question 1:
Write a Java program that demonstrates method overriding. Create a class Vehicle with a method start(), and then create
two subclasses Car and Bike that override the start() method.
class Vehicle {
void start() {
System.out.println("Vehicle is starting");
@Override
void start() {
System.out.println("Car is starting");
@Override
void start() {
System.out.println("Bike is starting");
vehicle.start();
vehicle.start();
}
Question 2:
Write a Java program that uses method overloading. Create a class Calculator with methods add() that can handle both
integers and floating-point numbers.
class Calculator {
return a + b;
return a + b;
Question 3:
Create a program that demonstrates runtime polymorphism. Write a base class Employee with a method calculateSalary().
Then, create subclasses FullTimeEmployee and PartTimeEmployee that override the calculateSalary() method.
class Employee {
void calculateSalary() {
System.out.println("Calculating salary for general employee");
@Override
void calculateSalary() {
@Override
void calculateSalary() {
Employee employee;
employee.calculateSalary();
employee.calculateSalary();
}
Question 4:
Write a Java program where you have a superclass Shape with a method area(). Create subclasses Circle and Rectangle
that implement the area() method, and display the area of each shape.
int radius = 5;
@Override
void area() {
@Override
void area() {
shape1.area();
shape2.area();
}
Question 5:
Create a Java program where you have an interface Playable with a method play(). Create two classes Guitar and Drums
that implement the Playable interface and override the play() method.
interface Playable {
void play();
@Override
System.out.println("Guitar is playing");
@Override
playable1.play();
playable2.play();
}
2. Abstraction
Question 1:
Write a Java program that uses an abstract class Appliance with an abstract method turnOn(). Create two subclasses
WashingMachine and Refrigerator that implement the turnOn() method.
@Override
void turnOn() {
@Override
void turnOn() {
appliance1.turnOn();
appliance2.turnOn();
}
Question 2:
Create a program where you define an abstract class BankAccount with an abstract method calculateInterest(). Then,
create two concrete classes SavingsAccount and CurrentAccount that provide their implementations of calculateInterest().
@Override
void calculateInterest() {
@Override
void calculateInterest() {
account1.calculateInterest();
account2.calculateInterest();
}
Question 3:
Write a Java program to model a generic class Vehicle with an abstract method move(). Create subclasses Car and Bicycle
that implement the move() method to describe their own ways of movement.
@Override
void move() {
@Override
void move() {
vehicle1.move();
vehicle2.move();
}
Question 4:
Create a Java program with an interface Shape with a method draw(). Implement the draw() method in the concrete
classes Circle and Rectangle.
interface Shape {
@Override
System.out.println("Drawing a Circle");
@Override
System.out.println("Drawing a Rectangle");
shape1.draw();
shape2.draw();
}
Question 5:
Write a program to simulate a PaymentSystem using an interface PaymentMethod with a method processPayment().
Implement subclasses CreditCard and PayPal with specific payment processing logic.
interface PaymentMethod {
@Override
@Override
payment1.processPayment();
payment2.processPayment();
}
3. Exception Handling
Question 1:
Write a Java program that handles an ArithmeticException by catching it and displaying an appropriate message when
dividing by zero.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
Question 2:
Write a Java program that reads a number from the user and catches a NumberFormatException if the input is not a valid
integer.
import java.util.Scanner;
try {
} catch (NumberFormatException e) {
}
Question 3:
Write a Java program that handles multiple exceptions in a single catch block. For example, handle ArithmeticException
and NullPointerException in the same catch block.
try {
str.length(); // NullPointerException
Question 4:
Create a program that uses a finally block to close a database connection, even if an exception occurs during the database
operation.
import java.sql.*;
try {
} catch (SQLException e) {
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Connection closed");
} catch (SQLException e) {
4. Threads
Question 1:
Write a Java program that creates a thread by extending the Thread class. In the run() method, print numbers from 1 to 10
with a delay of 1 second between each print.
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
thread.start();
}
Question 2:
Create a program that creates two threads using the Runnable interface. One thread should print even numbers from 1 to
20, and the other thread should print odd numbers from 1 to 20.
System.out.println(i);
System.out.println(i);
evenThread.start();
oddThread.start();
Question 3:
Write a Java program that uses Thread.sleep() to simulate a process that pauses for 5 seconds before continuing. Print a
message before and after the sleep.
public class SleepTest {
System.out.println("Before sleep");
try {
} catch (InterruptedException e) {
System.out.println(e);
System.out.println("After sleep");
Question 4:
Write a Java program that creates two threads. The first thread should print numbers from 1 to 5, and the second thread
should print letters from 'A' to 'E'. Use Thread.join() to ensure the first thread completes before the second thread starts.
System.out.println(i);
thread.start();
System.out.println("Thread completed");