Results 1 to 6 of 6
- 04-21-2012, 09:32 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
I don't find where I did wrong ...
Hello! I'm currently learning java from a book and made this exercise, the withdraw() and deposit() methods doesn't work, some help please?
here is my code:
Java Code:package Chapter8; import java.util.*; public class Ex8_7 { public static void main (String args[]){ Account account = new Account(1122, 20000.0D); account.setAnnualInterestRate(4.5D); account.withdraw(2500.0D); account.deposit(3000.0D); System.out.print("The balance is "+account.getBalance()+"\n"+"The monthly interest rate is "+account.getMonthlyInterestRate()+"\n" +"The date when this account was created is: "+account.getDateCreated()); } } class Account{ private int id; private double balance; private double annualInterestRate; private Date dateCreated = new Date(); Account(){ } Account(int newID, double newBalance){ id = newID; balance = newBalance; } public int getID(){ return id; } public void setID(int newID){ id = newID; } public double getBalance(){ return balance; } public void setBalance(double newBalance){ balance = newBalance; } public double getAnnualInterestRate(){ return annualInterestRate; } public void setAnnualInterestRate(double newAnnualInterestRate){ annualInterestRate = newAnnualInterestRate; } public String getDateCreated(){ return dateCreated.toString(); } public double getMonthlyInterestRate(){ return Math.pow(1.0D+annualInterestRate, 1/12.0D) - 1.0D; } public double withdraw(double a){ return balance - a; } public double deposit(double a){ return balance + a; } }
- 04-21-2012, 09:45 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 11
Re: I don't find where I did wrong ...
Your deposit and withdraw methods do not change the value of balance. They merely return balance plus/minus the value passed to the methods
- 04-21-2012, 10:03 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Re: I don't find where I did wrong ...
Can you please tell me if this is good? It's working ... And tell me if tere are other alternatives just for my knowledge.a
The new code:
Java Code:package Chapter8; import java.util.*; public class Ex8_7 { public static void main (String args[]){ Account account = new Account(1122, 20000.0D); account.setAnnualInterestRate(4.5D); account.withdraw(2500.0D); account.deposit(3000.0D); System.out.print("The balance is "+account.getBalance()+"\n"+"The monthly interest rate is "+account.getMonthlyInterestRate()+"\n" +"The date when this account was created is: "+account.getDateCreated()); } } class Account{ private int id; private double balance; private double annualInterestRate; private Date dateCreated = new Date(); Account(int newID, double newBalance){ id = newID; balance = newBalance; } public int getID(){ return id; } public void setID(int newID){ id = newID; } public double getBalance(){ return balance; } public double setBalance(double newBalance){ return balance = newBalance; } public double getAnnualInterestRate(){ return annualInterestRate; } public void setAnnualInterestRate(double newAnnualInterestRate){ annualInterestRate = newAnnualInterestRate; } public String getDateCreated(){ return dateCreated.toString(); } public double getMonthlyInterestRate(){ return Math.pow(1.0D+annualInterestRate, 1/12.0D) - 1.0D; } public double withdraw(double a){ return setBalance(balance-a); } public double deposit(double a){ return setBalance(balance+a); } }
- 04-22-2012, 05:25 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 84
- Rep Power
- 0
Re: I don't find where I did wrong ...
looks good to me....but why is there a setBalance method? makes the code more confusing to me. You also already have a getBalance method to return the balance, so the withdraw/deposit methods have no need to return anything. I would do it like this personally....
Java Code:private void withdraw(double balance) { this.balance = balance; }
- 04-22-2012, 05:28 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Re: I don't find where I did wrong ...
Yes, you're wright, but the "this." concept is explained in the next chapters so I didn't know about it ... I follow the book. Thank you very much! :)
Edit1: Oh, the withdraw and deposit methods must return the "balance - a" so is must be an expression there for the "+/-" operations wright?Last edited by allexif; 04-22-2012 at 05:30 PM.
- 04-23-2012, 04:57 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 02:37 AM -
Can't see what is wrong
By SirJabalot in forum New To JavaReplies: 7Last Post: 05-05-2011, 04:43 AM -
does anyone know what is wrong with this?
By blueduiker in forum New To JavaReplies: 5Last Post: 03-22-2010, 11:25 AM -
So, what am I doing wrong?
By Charles_Smith in forum New To JavaReplies: 0Last Post: 10-29-2008, 03:50 PM -
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 02:16 PM
Bookmarks