Results 1 to 6 of 6
  1. #1
    allexif is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Question 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;
    	}
    }
    Thank you in advance!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default 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

  3. #3
    allexif is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default 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);
    	}
    }

  4. #4
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default 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;
    }

  5. #5
    allexif is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Re: I don't find where I did wrong ...

    Quote Originally Posted by Danieldcc View Post
    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;
    }
    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 04:30 PM.

  6. #6
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default 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;
    }

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Can't see what is wrong
    By SirJabalot in forum New To Java
    Replies: 7
    Last Post: 05-05-2011, 03:43 AM
  3. does anyone know what is wrong with this?
    By blueduiker in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 10:25 AM
  4. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 02:50 PM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:16 PM

Posting Permissions

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