Question

1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so...

1) add toString method to BankAccount and SavingsAccount classes
2) Override the withdraw() in SavingsAccount so that it will not withdraw more money than is
currently in the account.
3) Provide constructors for SavingsAccount
4) Add this feature to SavingsAccount: If you withdraw more than 3 times you are charged $10 fee
and the fee is immediately withdrawn from your account.once a fee is deducted you get another 3 free withdrawals.
5) Implement the Comparable Interface for SavingsAccount based on balance.
Once you finish adding/modifying the code make sure it compiles and run.
Submit your modified InheritanceTester.java file to the assignment folder

Warning: Notice that there are 3 classes in one file, and only one of them is public,
and this public class name should be the filename. Be careful about the { } braces.

public class InheritanceTester{
public static void main(String[] args){
SavingsAccount tom = new SavingsAccount(5000);
SavingsAccount kim = new SavingsAccount();
tom.withdraw(100);tom.withdraw(1000);tom.withdraw(200);
kim.withdraw(100);//should print error message: Insufficient balance
System.out.println(tom);//should print $3700 as balance
tom.withdraw(1000);
System.out.println(tom);//should print $2690 as balance, and fee charged
tom.withdraw(1000);
System.out.println(tom);//should print $1690 as balance
}
}

class BankAccount
{
private double balance;
public BankAccount()
{ balance = 0; }

public BankAccount(double initialBalance)
{ balance = initialBalance; }

public void deposit(double amount)
{ balance = balance + amount; }

public void withdraw(double amount)
{ balance = balance - amount; }

public double getBalance()
{ return balance; }
}
class SavingsAccount extends BankAccount{
//add code here.


}

Homework Answers

Answer #1

Solution :-

Program: In this program, we create the class SavingsAccount which extends BankAccount class. It has two constructors, one default and one arguments constructor, and 4 other methods. Both classes have a toString() method to display information.

Code:

public class InheritanceTester{
    public static void main(String[] args){

        //Creating objects and testing
        SavingsAccount tom = new SavingsAccount(5000);
        SavingsAccount kim = new SavingsAccount();
        tom.withdraw(100);
        tom.withdraw(1000);
        tom.withdraw(200);
        kim.withdraw(100);//should print error message: Insufficient balance
        System.out.println(tom);//should print $3700 as balance
        tom.withdraw(1000);
        System.out.println(tom);//should print $2690 as balance, and fee charged
        tom.withdraw(1000);
        System.out.println(tom);//should print $1690 as balance

    }
}

//Class BankAccount
class BankAccount
{
    private double balance;
    public BankAccount()
    { balance = 0; }

    public BankAccount(double initialBalance)
    { balance = initialBalance; }

    public void deposit(double amount)
    { balance = balance + amount; }

    public void withdraw(double amount)
    { balance = balance - amount; }

    public double getBalance()
    { return balance; }

    @Override
    public String toString() {
        return "BankAccount: " +
                "Balance = $" + balance;
    }
}
//SavingsAccount class
class SavingsAccount extends BankAccount{
//add code here.

    //Member variables for balance and withdraw count checker
    private double balance;
    int countCheck;

    //Constructor
    public SavingsAccount()
    {
        balance = 0;
    }

    //Args Constructor
    public SavingsAccount(double initialBalance)
    {
        this.balance = initialBalance;
    }

    //Function to deduct $10 after 3 withdraws
    public void withdrawDeduction()
    {
        balance = balance - 10;
        countCheck = 0;
    }

    //Withdraw function of Bank Account class modified
    @Override
    public void withdraw(double amount)
    {
        if ((balance - amount < 0) || (amount < 0))
        {
            System.out.println("Insufficient Balance");
        }
        else
            {
            balance = balance - amount;
            countCheck++;
            if (countCheck > 3)
            {
                withdrawDeduction();
            }
        }
    }

    //ToString() method
    @Override
    public String toString()
    {
        return "SavingsAccount: " +
                "Balance = $" + balance;
    }

    //Compares to method
    public int compareTo(SavingsAccount other)
    {
        if(balance < other.balance) return -1;
        if(balance == other.balance) return 0;
        return 1;
    }
}

Output:

Know the answer?
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
Similar Questions
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private data fields to store the account holder's name and the account balance A constructor with 0 arguments A constructor with 1 argument (account holder's name) A constructor with 2 arguments(account holder's name and account balance) Public properties for the account holder's name and the account balance. Do not use auto-implemented properties. A method to increase the balance (deposit) A method to decrease the balance...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions:...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions: 1. Fully parametrized Constructor (to initialize class variables) 2. Getter (getBalance()) and Setter (setBalance()) for the float balance, must be greater than zero. In Account.java Methods: checkBalance( ) to print CURRENT BALANCE. deposit(int added_amount) should add add_amount to balance withdraw(int withdraw_amount) should deduct withdraw_amount balance display( ) will print all the information (account number, name, balance). Class 2: AccountTest.java Create an object account1 and...
Create a client for your BankAccount classcalled BankAccountClient.java.    This a separate file from the BankAccount file....
Create a client for your BankAccount classcalled BankAccountClient.java.    This a separate file from the BankAccount file. Both files must be in the same directory to compile. Display all dollar amounts using the DecimalFormat class. Create an account Ask the user for the type of account, the bank account number, the amount that they will deposit to start the account. Set interest earned to 0. Print the account information using an implicit or explicit call to toString Update an account Use...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[]...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[] gpaarr ) to calculate the average of gpaArray; 2) add codes in the for loop to calculate sum 3) print the average . public class Test { public static void main (String args[]) { double[ ] gpaArray = { 2.5, 4.0, 3.8, 3.2, 2.9, 4.0 } ;   //add your codes here (you can put your codes in the Answer area with/without copying the original...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here. Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT