Week 8
Week 8
WEEK-8
1. Write a program to access the methods of one package methods in
another packages: Create a bank class and implement the methods of
deposit() and withdraw(). Access these in another package.
Write a program to access the methods of one package methods in
another packages .
Program:
Package 1: bank
package bank;
public class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance.");
}
}
public double getBalance() {
return balance;
}
Date SheetNo:
}
Package 2: main
package main;
import bank.BankAccount;
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.withdraw(1500.0);
System.out.println("Final balance: " + account.getBalance());
}
}
Output:
Deposited: 500.0
Withdrawn: 200.0
Insufficient balance.
Final balance: 1300.0
Date SheetNo:
2. Create multiple threads Hello and Welcome which prints "Hello Java"
and " Hello Dotnet " using Runnable interface for 10 and 20 times.
Program:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2)
Runnable helloTask = () -> {
for (int i = 0; i < 10; i++) {
System.out.println("Hello Java");
}
};
Runnable welcomeTask = () -> {
for (int i = 0; i < 20; i++) {
System.out.println("Hello Dotnet");
}
};
executorService.submit(helloTask);
executorService.submit(welcomeTask);
executorService.shutdown();
}
}
Output :
Hello Java
Hello Dotnet
Date SheetNo:
3. Create two classes IT and CSE which extends Thread class each.
Inside ea of the class, print Hello IT and Hello CSE for 5 and 10 times.
Program:
class IT extends Thread{
public void run(){
for(int i=0;i<5;i++){
System.out.println("HELLO IT");
}
}
}
class CSE extends Thread{
public void run(){
for(int i=0;i<10;i++){
System.out.println("HELLO CSE");
}
}
}
public class HelloWorld {
public static void main(String[] args) {
IT it=new IT();
CSE cse= new CSE();
it.start();
cse.start();
}
}
Output :
Date SheetNo:
HELLO IT
HELLO CSE
HELLO IT
HELLO CSE
HELLO IT
HELLO CSE
HELLO IT
HELLO CSE
HELLO IT
HELLO CSE
HELLO IT
HELLO CSE
HELLO CSE
HELLO CSE
HELLO CSE
HELLO CSE
Date SheetNo:
4. Create multiple threads that display welcome to it and welcome to
seca for every 5 and 10 seconds. Also write a for loop to print welcome
to vrsec forevery 15 secs
Program:
class Mess1 extends Thread{
public void run(){
try{
for(int i=0;i<6;i++){
System.out.println("welcome it ");
Thread.sleep(5000); }
}
catch(InterruptedException e){
Thread.currentThread().interrupt(); }
} }
class Mess2 extends Thread{
public void run(){
try{
for(int i=0;i<6;i++){
System.out.println("welcome seca");
Thread.sleep(10000); } }
catch(InterruptedException e){
Thread.currentThread().interrupt(); }
}
}
class Mess3 extends Thread{
public void run(){
try{
for(int i=0;i<6;i++){
System.out.println("welcome VRSEC");
Date SheetNo:
Thread.sleep(15000);
} }
catch(InterruptedException e){
Thread.currentThread().interrupt(); }
}
}
public class HelloWorld {
public static void main(String[] args) {
Mess1 obj1=new Mess1();
Mess2 obj2=new Mess2();
Mess3 obj3=new Mess3();
obj1.start();
obj2.start();
obj3.start();
}
}
Output :
welcome it
Date SheetNo:
welcome seca
welcome it
welcome VRSEC
welcome it
welcome seca
welcome it
welcome VRSEC
welcome it
welcome seca
welcome it
welcome VRSEC
Output :
thread is starting
java programming lab
Date SheetNo:
java programming lab
java programming lab
java programming lab
java programming lab
java programming lab
java programming lab
java programming lab
java programming lab
java programming lab
thread is finished