Question

In: Computer Science

Implementing Comparable and Inheritance You will use what you have learned from Chapters 9 in Big...

Implementing Comparable and Inheritance

You will use what you have learned from Chapters 9 in Big Java: Late Objects to complete an Inheritance SavingsAccount program. Use what you have learned about the classes, Inheritance, and Comparable to complete the project.

Please comment your program correctly. **Please do not use any advanced material that is not in Chapter8 and 9.

Download the attached InheritanceTester.java file and make your modifications. The program includes testing code in main().

:
This is the code!
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.

Solutions

Expert Solution

Here are the updates to the BankAccount.java and SavingsAccount.java classes with comments.

Thanks.

===========================================================================

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;
    }

    //1) add toString method to BankAccount
    public String toString() {
        return "Current Balance: $" + balance;

    }
}

==================================================================

//5) Implement the Comparable Interface for SavingsAccount based on balance.
class SavingsAccount extends BankAccount implements Comparable<BankAccount>{


    private int withdrawlCount;

    //3) Provide constructors for SavingsAccount
    public SavingsAccount(double balance) {
        super(balance);
        withdrawlCount = 0;
    }

    public SavingsAccount() {
        super(0);
        withdrawlCount = 0;
    }

    //2) Override the withdraw() in SavingsAccount so that it will not
    // withdraw more money than is currently in the account.
    public void withdraw(double amount) {

        //// will not withdraw more money than is currently in the account.
        if (amount > getBalance()) {
            System.out.println("Insufficient balance");
        } else {
            withdrawlCount += 1;
            super.withdraw(amount);
        }
        //4) Add this feature to SavingsAccount: If you withdraw
        // more than 3 times you are charged $10 fee
        if (withdrawlCount > 3) {
            super.withdraw(10); // subtract $10
            //deducted you get another 3 free withdrawals.
            withdrawlCount = 0; //r eset to zero
        }
    }

    //1) SavingsAccount classes
    public String toString() {
        return "$" + getBalance();
    }

    //5) Implement the Comparable Interface for SavingsAccount based on balance.
    public int compareTo(BankAccount o) {
        return Double.compare(getBalance(),o.getBalance());
    }
}

==================================================================


Related Solutions

1) What is the most surprising thing about money you have learned from reading chapters in...
1) What is the most surprising thing about money you have learned from reading chapters in Economics. 2) What do you think about free trade agreements Canada has entered into? Recently, Canada signed a new agreement with several European countries- comments, opinions, thoughts?
As you read the chapters and review the videos reflect on what you have learned about...
As you read the chapters and review the videos reflect on what you have learned about government intervention to redistribute income and assist with retirement and healthcare funding.. Write a two page presentation of your own views in your own words about the sources of inequality in wealth and income and whether this inequality should be reduced and how.
Summarize concepts from the first 3 chapters. What have you learned? I would like to see...
Summarize concepts from the first 3 chapters. What have you learned? I would like to see summary of: 1. Chapter 1: Equity method (new account titles, how account balances change for each) 2. Chapter 2: List the 3 types of business combinations and define them. Define acquisition method Also list the first 2 consolidation entries for acquisitions. 3. Chapter 3: What are the 3 methods parents can use to record investment in subsidiary? What are the consolidation entries for each...
Take a moment and reflect on what you've learned from chapters 12-13 and use this knowledge...
Take a moment and reflect on what you've learned from chapters 12-13 and use this knowledge to investigate how the new Pfizer COVID-19 vaccine might work to stimulate the immune system. Discuss how the mRNA-based vaccine strategy may effectively induce BOTH a humoral and cellular immune response and talk about the key players. You'll need to take some time to learn a bit more about the vaccine if you're unfamiliar with the mRNA-based approach before answering this question.
Review the chapters and videos and reflect on what you have learned about the "sustainability" problems...
Review the chapters and videos and reflect on what you have learned about the "sustainability" problems with our economy and conventional economics. Write a two-page presentation of your own views in your own words about whether you agree that such problems exist, and if they do, whether the proposed solutions are adequate, appropriate, and sufficiently detailed and capable of being put into practice.
Consider what you have learned from the unit's studies. What statistics do you use in the...
Consider what you have learned from the unit's studies. What statistics do you use in the real world? What scales of measurement are you using (for example, nominal, ordinal, interval, or ratio)? Please identify at least two instances of using statistics in the real world and explain the scale of measurement for each variable. Be sure to reference the text and articles.
Using the key concepts, you have learned in chapters 10 and 11, write a minimum of...
Using the key concepts, you have learned in chapters 10 and 11, write a minimum of a 3 page paper on 1 of the following subjects from the perspective of the CFO: 1-You are starting a new company and are trying to come up with your optimal capital structure. Describe the nature of your business and why you believe your capital structure to be the best fit for your company. An example may be that you are attempting to open...
n this lab, you use what you have learned about searching an array to find an...
n this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten C++ program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user the city name is not found in...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1:   Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today.    Option B: Receive a $1600 gift each year for the next 10...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1:   Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today.    Option B: Receive a $1600 gift each year for the next 10...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT