简体   繁体   中英

Abstract Class/Method Implementation

I'm supposed to design a BankAcct class with an abstract method computeIntt() . Save and Time are kinds of BankAcct. The problem is I can't implement the displaying of the Interest and the New balance

Here's my current codes:

public abstract class BankAcct 
{
    private int accountNumber;
    private String accountName;
    private double bal;

    public BankAcct(int act, String name, double m)
    {
        accountNumber = act;
        accountName = name;
        bal = m;
    }

    public double getBalance()
    {
        return bal;
    }

    public String getAccountName()
    {
        return accountName;
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void deposit(double m)
    {
            bal+=m;
    }

    public void withdraw(double m)
    {
        bal-=m;
    }

    public abstract double computeInt();
}

public class Save extends BankAcct 
{
    public Save(int act, String name, double m)
    {
        super(act, name, m);
    }

    int act;
    String name="";
    double m;

    public double computeInt()
    {
        return m * 0.0025;
    }

}

public class Time extends BankAcct 
{
    public Time(int act, String name, double m)
    {
        super(act, name, m);
    }

    int act;
    String name="";
    double m;

    public double computeInt()
    {
        return m * 0.033;
    }

}

public class MainPr 
{
    public static void main(String[] args)
    {
        BankAcct sav = new Savings(1234, "ABC", 10000);
        BankAcct td = new TimeDeposit(9876, "DEF", 20000);

        // display the current balance

        System.out.println(sav.getAccountName() + " with account number " +
                    sav.getAccountNumber() + " has a balance of " +
                    sav.getBalance());

        System.out.println(td.getAccountName() + " with account number " +
            td.getAccountNumber() + " has a balance of " +
            td.getBalance());

        // deposit money

        sav.withdraw(5000.0);

        // display current value with its interest

        System.out.println("\n" + sav.getAccountName() + " with account number " +
            sav.getAccountNumber() + " earned an interest of " +
            sav.computeInt());

        System.out.println(sav.getAccountName() + " with account number " +
            sav.getAccountNumber() + " has a NEW BALANCE of " +
            sav.getBalance());

        td.deposit(2000.0);

        System.out.println("\n" + td.getAccountName() + " with account number " +
            td.getAccountNumber() + " earned an interest of " +
            td.computeInt());

        System.out.println(td.getAccountName() + " with account number " +
            td.getAccountNumber() + " has a NEW BALANCE of " +
            td.getBalance());
    }
}

MainPr that will instantiate two objects for the following data: Save has account number of 1234; account name of ABC; balance of 10,000. Time has account number of 9876; account name DEF; balance of 20,000.

This should be the output: CORRECT ONE

ABC with account number 1234 has a balance of 10000.0

DEF with account number 9876 has a balance of 20000.0

ABC with account number 1234 earned interest 25.0

ABC with account number 1234 has a NEW balance of 5025.0

DEF with account number 9876 earned interest 660.0

DEF with account number 9876 has a NEW balance of 22660.0

but my program produces the incorrect output:

ABC with account number 1234 has a balance of 10000.0

DEF with account number 9876 has a balance of 20000.0

ABC with account number 1234 earned interest 0.0

ABC with account number 1234 has a NEW balance of 5000.0

DEF with account number 9876 earned interest 0.0

DEF with account number 9876 has a NEW balance of 22000.0

In this case, in Save the interest rate is 0.25% while the interest in time is 3.3% The balance depends whether it is deposit or withdraw.

Kindly help me. thanks

Your instance variables m are always 0 (never initialized) in your subclasses, but they look like they are set to the bal in your BankAcct class. Wouldn't this be appropriate for your computerInterest method in TimeDeposit :

public double computeInterest() {
    return getBalance() * 0.033;
}

A similar fix should be provided in the other subclass or at least initialize your m variables. On a related note, your subclasses don't need to define act or name since your superclass BankAcct has it defined and stored already.

Also, your computerInterest isn't modifying your balance, which your program seems to expect. If that's the case, this might be more appropriate:

public double computeInterest() {
    double interest = getBalance() * 0.033;
    deposit(interest);
    return interest;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM