0% found this document useful (0 votes)
9 views5 pages

P7 Java 39

Practical 7 Of Java

Uploaded by

vhoratanvir1610
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)
9 views5 pages

P7 Java 39

Practical 7 Of Java

Uploaded by

vhoratanvir1610
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/ 5

Programming With Java

202044502

Practical- 7 Exception Handing

7.1 ] Write a program for creating a Bank class, which is used to manage the bank 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, NotEnoughMoneyException.

→Program :
import java.util.Scanner;

class NotEnoughMoneyException extends Exception {


public NotEnoughMoneyException(String message) {
super(message);
}
}
class Bank {
private String customerName;
private double balance;
public Bank(String customerName, double balance) {
this.customerName = customerName;
this.balance = balance;
}
public void deposit(double amount) {
System.out.println("\n--- Deposit ---");
System.out.println("Old Balance: " + balance);
balance += amount;
System.out.println("Deposited: " + amount);
System.out.println("New Balance: " + balance);
}
public void withdraw(double amount) throws
NotEnoughMoneyException {
System.out.println("\n--- Withdraw ---");
System.out.println("Old Balance: " + balance);
if (amount > balance) {
throw new ArithmeticException("Insufficient Balance
to withdraw " + amount);
}
balance -= amount;
System.out.println("Withdrawn: " + amount);
System.out.println("New Balance: " + balance);

if (balance < 500) {


throw new NotEnoughMoneyException("Balance below 500
is not allowed. Current Balance: " + balance);
}
}

PARMAR PRAGNESH
12302110501039
Programming With Java
202044502

public double getBalance() {


return balance;
}
}
public class BankApplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
Bank customer = new Bank(name, 0);

int choice;
do {
System.out.println("\n==== Bank Menu ====");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter amount to deposit:
");
double depAmt = sc.nextDouble();
customer.deposit(depAmt);
break;
case 2:
System.out.print("Enter amount to withdraw:
");
double withAmt = sc.nextDouble();
try {
customer.withdraw(withAmt);
} catch (ArithmeticException e) {
System.out.println("Error: " +
e.getMessage());
} catch (NotEnoughMoneyException e) {
System.out.println("Custom Error: " +
e.getMessage());
}
break;
case 3:
System.out.println("Current Balance: " +
customer.getBalance());
break;
case 4:
System.out.println("Exiting... Thank you, " +
name + "!");
break;

PARMAR PRAGNESH
12302110501039
Programming With Java
202044502

default:
System.out.println("Invalid choice! Please
select 1, 2, 3, or 4.");
}
} while (choice != 4);
sc.close();
}
}
→OUTPUT:

PARMAR PRAGNESH
12302110501039
Programming With Java
202044502

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. If n is zero.
→Program :
import java.util.Scanner;

class InvalidInputException extends Exception {


public InvalidInputException(String msg) {
super(msg);
}
}
public class AverageArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

try {
System.out.print("Enter number of elements (n): ");
int n = sc.nextInt();

if (n == 0) {
throw new InvalidInputException("n cannot be
zero");
}
int[] A = new int[n];
int sum = 0;

System.out.println("Enter " + n + " positive


integers:");

for (int i = 0; i < n; i++) {


try {
A[i] = sc.nextInt();

if (A[i] < 0) {
throw new InvalidInputException("Negative
numbers not allowed");
}

sum += A[i];
} catch (InvalidInputException e) {
System.out.println(e.getMessage());
i--;
} catch (Exception e) {
System.out.println("Please enter only
integers");

PARMAR PRAGNESH
12302110501039
Programming With Java
202044502

sc.next();
i--;
}
}

double avg = (double) sum / n;


System.out.println("Average = " + avg);

} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
sc.close();
}
}
→Output :

PARMAR PRAGNESH
12302110501039

You might also like