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

D *SavingsAccount.java × 1 package code; 3 public class SavingsAccount /* Task *

ID: 3603663 • Letter: D

Question

D *SavingsAccount.java × 1 package code; 3 public class SavingsAccount /* Task *Define this class to that represents a simple Savings Account Pay attention to the names, parameters and return types of the methods described. They must be exactly as given, else the code won't compile * on AutoLab 10 *The account object must have two private instance variables, one of type * double representing the current balance, and the other of type boolean *indicating whether the account has authorization to perform a withdrawal L2 13 14 15 16 17 18 *The balance of the account must never go below zero. A withdrawal must * not be done unless there is prior authorization. If authorization has *been obtained a single withdrawal can be performed. Performing an *authorized withdrawal rescinds the withdrawal, preventing any additional *withdrawals from taking place until authorization for another withdrawal * is obtained The constructor of the class must initialize the balance to zero, and the * withdrawal authorization to false 24 *The "deposit' method must verify that the amount to be deposited is * non-negative before proceeding with the deposit. The 'deposit' method *must have a void return type * The "withdraw. method must verify that the amount to be withdrawn is 30 31 32 non-negative, that the amount is no greater than the current balance, and that the withdrawal is authorized before proceeding with the withdrawal *The 'withdraw' method must have a void return type 34 * The 'balance' method must return the current balance. It must have a *double return type 36 37 38 39 40 41 The "authorize' method must set the authorization to true. It must have a * void return type The "authorized' method must return the authorization It must have a * boolean return type 43 46

Explanation / Answer

class SavingsAccount {

private double bal;

private boolean isAuthorized;

public SavingsAccount() {

bal = 0.0;

isAuthorized = false;

}

void deposit(double amt) {

if ( amt >= 0.0)

bal += amt;

}

void withdraw(double amt) {

if (isAuthorized == true) {

isAuthorized = false;

if ( amt >= 0.0 && amt<= bal) {

bal -= amt;

}

isAuthorized = true;

}

}

double balance() {

return bal;

}

void authorize() {

isAuthorized = true;

}

boolean authorized() {

return isAuthorized;

}

}