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:
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;
}
}
Thank you in advance!
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
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:
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);
}
}
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....
Code:
private void withdraw(double balance) {
this.balance = balance;
}
Re: I don't find where I did wrong ...
Quote:
Originally Posted by
Danieldcc
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....
Code:
private void withdraw(double balance) {
this.balance = balance;
}
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?
Re: I don't find where I did wrong ...
true, hung over so not sure what i was thinking
private void withdraw(double amount) {
balance = balance - amount;
}