Results 1 to 4 of 4
Thread: TimeDepositAccount class
- 01-27-2013, 07:26 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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; } }
the result is: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); } }
500.0
500.0
which is not correct, any idea why I am having wrong values? Thanks in advance!
-
Re: TimeDepositAccount class
How do you propose that the double variable bal will change from line (A) to line (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.Java Code:TimeDepositAccount3 myChecking = new TimeDepositAccount3(); double bal = myChecking.currentBalance(); System.out.println(bal); // (A) myChecking.addInterest(); System.out.println(bal); // (B)Last edited by Fubarable; 01-27-2013 at 07:47 AM.
- 01-27-2013, 08:17 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Re: TimeDepositAccount class
Indeed, they weren't related at all! I changed it to this:
Thank you!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); } }
-
Similar Threads
-
Class example not compiling Runner class and Swimmer class
By Joshsmith in forum New To JavaReplies: 1Last Post: 12-13-2012, 03:06 AM -
could not find or load main class | cannot manually locate class file
By Holden Caulfield in forum New To JavaReplies: 1Last Post: 11-29-2012, 09:46 AM -
How to get a compatible class of a template class? Return type of method is AClass<E>
By SKuypers in forum Advanced JavaReplies: 0Last Post: 12-07-2011, 11:55 AM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks