/**
 * Class BankAccount - a simple model of a bank account
 * designed to illustrate the concepts of classes, objects, and methods.
 *
 * @author Man Hin Wong 
 * 
 * @version 2010.08.12-version
 */


class BankAccount
{

    // These are the instance variables

    private int balance;
    private String accountName;


    // TODO Add new instance variables here
    private int valueDeposits;
    private int valueWithdrawals;
    private int maximumBalance;
    private int minimumBalance;


    // This is the constructor

    public BankAccount(String accountName, int balance)  {

     this.accountName = accountName;
     this.balance = balance;

     // TODO Initialise all new instance variables
     this.valueDeposits = 1000;
     this.valueWithdrawals = 0;
     this.maximumBalance = balance;
     this.minimumBalance = balance;

    }

    // Prescribed accessor methods 

    public int getBalance() {
      return balance;  
    }

    public String getAccountName() {
         return accountName;
    }

    // TODO Methods that need updating 

    public void deposit(int amount) {
      balance = balance + amount;
      valueDeposits = valueDeposits + amount;
      if (maximumBalance<balance) {
            maximumBalance=balance;
        }
      // TODO Handle instance variables that change on deposit

    }

    public void withdraw(int amount) {
      balance = balance - amount;       
      valueWithdrawals = valueWithdrawals + amount;
      if (minimumBalance>balance) {
          minimumBalance=balance;
        }
      // TODO Handle instance variables that change on withdrawal

    }

    public int getValueDeposits() {

        // TODO Complete this accessor method and return correct value

        return valueDeposits;
    }

    public int getValueWithdrawals() {

        // TODO Complete this accessor method and return correct value

        return valueWithdrawals;
    }

    public boolean isOverdrawn() {

        // TODO Complete this accessor method and return correct value
       if (balance<0) {
           return true;}
           else
           {
        return false;} // always fail
    }

    // TODO Add additional accessor methods here (there are two)
            public int getMaximumBalance() {
                return maximumBalance;
            }
            public int getMinimumBalance() {
                return minimumBalance;
            }
    // TODO Add additional method here (there is one)
        public void applyInterest(double rate) {

if (valueWithdrawals>balance) {
           interest = (int) (balance*double rate);

        }
    }
}

Instruction:
This method credits interest to an account provided it is not overdrawn and depends on the parameter rate which is of type double (thus an interest rate of 5% is represented as 0.05). The interest rate will always be positive (> 0). Overdrawn accounts remain unaffected by this method. For example, if the call applyInterest(0.0325) is made and the account has a current balance of 1055, the new balance should be 1089.

Note that multiplying an int by a double gives a double result: in order to assign this to an int variable, you need to cast the result to an int.

int interest;
int balance = 159;
double rate = 0.05;

interest = balance*rate;         // This will not compile because balance*rate (=7.95) is a double
                                 // (extended decimal) and interest is an int (integer)
interest = (int) (balance*rate); // This will work and be equal to 7 (a truncation of 7.95).

Don't forget the side effects of depositing interest: adding interest counts as a deposit for the purpose of computing the total value of all deposits and may change the historical maximumBalance.

I don't know what's going on here.
I can't fix this and I have to hand in my work soon!!

Recommended Answers

All 2 Replies

I only glanced the the code.
but why you write
:

interest = (int) (balance*double rate);

i don't think your suppoused to write that "double"
(most likley i'm wrong...but still )
or try taking off those () in the balace*rate

If you want something converted to int or double do what you correctly did

interest = (int)(.....);

Of course you were trying to convert rate to a double too. I haven't looked what is its type but if it is not double and you want it to be double do what you did with int:

interest = (int) (balance * (double)rate);

Or

interest = (int) (balance * 1.0* rate);

If balance is already double then you don't need the casting. If balance is double then when you do this:
balance * rate, then you multiply double with something else which will result double:

interest = (int) (balance * rate);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.