OOPJ (BCS306A) Assignment-02 2025-2026
Program-01
You are required to demonstrate the concept of Java Packages by designing a simple banking
system. Create a package named bank. Inside the package, create a class: Account.
The class should contain: Private variables: accountNumber, balance
A constructor to initialize values
Methods:
deposit(amount)
withdraw(amount)
getBalance()
In the main class (outside the package), import the package and perform the operations.
Sample Input
accountNumber: 101
initialBalance:5000
depositAmount: 2000
withdrawAmount:1500
Sample Output
Balance after deposit: 7000
Balance after withdrawal: 5500
Program
import [Link];
public class Main
public static void main(String[] args)
[Link]("Name:Chinmayi K ");
[Link]("USN:4YG24CS033");
Scanner sc = new Scanner([Link]);
[Link]("Enter Account Number: ");
int accNo = [Link]();
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 1
OOPJ (BCS306A) Assignment-02 2025-2026
[Link]("Enter Initial Balance: ");
double bal = [Link]();
Account acc = new Account(accNo, bal);
[Link]("Enter deposit amount: ");
double dep = [Link]();
[Link](dep);
[Link]("Enter withdrawal amount: ");
double wit = [Link]();
[Link](wit);
[Link]("Current Balance: " +
[Link]());
[Link]();
class Account
private int accountNumber;
private double balance;
Account(int accountNumber, double balance)
[Link] = accountNumber;
[Link] = balance;
void deposit(double amount)
if (amount > 0)
balance += amount;
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 2
OOPJ (BCS306A) Assignment-02 2025-2026
[Link]("Amount Deposited: " + amount);
else
[Link]("Invalid deposit amount");
void withdraw(double amount)
if (amount > 0 && amount <= balance)
balance -= amount;
[Link]("Amount Withdrawn: " + amount);
else
[Link]("Insufficient balance");
double getBalance()
return balance;
Output:
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 3
OOPJ (BCS306A) Assignment-02 2025-2026
Program 02:
You are required to compute the power of a number by implementing a calculator. Create a class
MyCalculator which consists of a single method long power(int, int). This method takes two integers,
n and p, as parameters and finds np. If either n or p is negative, then the method must throw an
exception which says "n and p should not be negative". Also, if both n and p are zero, then the
method must throw an exception which says "n and p should not be zero."
Sample Input 0
35
00
-1 -2
-1 3
Sample Output 0
243
Program:
import [Link];
class MyCalculator
long power(int n, int p) throws Exception
if (n < 0 || p < 0)
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 4
OOPJ (BCS306A) Assignment-02 2025-2026
{
throw new Exception("n and p should not be negative");
if (n == 0 && p == 0)
throw new Exception("n and p should not be zero");
return (long) [Link](n, p);
public class Main
public static void main(String[] args)
[Link]("Name:Chinmayi K ");
[Link]("USN:4YG24CS033");
Scanner sc = new Scanner([Link]);
MyCalculator mc = new MyCalculator();
[Link]("Enter n value: ");
int n = [Link]();
[Link]("Enter p value: ");
int p = [Link]();
try
long result = [Link](n, p);
[Link]("Result: " + result);
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 5
OOPJ (BCS306A) Assignment-02 2025-2026
catch (Exception e)
[Link]([Link]());
[Link]();
Output:
Program 03:
You are given N tasks, each task must print a specific message. However, tasks must run
concurrently using threads, and still ensure that: Each task prints a start message, Then prints its
assigned number, Then prints a completion message, Outputs for each task should appear in order of
task id even though threads run concurrently.
You must ensure correct synchronization so output order remains consistent.
Sample Input
5937
Sample Output
Task 1 started
Number: 5
Task 1 completed
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 6
OOPJ (BCS306A) Assignment-02 2025-2026
Task 2 started
Number: 9
Task 2 completed
Task 3 started
Number: 3
Task 3 completed
Task 4 started
Number: 7
Task 4 completed
Program:
class Task implements Runnable
private static int currentTask = 1;
private int taskId;
Task(int taskId)
[Link] = taskId;
@Override
public void run()
synchronized ([Link])
while (taskId != currentTask)
try
[Link]();
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 7
OOPJ (BCS306A) Assignment-02 2025-2026
}
catch (InterruptedException e)
[Link]();
[Link]("Name:Chinmayi K");
[Link]("USN:4YG24CS033");
[Link]("Task " + taskId + " started");
[Link]("Task number: " + taskId);
[Link]("Task " + taskId + " completed");
currentTask++;
[Link]();
public class Main
public static void main(String[] args)
int N = 5;
for (int i = 1; i <= N; i++)
Thread t = new Thread(new Task(i));
[Link]();
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 8
OOPJ (BCS306A) Assignment-02 2025-2026
Output:
Program 04:
Multiple Hydrogen(H) and Oxygen(O) threads are created randomly. A valid water molecule must
always be formed as:
HHO
Two H must print before one O.
You must synchronize threads so output repeatedly prints:
HHO HHO HHO…
No extra H or O should print without completing a molecule.
Sample Input
Sample Output
HHOHHOHHO
Program:
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 9
OOPJ (BCS306A) Assignment-02 2025-2026
import [Link];
class Water
private Semaphore hydrogen = new Semaphore(2);
private Semaphore oxygen = new Semaphore(0);
public void hydrogen() throws InterruptedException
[Link]();
[Link]("H");
[Link]();
public void oxygen() throws InterruptedException
[Link](2);
[Link]("O ");
[Link](2);
class Hydrogen extends Thread
Water water;
Hydrogen(Water water)
[Link] = water;
public void run()
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 10
OOPJ (BCS306A) Assignment-02 2025-2026
try
[Link]();
catch (InterruptedException e)
[Link]();
class Oxygen extends Thread
Water water;
Oxygen(Water water)
[Link] = water;
public void run()
try
[Link]();
catch (InterruptedException e)
[Link]();
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 11
OOPJ (BCS306A) Assignment-02 2025-2026
}
public class Main
public static void main(String[] args)
[Link]("Name :Chinmayi K ");
[Link]("USN : 4YG24CS033");
[Link]("Output:");
Water water = new Water();
for (int i = 0; i < 6; i++)
new Hydrogen(water).start();
for (int i = 0; i < 3; i++)
new Oxygen(water).start();
Output:
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 12
OOPJ (BCS306A) Assignment-02 2025-2026
Program 05:
Define an enumeration for the 12 months of the year starting with JAN = 1. Given a month number,
print its name. If month number is not entered properly i.e other than 1-12 is entered print a suitable
message (invalid month)
Sample Input
Sample Output
August
Program:
import [Link];
enum Month
JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6),
JUL(7), AUG(8), SEP(9), OCT(10), NOV(11), DEC(12);
int value;
Month(int value)
[Link] = value;
static Month getMonth(int num)
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 13
OOPJ (BCS306A) Assignment-02 2025-2026
{
for (Month m : [Link]())
if ([Link] == num)
return m;
return null;
public class Main
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
[Link]("Name :Chinmayi K ");
[Link]("USN : 4YG24CS033");
[Link]("Enter month number (1-12): ");
int monthNum = [Link]();
Month month = [Link](monthNum);
if (month != null)
[Link]("Month name: " + month);
else
[Link]("Invalid month");
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 14
OOPJ (BCS306A) Assignment-02 2025-2026
}
[Link]();
} }
Output:
Dept. of CSE, NCEH Chinmayi K(4YG24CS033) 15