Uploaded by NO NO

Assignment 6 final

advertisement
CSE 1007 JAVA PROGRAMMING
ASSESSMENT 6
L 37+38
19BCE2612
ISHAN KHADKA
Pseudocode:
Declare class Atm
Method synchronized void checkBalance(String str)
System.out.print(str," is checking his ")
try Thread.sleep(10) catch(Exception e)
Display(e)
Display("Balance ")
synchronized void withdraw(String str, int amt)
System.out.print(str, " is withdrawing amount: ")
try Thread.sleep(10) catch(Exception e)
Display(e)
Display(amt)
Declare class Customer extends Thread
String name
int amt
Atm a
Customer (Atm a)
this.a=a
public void useAtm()
synchronized (a)
a.checkBalance(name)
a.withdraw(name,amt)
public void run()
useAtm()
Declare public class Main
public static void main(String[] args)
Atm a=new Atm()
Customer c1=new Customer(a)
Customer c2=new Customer(a)
Customer c3=new Customer(a)
c1.name="Ram"
c1.amt=5000
c2.name="Hari"
c2.amt=51000
c3.name="Shyam"
c3.amt=545000
Customer c4=new Customer(a)
Customer c5=new Customer(a)
Customer c6=new Customer(a)
c4.name="xam"
c4.amt=500220
c5.name="pari"
c5.amt=5100220
c6.name="ohyam"
c6.amt=54501200
c1.start()
try c1.join() catch(Exception e)
c2.start()
c3.start()
c4.start()
c5.start()
c6.start()
Code:
class Atm{
synchronized void checkBalance(String str){
System.out.print(str+" is checking his ");
try{Thread.sleep(10);}catch(Exception e){
System.out.println(e);
}
System.out.println("Balance ");
}
synchronized void withdraw(String str, int amt){
System.out.print(str+ " is withdrawing amount: ");
try{Thread.sleep(10);}catch(Exception e){
System.out.println(e);
}
System.out.println(amt);
}
}
class Customer extends Thread{
String name;
int amt;
Atm a;
Customer (Atm a){
this.a=a;
}
public void useAtm(){
synchronized (a){
a.checkBalance(name);
a.withdraw(name,amt);
}
}
public void run(){
useAtm();
}
}
public class Main {
public static void main(String[] args) {
Atm a=new Atm();
Customer c1=new Customer(a);
Customer c2=new Customer(a);
Customer c3=new Customer(a);
c1.name="Ram";
c1.amt=5000;
c2.name="Hari";
c2.amt=51000;
c3.name="Shyam";
c3.amt=545000;
Customer c4=new Customer(a);
Customer c5=new Customer(a);
Customer c6=new Customer(a);
c4.name="xam";
c4.amt=500220;
c5.name="pari";
c5.amt=5100220;
c6.name="ohyam";
c6.amt=54501200;
c1.start();
try{c1.join();}catch(Exception e){}
c2.start();
c3.start();
c4.start();
c5.start();
c6.start();
}
}
Output:
Pseudocode:
Create class Numerator extends Thread
initialize int x[] = new int[10]
initialize int f[] = new int[10]
initialize int numerator
Numerator(int x[],int f[])
this.x=x
this.f=f
this.numerator=0
Numerator()
this.numerator=0
public void run()
for(int i=0 i<10 i++)
numerator=numerator+f[i]*x[i]
Create class Denominator extends Thread
int f[] = new int[10]
int denominator
int x
Denominator(int f[])
this.f=f
this.denominator=0
Denominator()
this.denominator=0
public void run()
for(int i=0 i<10 i++)
denominator=denominator+f[i]
this.x=denominator
public class Main
declare static int mean(int a,int b)
return a/b
public static void main(String[] args)
int x[]= 1,2,3,4,5,6,7,8,9,10
int f[]= 3,4,17,8,23,10,4,6,5,2
Numerator nu=new Numerator(x,f)
Denominator de=new Denominator(f)
nu.start()
de.start()
try nu.join() catch (Exception e)
try de.join() catch (Exception e)
Thread t=Thread.currentThread()
int m
m=mean(nu.numerator,de.denominator)
Display("Mean: ", m)
Code:
class Numerator extends Thread{
int x[] = new int[10];
int f[] = new int[10];
int numerator;
Numerator(int x[],int f[]){
this.x=x;
this.f=f;
this.numerator=0;
}
Numerator(){
this.numerator=0;
}
public void run(){
for(int i=0;i<10;i++){
numerator=numerator+f[i]*x[i];
}
}
}
class Denominator extends Thread{
int f[] = new int[10];
int denominator;
int x;
Denominator(int f[]){
this.f=f;
this.denominator=0;
}
Denominator(){
this.denominator=0;
}
public void run(){
for(int i=0;i<10;i++){
denominator=denominator+f[i];
}
this.x=denominator;
}
}
public class Main {
static int mean(int a,int b){
return a/b;
}
public static void main(String[] args) {
int x[]={1,2,3,4,5,6,7,8,9,10};
int f[]={3,4,17,8,23,10,4,6,5,2};
Numerator nu=new Numerator(x,f);
Denominator de=new Denominator(f);
nu.start();
de.start();
try{nu.join();}catch (Exception e){}
try{de.join();}catch (Exception e){}
Thread t=Thread.currentThread();
int m;
m=mean(nu.numerator,de.denominator);
System.out.println("Mean: "+ m);
}
}
Output:
Pseudocode:
import java.io.File
class Main
public static void main(String[] args)
File file = new File("19BCE2612 Ishan Khadka.txt")
try
boolean value = file.createNewFile()
if (value)
Display("New file Created Successfully!!!")
else
Display("File Already Exists!!!")
catch(Exception e)
e.getStackTrace()
Code:
import java.io.File;
class Main {
public static void main(String[] args) {
File file = new File("19BCE2612 Ishan Khadka.txt");
try {
boolean value = file.createNewFile();
if (value) {
System.out.println("New file Created Successfully!!!");
}
else {
System.out.println("File Already Exists!!!");
}
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output:
Before executing:
After Executing:
Rerunning the code:
Download