Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 7 Java

 PRACTICAL 7  EXCEPTION HANDLING

7-(1)

Write a program for creating a Bank class, which is used to manage the bank account of customers. Class has two methods, Deposit () and withdraw (). Deposit method display old balance and new balance after depositing the specified amount. Withdrew method display old balance and new balance after withdrawing. If balance is not enough to withdraw the money, it throws ArithmeticException and if balance is less than 500rs after withdrawing then it throw custom exception


package com.company; import java.util.Scanner;
public class Bank extends Throwable { Scanner sc=new Scanner(System.in); static int a=0;
int ammount; String name; String acc_type; double accountno; Bank()
{
accountno=++a;
}

void set_accountno ()
{
ammount=sc.nextInt();
}
int widraw(int v)
{
ammount=ammount-v; return ammount;
}

}
public class NotEnoughmoneyException extends Throwable { NotEnoughmoneyException()
{
 

System.out.println("exception not anoigh money");
}

}
public class Main {

public static void main(String[] args) {
// write your code here Bank b=new Bank(); b.set_accountno(); b.widraw(500);
try{

if(b.ammount<0)
{
ArithmeticException e=new ArithmeticException(); throw (e);
}
}
catch ( ArithmeticException e)
{
System.out.println(e);
System.out.println("you can not widrow this amount");
}
try{

if(b.ammount<500)
{
NotEnoughmoneyException n1=new NotEnoughmoneyException(); throw (n1);
}
}
catch ( NotEnoughmoneyException c)
{
System.out.println(c); System.out.println("balance <500");
}

}
}

7-(2)

Write a complete program for calculation average of n +ve integer numbers of Array A. a. Read the array form keyboard b. Raise and handle Exception if

i. Element value is -ve or non-integer. ii. ii. If n is zero.

package com.company; import java.util.Scanner; public class ARR {
int[] arra; int size;
Scanner sc=new Scanner(System.in); ARR (int n)
{
size=n; arra=new int[n];
}
int ifsizezero()
{
if(size==0)
{
return 1;
}
return 0;
}

int checkifnegative()
{
int flag=0;
for(int i=0;i<size;i++)
{
if(arra[i] < 0)
{
flag=1; return flag;
}
}
return flag;
}
void entervalue()
{
for(int i=0;i<size;i++)
{
arra[i]=sc.nextInt();
}
}

}
 

public class negativeele extends Throwable { negativeele()
{
System.out.println("-ve number entered");
}

}
public class sizeo extends Throwable { sizeo()
{
System.out.println("size of array entered is 0");
}

}
public class Main {

public static void main(String[] args) { ARR a1=new ARR(0);
ARR a2=new ARR(4);
try
{
if(a1.ifsizezero()==1)
{
sizeo s1=new sizeo(); throw (s1);
}
}
catch (sizeo s)
{
System.out.println(s);
}

a2.entervalue(); try
{
if(a2.checkifnegative()==1)
{
negativeele s1=new negativeele(); throw (s1);
}
}
catch (negativeele s)
{
System.out.println(s);
 

}
}
}


Post a Comment

0 Comments