Java Programs Assignment
1. Write a program in Java, to implement the Stack data Structure.
public class Stack {
private int maxSize;
private int[] stackArray;
private int top;
public Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value;
} else {
System.out.println("Stack is full.");
}
}
public int pop() {
if (top >= 0) {
return stackArray[top--];
} else {
System.out.println("Stack is empty.");
return -1;
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
System.out.println("Popped: " + stack.pop());
}
}
Output:
Popped: 20
2. Write a program in Java to implement a simple Bank Account.
class BankAccount {
private String accountHolder;
private double balance;
public BankAccount(String name, double initialBalance) {
this.accountHolder = name;
this.balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds.");
}
}
public void display() {
System.out.println("Account Holder: " + accountHolder);
System.out.println("Balance: $" + balance);
}
public static void main(String[] args) {
BankAccount acc = new BankAccount("John", 500);
acc.deposit(200);
acc.withdraw(100);
acc.display();
}
}
Output:
Account Holder: John
Balance: $600.0
3. Write a program in Java showing the action from three threads using a suitable example
class MyThread extends Thread {
private String threadName;
public MyThread(String name) {
threadName = name;
}
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread("Thread 1");
MyThread t2 = new MyThread("Thread 2");
MyThread t3 = new MyThread("Thread 3");
t1.start();
t2.start();
t3.start();
}
}
Output: (Order may vary due to thread scheduling)
Thread 1 - Count: 1...
Thread 3 - Count: 5...
4. Write a program of threads in Java showing interleaving of actions from two threads: t1 &
t2 synchronizing on a shared object.
class PingPong {
private final Object lock = new Object();
private boolean pingTurn = true;
class Ping extends Thread {
private int interval, messages, cycles;
public Ping(int interval, int messages, int cycles) {
this.interval = interval;
this.messages = messages;
this.cycles = cycles;
}
public void run() {
for (int c = 0; c < cycles; c++) {
for (int m = 0; m < messages; m++) {
synchronized (lock) {
while (!pingTurn) {
try { lock.wait(); } catch (InterruptedException e) {}
}
System.out.print("Ping -> ");
pingTurn = false;
lock.notify();
}
try { Thread.sleep(interval); } catch (InterruptedException e) {}
}
}
}
}
class Pong extends Thread {
private int interval, messages, cycles;
public Pong(int interval, int messages, int cycles) {
this.interval = interval;
this.messages = messages;
this.cycles = cycles;
}
public void run() {
for (int c = 0; c < cycles; c++) {
for (int m = 0; m < messages; m++) {
synchronized (lock) {
while (pingTurn) {
try { lock.wait(); } catch (InterruptedException e) {}
}
System.out.println("<- Pong");
pingTurn = true;
lock.notify();
}
try { Thread.sleep(interval); } catch (InterruptedException e) {}
}
}
}
}
public static void main(String[] args) {
int t1Sleep = Integer.parseInt(args[0]);
int t2Sleep = Integer.parseInt(args[1]);
int messages = Integer.parseInt(args[2]);
int cycles = Integer.parseInt(args[3]);
PingPong pp = new PingPong();
pp.new Ping(t1Sleep, messages, cycles).start();
pp.new Pong(t2Sleep, messages, cycles).start();
}
}
Output (varies with input):
Ping -> <- Pong
Ping -> <- Pong ...
5. Write a program in Java which converts a text file into all capital letters.
import java.io.*;
public class ToUpperFile {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line.toUpperCase());
writer.newLine();
}
reader.close();
writer.close();
System.out.println("Conversion complete.");
}
}
Output:
Contents of input.txt are converted to uppercase in output.txt