Question

2. Write a java program that implements bank trancations using user deined exception 3. Write a java program to identify the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Bank Transactions using user defined exception.

Source code:-

import java.util.Scanner;
import java.lang.Exception;   
import java.util.Random;

class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
interface Account
{
void CreateAcc();
void Deposit();
void Withdraw();
}

class Bank extends BankUtilities implements Account
{
  
  
void getInfo()
{
try
{   
System.out.print("Enter Name Of the Acc Holder: "); //Enter Name Of the Acc Holder:
name=in.nextLine();
  
  
System.out.print("Enter SSN: "); //Enter SSN:
ssn=in.nextInt();
  
in.nextLine();
System.out.print("Enter Your Location: ");
loc=in.nextLine();
  
System.out.println("Mention your account type: \n 1.Savings\n 2.Current ");
acctype=in.nextInt();
switch(acctype)
{
case 1:
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
if(temp<0)
{
System.out.println("Invalid Amount\nTry again:\n");
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
}
Deposit(temp);
System.out.println("Enter no.of years:");
year=in.nextInt();
if(year<=0)
{
System.out.println("Invalid year\n Try again.");
System.out.println("Enter no.of years:");
year=in.nextInt();
}
break;
case 2:
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
if(temp<0)
{
System.out.println("Invalid Amount\nTry again:\n");
System.out.println("Enter the initial amount of deposit:");
temp=in.nextFloat();
}
Deposit(temp);
break;
default: System.out.println("Choose correct Option");
} //Switch
}
catch(Exception e)
{
System.out.println("Inbuilt Exception "+e);
System.exit(0);
}
  
}//Get Information
  
public void CreateAcc() //New account creation
{
try
{
getInfo();
System.out.println("\nYour Account is Successfully Created!");
accnum=rnd.nextInt(1000)+1;
System.out.println("Hello "+name+" your account no is " +accnum+".\n");
}
catch(Exception e)
{
System.out.println("Fatal Error");
}
}
  
//Deposit
void Deposit(double temp) // Initial Deposit
{
try
{
if(temp>500) //throws exception when deposited amount is less than 500
{
bal+=temp;
System.out.println("SUCCESSFULLY CREDITED");
}
else
{
throw new MyException("Minimum Deposit Violation");
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
System.exit(0);
}
  
}
  
public void Deposit() // regular deposit
{
  
System.out.println("Enter the amount to be deposited :");
temp=in.nextFloat();
try
{
if(temp>0)
{
bal+=temp;
System.out.println("SUCCESSFULLY CREDITED");
}
else
{
throw new MyException("INVALID AMOUNT EXCEPTION");
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
}
  
}

//Withdraw
public void Withdraw() // For Withdraw the money
{
  
System.out.println("Enter the amount to be withdrawn :");
temp=in.nextFloat();
if(temp<=0)
{
System.out.println("Invalid Amount Error.");
System.exit(0);
}

try
{
  
if(temp<bal)
{
bal-=temp;
System.out.println("SUCCESSFULLY DEBITED");
}
else
{
throw new MyException("INSUFFIECEINT FUND EXCEPTION");
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
System.out.println("TRANSACTION FAILURE");
}
  
}
  
//Checking Balance
void CheckBal() //To check the balance
{
System.out.println("Your Balance is :"+bal);
}
  
}

// Main class
class BankDemoException
{
public static void main(String arg[]) throws MyException
{
Scanner in = new Scanner(System.in);
Bank b = new Bank();

System.out.println("\n\n\t- - -Menu - - -");
try
{
while(true)
{
System.out.println(" 1.Create A New Account \n 2.Deposit Money \n 3.Check The remaining Balance \n 4.Withdraw \n 5.Exit.");
int ch = in.nextInt();
switch(ch)
{
case 1:
b.CreateAcc();
break;
  
case 2:
b.Deposit();
break;

case 3:
b.CheckBal();
break;   
  
case 4:
b.Withdraw();
break;

  
case 5:
System.exit(0);
break;
  
default: System.out.println("Enter correct option");
}
}
}
catch(Exception e)
{
System.out.println("exception "+e);
}
}
}


class BankUtilities
{
int acctype,year=1,ssn=0,accnum;
float intr;
String name="";
String loc="";
Scanner in = new Scanner(System.in);
Random rnd = new Random();
double temp=0.0,bal=0.0;
  
}

output of above program:-

C:\Users\VenkateshEmmandi\Desktop>java BankDemoException - - - Menu - - - 1. Create A New Account 2.Deposit Money 3. Check Th

1. Create A New Account 2.Deposit Money 3. Check The remaining Balance 4.Withdraw 5. Exit. Your Balance is :1000.0 1. Create

2.

what is Finally Block?

Finally block is used to execute the important code of the program.

Note that It is always executed whether exception is handled or not.

That means eventhough we didnot handle the exception , the code in finally block definitely executed.

By using three cases finally block is explained here.

case 1:- Finally block is executed when the exception does not occur.(finally block is executed here).

import java.io.*;
class finallyBlock{
   public static void main(String[] args) {
       try{
           int a = 30/3; //the case where exception is going to occur.
       }
       catch(NullPointerException e){
           System.out.println(e); // if exception occurs , it will handle using catch
       }
       finally{
           System.out.println("finally block is executed here eventhough exception does not occur");
       }

   }
}

Explanation of case1:-

exception does not occur in the above program because 30 is divided by 3 .So, there is no exception.

Eventhough there is no exception, code in finally block is executed here.

output of case1:-

C:\Users\n150789\Desktop\mlProjects>java finallyBlock finally block is executed here eventhough exception does not occur

case2:- Exception occurs in our program , but not handled that exception.(finally block is executed here).

import java.io.*;
class finallyBlock{
public static void main(String args[]){
try{
int result = 15 / 0; //the case where exception is going to occur.
System.out.println(result);
}
catch(NullPointerException e){
   System.out.println(e); // if exception occurs , it will handle using catch
}
finally{
   System.out.println("finally block is executed eventhough exception is not handled");
}    
}
}

output of case2:-

C:\Users\n150789\Desktop\mlProjects>java finallyBlock finally block is executed eventhough exception is not handled Exception

Explantion of case2:-

If we observe above code 15 is not divided by 0 so arithmetic exception is not present but we handle this exception using null pointer exception that means exception is not handled with the correct exception (arithmetic exception).

Eventhough it does not handled , the code in finally block is executed.

case3:- Exception occurs and handled (finally block is executed here also).

import java.io.*;
class finallyBlock{
public static void main(String args[]){
try{
int result=15/0; //the case where exception is going to occur.
System.out.println(result);
}
catch(ArithmeticException e){
   System.out.println(e); // if exception occurs , it will handle using catch
}
finally{
   System.out.println("finally block is always executed");
}

}
}

output of case 3:-

C:\Users\n150789\Desktop\mlProjects>java finallyBlock java.lang.ArithmeticException: / by zero finally block is always execut

Explantion of case 3: -

If we observe the above code, exception occurs i.e., 15 is not divided by 0 and that is arithmetic exception. In the catch block exception handled. That means there is no problem at all.

Therefore, finally we can say that Finally Block is always executed.

3.

Generate multiple threads of creating clock pulses using Runnable Interface:-

we can create threads using by extending Thread class and by implementing runnable interface.

Note that by creating object using any class does not mean that it is thread.

Thread is created only by using thread class and runnable interface.see below example.

java code :-

import java.io.*;

class multiThread implements Runnable{ //implementing runnable interface to create thread note that runnable interface is implemented by any class.
public void run(){ // this is the only method which will be in runnable interface
  
       for(int i = 1; i < 7;i++){
           try{
               Thread.sleep(500); // sleep method is used to sleep a thread for some amount of time
               System.out.println(i); // printing threads here i created 3 threads.
           }
           catch(InterruptedException e){
               System.out.println(e); // exception handled if it occurs.
           }

       }
}
  
public static void main(String args[]){
multiThread m1 = new multiThread(); //multithread is our created class by using 3 objects are created i.e., m1,m2,and m3
multiThread m2 = new multiThread();
multiThread m3 = new multiThread();

Thread t1 =new Thread(m1); // the object m1 used in thread class to create thread t1.
Thread t2 =new Thread(m2); // the object m2 used in thread class to create thread t2.
Thread t3 =new Thread(m3); // the object m3 used in thread class to create thread t3.
t1.start(); //starting t1 thread
t2.start(); //starting t2 thread
t3.start(); //starting t3 thread
}
}

output:-

C:\Users\n150789\Desktop\mlProjects>java multiThread,

Explantion of output :-

3 threads are created and every thread is stopped for some time i.e., 500 seconds after that time each thread executed.So, first 1,1,1(t1,t2,t3). After 500 seconds 2,2,2(t1,t2,t3) . It will goes on upto the loop termination condition (only 6 times). Because 7th time loop is terminated.

Add a comment
Know the answer?
Add Answer to:
2. Write a java program that implements bank trancations using user deined exception 3. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me to solve this problem with java language! write a program that runs a...

    Please help me to solve this problem with java language! write a program that runs a thread which prints a timestamp and then waits a second by doing the following: 1. Implement a class that implements the Runnable interface. (1 point) 2. Place the code for your task into the run method of your class. (6 points) a) To get the date and time, construct a Date object. b) To wait a second, use the sleep method of the Thread...

