Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA EXAM QUESTIONS. 1) Create two subclasses of Account for checking and saving

ID: 659760 • Letter: J

Question

JAVA EXAM QUESTIONS.

1) Create two subclasses of Account for checking and savings accounts. Override the withdraw method so that a checking account has an overdraft limit, and a savings account cannot be overdrawn

2) Write a program that prompts the user to enter two integers and displays their sum. Using exceptions, your program should prompt the user to enter the number again if the input is incorrect. (Reminder: nextInt() throws InputMismatchException if the next token does not match the Integer regular expression, or is out of range)

Explanation / Answer

1)Account.java

public abstract class Account {

   public abstract void withdraw(int amount);

}

Checking.java

public class Checking extends Account {

   public void withdraw(int amount) {

       System.out.println("Amount is with drawn " + amount);

   }

}

Savings.java

public class Savings extends Account {

   public void withdraw(int amount) {

       System.out.println("Amount cannot be withdrawn");

   }

}

2) Sum.java

import java.util.InputMismatchException;

import java.util.Scanner;

public class Sum {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       int num1 = 0;

       int num2 = 0;

       try {

           System.out.println("Please enter Integer 1: ");

           num1 = input.nextInt();

           System.out.println("Please enter Integer 2: ");

           num2 = input.nextInt();

       } catch (InputMismatchException e) {

           System.out.println("Please Enter A Valid Integer");

       }

       System.out.println("Sum = " + (num1 + num2));

   }

}