reset password
Author Message
disrael
Posts: 44
Posted 12:51 Oct 11, 2013 |

I'm just a little confused on what I'm doing wrong or what I'm missing from my code. This is my code and I can't seem to get the balance to print right. If anyone has any suggestions it would be very welcomed. Balance keeps coming up as 0.0.


public class BankAccountTester
{
   
    private static double bankAcc; //Stores bankAcc balance
    private static double balance;
   
    public void addDeposit(double deposit) {
        
     balance = balance + deposit;
    deposit= 1000;
     double withdraw1 = 400;
        double withdraw2= 500;
        bankAcc= balance - withdraw1 - withdraw2;
      
    }
    public double getbankAcc()//Returns value to bankAcc so it has new balance
    {
    return bankAcc;
    }
   
    
    //Prints out value and expected value
    public static void main(String[] args){
    System.out.println("My bank account has " + bankAcc);
    System.out.println("Expected is 100");
    }
    
}
   

 

Last edited by disrael at 15:47 Oct 11, 2013.
Eric Liao
Posts: 158
Posted 09:29 Oct 12, 2013 |

The balance is 0.0 is because that by default java will initialize double to be 0. Which at your case, the bankAcc variable is never modified.

The reason the bankAcc is never modified is that you haven't called the method addDeposit(double deposit).

A simple way to fix:

call addDeposit(100) in the main method and see what happen!

(hint: you probably need to add static to the method)

---

A minor note:

I would recommend to construct a BankAccount class that is separated from BankAccountTester, and in BankAccountTester, you just need to construct a BankAccount and call addDeposit() method test.