  • JAVA Question 11 (20 points) Using your choice of language, C# or Java (no pseudocode allowed),...

    JAVA Question 11 (20 points) Using your choice of language, C# or Java (no pseudocode allowed), give a format/template of Exception Handling codes with try, catch, and finally blocks. In the try block include some statements that raise an arithmetic exception and the catch block catches that arithmetic exception. You don't need to write any complete method or program, just write the code segments with try, catch, and finally blocks. O Paragraph BI U Question 12 (2 points)

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • Can some please help me to answer these two questions. Write a program in Java create...

    Can some please help me to answer these two questions. Write a program in Java create a class diagram fragment with two classes: User and Patient. Patient is a sub-class of User. User has a method getName(), which is both over-loaded and over-ridden in the subclass In Java write an abstract class called User. User has one abstract method called authenticate, which takes two string parameters. Write a concrete sub-class of User called Administrator with an implementation of the authenticate...

  • You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an...

    You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an array of instances of the class Book. The class Book is loaded in our Canvas files: Book.java The user will enter a number n (n must be > 0, trap the user until they input a valid value for n), Your program will declare and create an array of size n of instances of the class Book. The user will be asked to enter...

  • USING PYTHON *Write a program, in which the user enters two numbers, then divides the first...

    USING PYTHON *Write a program, in which the user enters two numbers, then divides the first number by the second, and prints the result to two decimal places. *Make sure that that the inputs are floats (with exception handling using the ValueError). *Make sure that there is no divide by zero error (with exception handling). *Then have a plain exception: That handles any other error. -Thank you in advance for your assistance-

  • JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The...

    JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The interface components will appear centered in the interface, and in two rows. o Row one will have a Label that reads “Please enter a valid integer:” and a Text Field to take in the integer from the user. o Row two will have a Button that when pressed converts the integer to binary  The conversion will be completed using recursion – A separate...

  • .)   Write a program to implement the exception handling with multiple (at least 3) catch statements....

    .)   Write a program to implement the exception handling with multiple (at least 3) catch statements. Any type of exception may be thrown. Catch statements must display (cout) the type of exception thrown. .)   Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in "main()". (This is pretty open ended, so anything from a calculator or "what have...

  • Write ONE application program by using the following requirements: Using JAVA File input and outp...

    Write ONE application program by using the following requirements: Using JAVA File input and output Exception handling Inheritance At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods At least one interface: this interface needs to have at least two abstract methods At least one method overloading At least one method overriding At least...

  • In java Write a program that asks the user to choose a selection (Enter 1 to...

    In java Write a program that asks the user to choose a selection (Enter 1 to convert from binary to decimal, 2 to convert from decimal to binary, 3 to convert binary to hexadecimal, 4 to convert hexadecimal to binary, 5 to convert decimal to hexadecimal, 6 to convert hexadecimal to decimal. Pls do it by creating classes and objects with get and set method.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT