Upload files to "OOP/Java/Lab"

This commit is contained in:
Aadit Agrawal 2024-10-14 09:28:53 +05:30
parent 0b8cfc509f
commit 1b5dc8d861
5 changed files with 427 additions and 0 deletions

View File

@ -0,0 +1,126 @@
// Online Java Compiler
/*interface product
{
void buy(int num);
}
class genproduct implements product
{
int quan;
genproduct(int n)
{
quan=n;
}
public synchronized void buy(int num)
{
if(quan<num)
{
System.out.println("Erretysuor");
}
else{
System.out.println(num+" in start "+quan);
quan=quan-num;
System.out.println("succesfull "+quan);
}
}
}
class inventory<T extends product> extends Thread
{
T obj;
int num;
inventory(T obj,int num)
{
this.obj=obj;
this.num=num;
}
public void run()
{
obj.buy(num);
}
}
class Mainclass
{
public static void main(String[] args) {
genproduct buyer1 =new genproduct(15);
inventory<genproduct> t1=new inventory(buyer1,2);
inventory<genproduct> t2=new inventory(buyer1,5);
// obj.start();
t1.start();
t2.start();
}
}*/
class BankAccount {
private int balance;
private int accountNumber;
public BankAccount(int accountNumber, int initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public synchronized void deposit(int amount) {
balance += amount;
System.out.println("Deposited: " + amount);
System.out.println("Current Balance after Deposit: " + balance);
}
public synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
System.out.println("Current Balance after Withdrawal: " + balance);
} else {
System.out.println("Insufficient funds for withdrawal.");
}
}
}
class TransactionThread extends Thread {
private BankAccount account;
private boolean isDeposit;
private int amount;
public TransactionThread(BankAccount account, boolean isDeposit, int amount) {
this.account = account;
this.isDeposit = isDeposit;
this.amount = amount;
}
public void run() {
if (isDeposit) {
account.deposit(amount);
} else {
account.withdraw(amount);
}
}
}
class Practice_question{
public static void main(String[] args) {
BankAccount account = new BankAccount(12345, 1000);
// Create multiple threads for deposits and withdrawals
TransactionThread depositThread1 = new TransactionThread(account, true, 500);
TransactionThread withdrawThread1 = new TransactionThread(account, false, 200);
TransactionThread depositThread2 = new TransactionThread(account, true, 100);
TransactionThread withdrawThread2 = new TransactionThread(account, false, 300);
// Start the threads
depositThread1.start();
withdrawThread1.start();
depositThread2.start();
withdrawThread2.start();
// Wait for all threads to finish
try {
depositThread1.join();
withdrawThread1.join();
depositThread2.join();
withdrawThread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,161 @@
import java.util.Scanner;
class Matrix {
static int[][] matrix;
Matrix(int rows, int columns) {
matrix = new int[rows][columns];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the values");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = sc.nextInt();
}
}
}
}
class DisplayMatrixThread extends Thread {
static int[][] matrix;
DisplayMatrixThread(Matrix matrixObj) {
matrix = Matrix.matrix;
}
public void run() {
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + "\t");
}
System.out.println();
}
}
}
class TransposeMatrixThread extends Thread {
int[][] matrix;
TransposeMatrixThread(Matrix matrixObj) {
matrix = matrixObj.matrix;
}
public void run() {
int rows = matrix.length;
int columns = matrix[0].length;
for (int j = 0; j < columns; j++) {
for (int i = 0; i < rows; i++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
}
class MaxValueThread extends Thread {
static int[][] matrix;
MaxValueThread(Matrix matrixObj) {
matrix = Matrix.matrix;
}
public void run() {
int max = matrix[0][0];
for (int[] row : matrix) {
for (int value : row) {
if (value > max) {
max = value;
}
}
}
System.out.println("Maximum value in the matrix: " + max);
}
}
class PrincipalDiagonalThread extends Thread {
static int[][] matrix;
PrincipalDiagonalThread(Matrix matrixObj) {
matrix = Matrix.matrix;
}
public void run() {
int rows = matrix.length;
int columns = matrix[0].length;
System.out.print("Principal Diagonal Elements: ");
for (int i = 0; i < Math.min(rows, columns); i++) {
System.out.print(matrix[i][i] + " ");
}
System.out.println();
}
}
class NonDiagonalThread extends Thread {
static int[][] matrix;
NonDiagonalThread(Matrix matrixObj) {
matrix = Matrix.matrix;
}
public void run() {
int rows = matrix.length;
int columns = matrix[0].length;
System.out.print("Non-Diagonal Elements: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (i != j) {
System.out.print(matrix[i][j] + " ");
}
}
}
System.out.println();
}
}
class ThreadMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no. of rows and columns");
int rows = sc.nextInt();
int columns = sc.nextInt();
Matrix matrixObj = new Matrix(rows, columns);
DisplayMatrixThread t1 = new DisplayMatrixThread(matrixObj);
TransposeMatrixThread t2 = new TransposeMatrixThread(matrixObj);
MaxValueThread t3 = new MaxValueThread(matrixObj);
PrincipalDiagonalThread t4 = new PrincipalDiagonalThread(matrixObj);
NonDiagonalThread t5 = new NonDiagonalThread(matrixObj);
try {
t1.start();
System.out.println("\nThread t1 is alive: " + t1.isAlive());
t1.join();
t2.start();
System.out.println("\nThread t2 is alive: " + t2.isAlive());
t2.join();
t3.start();
System.out.println("\nThread t3 is alive: " + t3.isAlive());
t3.join();
t4.start();
System.out.println("\nThread t4 is alive: " + t4.isAlive());
t4.join();
t5.start();
System.out.println("\nThread t5 is alive: " + t5.isAlive());
t5.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\nThread t1 is alive: " + t1.isAlive());
System.out.println("Thread t2 is alive: " + t2.isAlive());
System.out.println("Thread t3 is alive: " + t3.isAlive());
System.out.println("Thread t4 is alive: " + t4.isAlive());
System.out.println("Thread t5 is alive: " + t5.isAlive());
System.out.println("\nMain thread exits.");
}
}

