Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BankMachine.java import java.util.Scanner; public class BankMachine { public static void main(String[] args) { BankAccount newAcc; Scanner input = new Scanner(System.in); System.out.println(Enter your desired account type:

image text in transcribedimage text in transcribed

image text in transcribed

BankMachine.java

import java.util.Scanner;

public class BankMachine {

public static void main(String[] args) {

BankAccount newAcc;

Scanner input = new Scanner(System.in);

System.out.println("Enter your desired account type: 'c' for checking or 's' for savings."); String accountType = input.next();

if (accountType.equals("c")) { newAcc = new CheckingAccount(100); } else if (accountType.equals("s")) { newAcc = new SavingsAccount(100, 0.14); } else { System.out.println("Invalid account type."); return; // If bad input, then end program. }

String accountInfo = newAcc.toString();

System.out.println("Your new account has been successfully created."); System.out.println(accountInfo);

System.out.println("Transactions: " + newAcc.getTransactionCount()); System.out.println("InterestRate: " + newAcc.getInterestRate()); } }

= 1. Open Bank Machine.java and read over the code in its main() method. 2. The program produces two compilation errors, one when invoking method getTransactionCount() and the other when invoking getInterestRate(). Explain why the compiler issues these two errors. Write your answer in AnswersLab3.txt. 3. In the statement String accountInfo newAcc. tostring(); just by looking at the code can you tell whether the method toString() being invoked is from SavingsAccount or from CheckingAccount? Explain your answer and write it in AnswersLab3.txt. 4. Modify the code so that newAcc.getTransactioncount() is invoked only if newAcc references an object of the class CheckingAccount, and newAcc.getInterestRate() is invoked only if newAcc references an object of the class SavingsAccount. 5. Run the program and verify that it works correctly for both account types (run the program and type either 'c' or 's' so the program prints a message indicting that an account has been created and it also prints information about the account including either the interest rate or the number of transactions). public class CheckingAccount extends BankAccount /* Attribute: */ /* Counts number of transactions on the checking account */ private int transactionCount; /* Constants: */ /* Number of free transactions */ private static final int FREE_TRANSACTIONS = 3; /* Fee per transaction */ private static final double TRANSACTION_FEE = 2.0; /** Constructor: Creates a new instance of CheckingAccount * @param initialAmount Initial deposit for the new account */ public CheckingAccount(double initialAmount) { super (initialAmount); transactionCount = 0; } /** Method to access the transaction count * @return transactionCount the number of transactions public int getTransactionCount() { return transactionCount; } /** Method to deposit money into the checking account * @param amount amount to be deposited */ public void deposit(double amount) { transaction Count++; super.deposit(amount); } /** Method to withdraw money from the checking account * @param amount amount to be withdrawn */ public void withdraw(double amount) { transaction Count++; super.withdraw(amount); } /** Method to calculate fees and deduct this amount from the balance transactionCount is reset to o to start counting new transactions public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; } /** Method to represent checking account as a String for display purposes * @return String representation of checking account */ public String toString() { return ("CheckingAccount: balance $" + getBalance() + ", transactions " + transactionCount); } } * BankAccount.java: This class is a simple model of a general banking account, to be used for understanding inheritance. * @author From the book "Big Java" by Cay Horstmann public class BankAccount { /** Attribute: Keeps track of current balance */ private double balance; /** Creates a new instance of BankAccount * @param initialAmount initial amount deposited into new account */ public BankAccount(double initialAmount) { balance = initialAmount; } /** Accessor method for balance * @return current balance */ public double getBalance() { return balance; } /** Deposit money into account * @param amount amount to be deposited */ public void deposit (double amount) { balance = balance + amount; } /** Withdraw money from account * @param amount amount to be withdrawn */ public void withdraw(double amount) { if (amount

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

8. How would you explain your decisions to the city council?

Answered: 1 week ago