Question 1 :
public class IOException extends RuntimeException {
public IOException(String message) {
super(message);
}
}
import java.io.IOException;
public class Question1 {
public static void methodA(){
try{
methodB();
} catch(IOException a){
System.out.println("Nishant");
}
}
public static void methodB() throws IOException{
methodC();
}
public static void methodC() throws IOException {
throw new IOException("Exception Handlling.");
}
public static void main(String[] args) {
methodA();
}
}
Question 2:
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double x = sc.nextDouble();
try{
if(x <= 0) throw new IllegalArgumentException();
System.out.println(Math.sqrt(x));
} catch(IllegalArgumentException a){
System.out.println("Got an Exception");
}
}
}
Question 3 :
import java.util.Objects;
import java.util.Scanner;
public class user {
String name;
int password;
public user(String name, int password) {
this.name = name;
this.password = password;
}
public static void authenticate(user one, String temp_name, int temp_x){
try{
if(!Objects.equals(temp_name, one.name) || temp_x != one.password)
throw new IllegalArgumentException();
else System.out.println("ACCESS");
} catch (IllegalArgumentException a){
System.out.println("ACCESS DENIED");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter predefined name : ");
String temp_name = sc.next();
System.out.print("Enter predefined password : ");
int temp_x = sc.nextInt();
user one = new user(temp_name, temp_x);
System.out.print("Enter your name : ");
temp_name = sc.next();
System.out.print("Enter your password : ");
temp_x = sc.nextInt();
authenticate(one, temp_name, temp_x);
}
}
Question 4 :
import java.sql.SQLOutput;
public class Account {
String name;
String bank;
int balance;
public Account(String name, String bank, int balance) {
this.name = name;
this.bank = bank;
this.balance = balance;
}
public void withdraw(int sub){
try {
if(sub < 0) throw new IllegalArgumentException();
else if (balance - sub < 0) throw new ArithmeticException();
else balance = balance - sub;
} catch (ArithmeticException a){
System.out.println("Insufficient balance");
} catch (IllegalArgumentException b){
System.out.println("Invalid number");
}
}
public void deposit(int add){
try{
if(add < 0) throw new IllegalArgumentException();
else balance = balance + add;
} catch(IllegalArgumentException a){
System.out.println("Invalid number");
}
}
public void display(){
System.out.println("Name : " + name);
System.out.println("Bank : " + bank);
System.out.println("Balance : " + balance);
}
public void display_balance(){
System.out.println("Balance : " + balance);
}
}
class ATM_System{
public static void main(String[] args) {
Account one = new Account("Nishant", "UCO BANK", 4000);
one.display();
one.withdraw(-9);
one.withdraw(5000);
one.deposit(-9);
one.withdraw(2000);
one.display_balance();
one.deposit(1000);
one.display_balance();
}
}