View File

@ -0,0 +1,43 @@
class Incrementer{
int count;
Incrementer(int cnt)
{
count=cnt;
}
synchronized void increment()
{
count++;
}
}
class IncrementThread extends Thread{
Incrementer ref;
IncrementThread(Incrementer obj)
{
ref=obj;
}
public void run()
{
for(int i=0;i<10000;i++)
{
ref.increment();
}
}
}
class syncMethod{
public static void main(String[] args) {
Incrementer inc=new Incrementer(0);
IncrementThread t1=new IncrementThread(inc);
IncrementThread t2=new IncrementThread(inc);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println(e.toString());
}
System.out.println(inc.count);
}
}

View File

@ -0,0 +1,44 @@
class IncrementThread extends Thread{
Incrementer ref;
IncrementThread(Incrementer obj)
{
ref=obj;
}
public void run()
{
for(int i=0;i<10000;i++)
{
ref.increment();
}
}
}
class Incrementer{
int count;
Incrementer(int cnt)
{
count=cnt;
}
void increment()
{
synchronized(this)
{
count++;
}
}
}
class syncMethod{
public static void main(String[] args) {
Incrementer inc=new Incrementer(0);
IncrementThread t1=new IncrementThread(inc);
IncrementThread t2=new IncrementThread(inc);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println(e.toString());
}
System.out.println(inc.count);
}
}

View File

@ -0,0 +1,53 @@
import java.util.Scanner;
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread created by runnable ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MyThread extends Thread {
public void run() {
System.out.println("Thread created by Thread class");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Threadmaking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("\nChoose an option:");
System.out.println("1. Create Thread using Runnable Interface");
System.out.println("2. Create Thread by Inheriting Thread Class");
System.out.println("3. Exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
Thread runnableThread = new Thread(new MyRunnable());//creating a runnable object which is directly passed to Thread
runnableThread.start();
break;
case 2:
MyThread myThread = new MyThread();
myThread.start();
break;
case 3:
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
}
}