Lab Programs
Lab Programs
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rate Of Interest(){return 9.15f;}
}
class PNB implements Bank{
public float rate Of Interest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
Bank b1 =new PNB ();
System.out.println("ROI: "+b1.rateOfInterest());
}
}
7. Write a program to make a package balance which has account class with display_balance
method in it. Import balance package in another program to access display_balance method of
account class.
Account.java
package Balance;
import java.util.Scanner;
public Account() {
curBalance = 500;
void deposit() {
amt = s.nextInt();
curBalance += amt;
void withdraw() {
amt = s.nextInt();
try {
curBalance -= amt;
} catch (LessBalanceException e) {
System.out.println(e);
}
void display_balance() {
int amt;
LessBalanceException(int x) {
MainProgram.java
package Balance;
import java.util.Scanner;
int ch;
while (true) {
System.out.println("1:Deposit\t2:Withdraw\t3:Balance\t4:Exit\n");
ch = s.nextInt();
switch (ch) {
case 1:
a.deposit();
break;
case 2:
a.withdraw();
break;
case 3:
a.display_balance();
break;
case 4:
return;
default:
System.out.println("Invalid choice\n");
return;
7. Write a JAVA program to create five threads with different priorities. Send two threads of the
highest priority to sleep state. Check the aliveness of the threads and mark which is long lasting.
class ThreadClass implements Runnable
{
long click=0;
Thread t;
private volatile boolean running =true;
public ThreadClass(int p)
{
t=new Thread(this);
t.setPriority(p);
}
public void run()
{
while(running)
{
click++;
}
}
public void stop()
{
running =false;
}
public void start()
{
t.start();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
hi1.start();
hi2.start();
hi3.start();
hi4.start();
hi5.start();
{ hi5.t.sleep(1000);
hi3.t.sleep(1000);
catch(InterruptedException e){
hi1.stop();
hi2.stop();
hi3.stop();
hi4.stop();
hi5.stop();
try
hi1.t.join();
hi2.t.join();
hi3.t.join();
hi4.t.join();
hi5.t.join();
catch(InterruptedException e)
8.