0% found this document useful (0 votes)
20 views15 pages

OOP Lab Final Suggestions

The document contains a series of Java programming exercises focused on Object-Oriented Programming concepts such as polymorphism, abstraction, exception handling, and threading. Each section includes multiple questions with code examples demonstrating method overriding, method overloading, abstract classes, interfaces, exception handling, and thread creation. The exercises are designed to help learners practice and understand key OOP principles in Java.

Uploaded by

Mihoyo Impact
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

OOP Lab Final Suggestions

The document contains a series of Java programming exercises focused on Object-Oriented Programming concepts such as polymorphism, abstraction, exception handling, and threading. Each section includes multiple questions with code examples demonstrating method overriding, method overloading, abstract classes, interfaces, exception handling, and thread creation. The exercises are designed to help learners practice and understand key OOP principles in Java.

Uploaded by

Mihoyo Impact
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

OOP Lab 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");

class Car extends Vehicle {

@Override

void start() {

System.out.println("Car is starting");

class Bike extends Vehicle {

@Override

void start() {

System.out.println("Bike is starting");

public class TestPolymorphism {

public static void main(String[] args) {

Vehicle vehicle = new Car();

vehicle.start();

vehicle = new Bike();

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 {

// Method for adding two integers

int add(int a, int b) {

return a + b;

// Method for adding two floating-point numbers

double add(double a, double b) {

return a + b;

public class CalculatorTest {

public static void main(String[] args) {

Calculator calculator = new Calculator();

System.out.println("Sum of integers: " + calculator.add(5, 3));

System.out.println("Sum of doubles: " + calculator.add(5.5, 3.5));

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");

class FullTimeEmployee extends Employee {

@Override

void calculateSalary() {

System.out.println("Calculating salary for full-time employee");

class PartTimeEmployee extends Employee {

@Override

void calculateSalary() {

System.out.println("Calculating salary for part-time employee");

public class EmployeeTest {

public static void main(String[] args) {

Employee employee;

employee = new FullTimeEmployee();

employee.calculateSalary();

employee = new PartTimeEmployee();

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.

abstract class Shape {

abstract void area();

class Circle extends Shape {

int radius = 5;

@Override

void area() {

System.out.println("Area of Circle: " + (Math.PI * radius * radius));

class Rectangle extends Shape {

int length = 4, width = 6;

@Override

void area() {

System.out.println("Area of Rectangle: " + (length * width));

public class ShapeTest {

public static void main(String[] args) {

Shape shape1 = new Circle();

shape1.area();

Shape shape2 = new Rectangle();

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();

class Guitar implements Playable {

@Override

public void play() {

System.out.println("Guitar is playing");

class Drums implements Playable {

@Override

public void play() {

System.out.println("Drums are playing");

public class PlayableTest {

public static void main(String[] args) {

Playable playable1 = new Guitar();

playable1.play();

Playable playable2 = new Drums();

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.

abstract class Appliance {

abstract void turnOn();

class WashingMachine extends Appliance {

@Override

void turnOn() {

System.out.println("Washing Machine is now ON");

class Refrigerator extends Appliance {

@Override

void turnOn() {

System.out.println("Refrigerator is now ON");

public class ApplianceTest {

public static void main(String[] args) {

Appliance appliance1 = new WashingMachine();

appliance1.turnOn();

Appliance appliance2 = new Refrigerator();

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().

abstract class BankAccount {

abstract void calculateInterest();

class SavingsAccount extends BankAccount {

@Override

void calculateInterest() {

System.out.println("Calculating interest for Savings Account");

class CurrentAccount extends BankAccount {

@Override

void calculateInterest() {

System.out.println("Calculating interest for Current Account");

public class BankTest {

public static void main(String[] args) {

BankAccount account1 = new SavingsAccount();

account1.calculateInterest();

BankAccount account2 = new CurrentAccount();

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.

abstract class Vehicle {

abstract void move();

class Car extends Vehicle {

@Override

void move() {

System.out.println("Car is moving on the road");

class Bicycle extends Vehicle {

@Override

void move() {

System.out.println("Bicycle is moving on the cycle path");

public class VehicleTest {

public static void main(String[] args) {

Vehicle vehicle1 = new Car();

vehicle1.move();

Vehicle vehicle2 = new Bicycle();

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 {

void draw(); // Interface method (no need for 'abstract' keyword)

class Circle implements Shape {

@Override

public void draw() {

System.out.println("Drawing a Circle");

class Rectangle implements Shape {

@Override

public void draw() {

System.out.println("Drawing a Rectangle");

public class ShapeTest {

public static void main(String[] args) {

Shape shape1 = new Circle();

shape1.draw();

Shape shape2 = new Rectangle();

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 {

void processPayment(); // Interface method (no need for 'abstract' keyword)

class CreditCard implements PaymentMethod {

@Override

public void processPayment() {

System.out.println("Processing payment through Credit Card");

class PayPal implements PaymentMethod {

@Override

public void processPayment() {

System.out.println("Processing payment through PayPal");

public class PaymentSystemTest {

public static void main(String[] args) {

PaymentMethod payment1 = new CreditCard();

payment1.processPayment();

PaymentMethod payment2 = new PayPal();

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.

public class ArithmeticExceptionTest {

public static void main(String[] args) {

try {

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Error: Division by zero");

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;

public class NumberFormatExceptionTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

try {

int number = Integer.parseInt(scanner.nextLine());

System.out.println("You entered: " + number);

} catch (NumberFormatException e) {

System.out.println("Error: Invalid number format");

}
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.

public class MultipleExceptionsTest {

public static void main(String[] args) {

try {

int[] array = new int[5];

array[10] = 50; // ArrayIndexOutOfBoundsException

String str = null;

str.length(); // NullPointerException

} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {

System.out.println("Error: " + e.getClass().getSimpleName());

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.*;

public class FinallyTest {

public static void main(String[] args) {

Connection connection = null;

try {

connection = DriverManager.getConnection("jdbc:oracle:thin://localhost:1521:xe", "user", "password");

System.out.println("Database connected successfully");

} catch (SQLException e) {

System.out.println("Error: " + e.getMessage());

} finally {

if (connection != null) {

try {
connection.close();

System.out.println("Connection closed");

} catch (SQLException e) {

System.out.println("Error closing connection");

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.

class MyThread extends Thread {

public void run() {

for (int i = 1; i <= 10; i++) {

System.out.println(i);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

System.out.println(e);

public class ThreadTest {

public static void main(String[] args) {

MyThread thread = new MyThread();

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.

class EvenNumbers implements Runnable {

public void run() {

for (int i = 2; i <= 20; i += 2) {

System.out.println(i);

class OddNumbers implements Runnable {

public void run() {

for (int i = 1; i <= 19; i += 2) {

System.out.println(i);

public class ThreadTest {

public static void main(String[] args) {

Thread evenThread = new Thread(new EvenNumbers());

Thread oddThread = new Thread(new OddNumbers());

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 {

public static void main(String[] args) {

System.out.println("Before sleep");

try {

Thread.sleep(5000); // Sleep for 5 seconds

} 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.

class PrintNumbers implements Runnable {

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println(i);

public class JoinTest {

public static void main(String[] args) throws InterruptedException {

Thread thread = new Thread(new PrintNumbers());

thread.start();

thread.join(); // Main thread waits for this thread to finish

System.out.println("Thread completed");

You might also like