Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By FOX427

Thread: TimeDepositAccount class

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    50
    Rep Power
    0

    Post TimeDepositAccount class

    Hi All!
    Having an issue with implementing TimeDepositAccount class which has interest rate set in the constructor together with the initial balance. I need to provide a method to get the current balance, method to add the earned interest to the account and withdraw method that removes the entire balance. Note that addInterest method shouldn't have any parameters as we already know it. As far I am here:

    Java Code:
    public class TimeDepositAccount3 {
    	private double interestRate;
    	private double balance;
    	
    	public TimeDepositAccount3()
    	{
    		interestRate=1.2;
    		balance=500;
    	}
    	public double currentBalance()
    	{
    		return balance;
    	}
    	public void addInterest()
    	{
    		double newBalance = balance*interestRate;
    		balance = newBalance;
    	}
    	public void withdraw()
    	{
    		balance=balance-balance;
    	}
    
    }

    Java Code:
    public class TimeDepositAccountTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		TimeDepositAccount3 myChecking = new TimeDepositAccount3();
    		double bal = myChecking.currentBalance();
    		System.out.println(bal);
    		myChecking.addInterest();
    		System.out.println(bal);
    
    	}
    
    }
    the result is:
    500.0
    500.0
    which is not correct, any idea why I am having wrong values? Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: TimeDepositAccount class

    How do you propose that the double variable bal will change from line (A) to line (B)?

    Java Code:
            TimeDepositAccount3 myChecking = new TimeDepositAccount3();
            double bal = myChecking.currentBalance();
            System.out.println(bal);  // (A)
            myChecking.addInterest();
            System.out.println(bal);  // (B)
    in other words where is there a bal = something between those two lines? Note that changing the state of myChecking cannot mathemagically change the value of an unrelated double variable.
    Last edited by Fubarable; 01-27-2013 at 07:47 AM.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    50
    Rep Power
    0

    Post Re: TimeDepositAccount class

    Indeed, they weren't related at all! I changed it to this:

    Java Code:
    public class TimeDepositAccountTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		TimeDepositAccount3 myChecking = new TimeDepositAccount3();
    		double beforeInterest = myChecking.currentBalance();
    		System.out.println(beforeInterest);
    		myChecking.addInterest();
    		double afterInterest = myChecking.currentBalance();
    		System.out.println(afterInterest);
    
    	}
    
    }
    Thank you!
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  2. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  3. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  5. Replies: 5
    Last Post: 01-26-2011, 06:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •