Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Model 1 Java

The document contains 4 code examples demonstrating different concepts in Java: 1) An ATM class with methods to withdraw money while handling exceptions for insufficient balances. 2) An interface and class to book and display movie tickets, demonstrating polymorphism. 3) A producer-consumer problem solved using wait/notify to synchronize putting and getting values in a shared queue. 4) A synchronized method example ensuring threads call a target method sequentially by waiting on the target object.

Uploaded by

Rahul Venkatesan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Model 1 Java

The document contains 4 code examples demonstrating different concepts in Java: 1) An ATM class with methods to withdraw money while handling exceptions for insufficient balances. 2) An interface and class to book and display movie tickets, demonstrating polymorphism. 3) A producer-consumer problem solved using wait/notify to synchronize putting and getting values in a shared queue. 4) A synchronized method example ensuring threads call a target method sequentially by waiting on the target object.

Uploaded by

Rahul Venkatesan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1)

import java.util.Scanner;
class InsufficientBalanceException extends Exception
{
public InsufficientBalanceException(String message)
{
super(message);
}
}
class Atm
{
private double balance;
private static final double Min=500;
private String holdername;
private String branchname;
public Atm(double initialbalance,String
holdername,String branchname)
{
this.balance=initialbalance;
this.holdername=holdername;
this.branchname=branchname;
}
public void withdraw(double amount) throws
InsufficientBalanceException
{
if(amount>balance)
{ throw new
InsufficientBalanceException("Insuficient balance");}
if(balance-amount<Min)
{
throw new
InsufficientBalanceException("Withdrawal will result ini
balance below minimum required");
}
balance-=amount;
System.out.println("Withdrawal Successful!
Remaining balance: "+balance);
}
}
public class AtmFunc
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter Holder name: ");
String holdername=scanner.nextLine();
System.out.println("Enter Initial Balance: ");
double initialbalance=scanner.nextDouble();
System.out.println("Enter Branch name: ");
String branchname=scanner.nextLine();
Atm atm=new
Atm(initialbalance,holdername,branchname);
try
{
System.out.println("Enter Withdrawal Amount :");
double withdrawalAmount=scanner.nextDouble();
atm.withdraw(withdrawalAmount);
}
catch(InsufficientBalanceException e)
{
System.out.println("Error: "+e.getMessage());
}
finally
{
scanner.close();
}
}

2)
import java.util.Scanner;
interface Ticket
{
void book();
void display();
}
class Movie implements Ticket
{
private String name;
private int price;
private int numTickets;
public Movie(String name,int price)
{
this.name=name;
this.price=price;
this.numTickets=0;
}
public void book()
{
Scanner Scanner=new Scanner(System.in);
System.out.println("Enter the number of Tickets to
book: ");
int Tickets=Scanner.nextInt();
if(Tickets>0)
{
numTickets+=Tickets;
System.out.println(Tickets+" Tickets for"+ name+"
Movie have been booked");
}
else
{
System.out.println("Invalid Number of Tickets");
}
}
public void display()
{
System.out.println("Movie: "+name);
System.out.println("Price: "+price);
System.out.println("Total Amount:
"+(price*numTickets));
}
}
public class booking
{
public static void main(String[] args)
{
Movie movieticket=new Movie("Leo",150);
movieticket.book();
movieticket.display();
}
}

3)
class Q
{
int num;
boolean valueset=false;
public synchronized void put(int num)
{
while(valueset)
{
try{wait();} catch(Exception e){};
}
System.out.println("Put: "+num);
this.num=num;
valueset=true;
notify();

}
public synchronized void get()
{
while(!valueset)
{
try{wait();} catch(Exception e){};
}
System.out.println("Get: "+num);
valueset=false;
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
Thread t=new Thread(this,"Producer");
t.start();

}
public void run()
{
int i=0;
while(true)
{
q.put(i++);

try{Thread.sleep(1000);} catch(Exception
e){};

}
}
}

class Consumer implements Runnable


{
Q q;
Consumer(Q q)
{
this.q=q;
Thread t=new Thread(this,"Consumer");
t.start();
}
public void run()
{
while(true)
{
q.get();
try{Thread.sleep(2000);} catch(Exception
e){};
}
}
}

public class InterThread


{
public static void main(String[] args)
{
Q q=new Q();
new Producer(q);
new Consumer(q);
}
}

4)
class Callme
{
void call(String msg)
{
System.out.print("["+msg);
try{Thread.sleep(1000);}
catch(InterruptedException
e){System.out.println("Interepted");}
System.out.println("]");

}
}
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme target,String msg)
{
this.target=target;
this.msg=msg;
t=new Thread(this);
t.start();
}
public void run()
{
synchronized(target)
{
target.call(msg);
}
}

}
public class Synch
{
public static void main(String args[])
{
Callme target=new Callme();
Caller obj1=new Caller(target,"Hello");
Caller obj2=new Caller(target,"Syn");
Caller obj3=new Caller(target,"Rahul");
try{
obj1.t.join();

obj2.t.join();
obj3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}

}
}

You might also like