16bce0077 Java Lab 3
16bce0077 Java Lab 3
Harshit Aggarwal
16BCE0077
m
er as
class NegativeAmountException extends Exception{
NegativeAmountException(String s){
co
super(s);
eH w
}
}
o.
class BankAccount{
rs e
ou urc
private float balance;
o
BankAccount(){
this.balance = 0;
aC s
}
v i y re
}
ar stu
else
this.balance = bal;
}
sh is
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
}catch(Exception m){System.out.println("Exception occured: "+m);}
}
}
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
2. Three candidates OPS, EPS and Stalin of Tamilnadu to contest for the Chief
Minister election with the total strength of 240 MLAs. Write a Java program to
simulate the vote casting by generating 240 random numbers (1 for OPS, 2 for
EPS and 3 for Stalin) and store them in an array. Create four threads to equally
share the task of counting the number of votes cast for all the three candidates.
Use synchronized method or synchronized block to update the three count
variables. The main thread should receive the final vote count for all three
contestants and decide the Chief Minister based on the values received.
import java.util.Random;
class voting extends Thread
{
static int tot = 240;
static int ops;
static int eps;
m
er as
static int stalin;
static int arr[];
co
voting(){
eH w
ops = 0;
eps = 0;
o.
stalin = 0;
arr = new int[240];
rs e
ou urc
}
public void run(){
if(Thread.currentThread().getName()=="arr"){
o
synchronized(this){
for(int i=0; i<tot; i++)
aC s
}
}
else if(Thread.currentThread().getName()=="ops"){
synchronized(this){
ed d
ops += 1;
}
}
else if(Thread.currentThread().getName()=="eps"){
sh is
synchronized(this){
for(int i=0; i<tot; i++)
Th
if(arr[i]==2)
eps += 1;
}
}
else if(Thread.currentThread().getName()=="stalin"){
synchronized(this){
for(int i=0; i<tot; i++)
if(arr[i]==3)
stalin += 1;
}
}
}
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
Random rand = new Random();
int random = rand.nextInt((max - min) + 1) + min;
return random;
}
m
b.start();
er as
c.start();
co
try{
eH w
Thread.sleep(200);
}
o.
catch(Exception e){
rs e
System.out.println(e);
ou urc
}
System.out.println(obj.ops);
System.out.println(obj.eps);
System.out.println(obj.stalin);
o
}
aC s
}
v i y re
ed d
ar stu
sh is
Th
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
3. Design a class for share trading system. Create necessary supporting
variables for a share trading system also which has share_value. The
share_value is the variable used by many threads who has rights to update it
from time to time. Imagine thread owner is modifying the share_value, other
threads like, buyer1, buyer2, buyer3 has to wait till the owner notifies them.
Also when share broker thread is modifying the share_value all others including
owner has to wait till share broker notifies them. Use the multithreading
concepts, wait, notify, notifyall, sleep, synchronized.
m
Thread.currentThread().getName().equals("broker"))
er as
{
co
eH w
if(change<0 && share_value<Math.abs(change))
{
o.
rs e System.out.println("less share than change");
try{
ou urc
wait();
}
catch(Exception e){
System.out.println(e);
o
}
aC s
notify();
}
v i y re
else
synchronized(this){
{
share_value+=change;
ed d
notifyAll();
}
}
sh is
}
else
Th
{
synchronized(this){
if(change<0 && share_value<Math.abs(change))
{
System.out.println("less share than change");
try{
wait();
}
catch(Exception e){
System.out.println(e);
}
notify();
}
else
{
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
share_value+=change;
System.out.println("share value is:"+share_value);
notify();
}
}
}
}
m
er as
t1.setName("broker");
Thread t3=new Thread();
co
t1.setName("b1");
eH w
Thread t4=new Thread();
t1.setName("b2");
o.
rs e
Thread t5=new Thread();
t1.setName("b3");
ou urc
new Thread(){
public void run(){
o
m.modify(10);
}
aC s
}.start();
v i y re
new Thread(){
public void run(){
m.modify(-21);
ed d
}
ar stu
}.start();
new Thread(){
public void run(){
sh is
m.modify(21);
}
Th
}.start();
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
4. Design a class for cash counter processing system. Assume several variables
in class like mode of payment, amount, account number, remitter name, remitter
mobile number…..etc. Several objects are created for the account class by
cashier. When cashier opts to signout, write the state of all objects to an existing
file. When cashier performs login to the machine, read the state of all objects
from the file and display it on monitor.
import java.io.Serializable;
import java.util.Date;
public class CashCounter implements Serializable {
m
er as
public CashCounter(float payment, float amount, String account_num, String
co
remmiter_name, String remmiter_num) {
eH w
super();
this.payment = payment;
o.
this.amount = amount;rs e
this.account_num account_num= account_num;
ou urc
this.remmiter_name = remmiter_name;
this.remmiter_num = remmiter_num;
}
o
@Override
aC s
@Cashier Class
import java.io.FileOutputStream;
import java.io.FileInputStream;
sh is
import java.io.ObjectOutputStream;
import java.util.ArrayList;
Th
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.io.ObjectInputStream;\
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
// Writing the object in File
oos.writeObject(woi);
oos.flush();
} catch (Exception e) {
}
// Reading the file and fetching object and displaying the required
try {
FileInputStream fis=new
FileInputStream("C:\\Users\\Harshit\\Desktop\\List.txt");
m
ObjectInputStream ois=new ObjectInputStream(fis);
er as
// Donor wo=null;
co
// Donor[] woj=new Donor[5];
eH w
ArrayList<CashCounter> woi1=new ArrayList<>();
woi1=(ArrayList<CashCounter>)ois.readObject();
o.
for(int i=0;i<woi1.size();i++){
rs e
ou urc
}catch (Exception e) {
}
o
aC s
v i y re
ed d
ar stu
sh is
Th
5. Write a Java program to read book id, author and publisher details and add
books to list and printing all the books using ArrayList and Iterator.
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
import java.util.ArrayList;
class Book{
public int id;
public String author;
public String pub_details;
Book(int a,String b,String c){
id=a;
author=b;
pub_details=c;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Book> list=new ArrayList<Book>();
Book b1=new Book(15,"harshit","abc_publications");
Book b2=new Book(11,"vijay","xyz_publications");
Book b3=new Book(12,"aakash","qwz_publications");
m
er as
list.add(b1);
list.add(b2);
co
list.add(b3);
eH w
int i;
Book b;
o.
rs e
for(i=0;i<list.size();i++){
b=list.get(i);
ou urc
System.out.println("author is:"+b.author+" ,id
is:"+b.id+",publication is:"+b.pub_details);
}
o
}
}
aC s
v i y re
ed d
ar stu
sh is
Th
This study source was downloaded by 100000790675436 from CourseHero.com on 05-20-2021 00:04:41 GMT -05:00
https://www.coursehero.com/file/39618607/16BCE0077-JAVA-LAB-3docx/
Powered by TCPDF (www.tcpdf.org)