Stumped!


A

aver24

I realize that this program is for beginners. In any case, I can't find
the problem. Here it is:
(It's supposed to print out alice: balance = 625; bob = 436; I don't
think it's adding the interest and I can't see why)


public class BankDemo {
public static void main(String[] args) {
BankAccount alice = new BankAccount(0);
BankAccount bob = new BankAccount(0);
alice.deposit(500);
bob.deposit(500);

System.out.println("Alice's balance: " + alice.getBalance());
System.out.println("Bob's balance: " + bob.getBalance());

alice.addInterest(5);
alice.transferFunds(bob, 100);
bob.addInterest(6);
bob.transferFunds(alice, 200);

System.out.println("Alice's balance: " + alice.getBalance());
System.out.println("Bob's balance: " + bob.getBalance());

System.out.println("Alice:\t" + alice);
System.out.println("Bob:\t" + bob);

}
}
class BankAccount {
public BankAccount(double initialBalance) {
double balance = initialBalance;
}

public void deposit(double amount) {
balance = balance + amount;
}

public void withdraw() {
balance = balance - 100;
}

public void withdraw(double amount) {
balance = balance - amount;
}

public double getBalance() {
return balance;
}

public String toString() {
return "BankAccount [balance=" + balance + "]";
}

private double balance = 0;

public void addInterest(double interestRate) {
double Interest = interestRate / 100;
}

public void setBalance(double amount) {
withdraw(getBalance());
deposit(amount);
}

public void transferFunds(BankAccount name, double amount) {
withdraw(amount);
name.deposit(amount);
}

}

// add 5% interest to alice's account balance
//alice.addInterest(5);

// move $100 from alice's account into bob's
// alice.transferFunds(bob, 100);

// add 6% interest to bob's account balance
//bob.addInterest(6);

// move $200 from bob's account into alice's
//bob.transferFunds(alice, 200);

//System.out.println("Alice:\t" + alice);
//System.out.println("Bob:\t" + bob);
 
Ad

Advertisements

C

Chris Smith

I realize that this program is for beginners. In any case, I can't find
the problem. Here it is:
(It's supposed to print out alice: balance = 625; bob = 436; I don't
think it's adding the interest and I can't see why)

I also think it's not adding the interest. That's because you don't
tell it to.
public void addInterest(double interestRate) {
double Interest = interestRate / 100;
}

That declares and initializes a local variable called "Interest"
(incidentally, local variables shouldn't be capitalized, so you ought to
name it "interest" instead), and then doesn't do anything with it.
Assuming this method is invoked once per year, you're missing is where
you multiply the current balance by the interest rate and add the result
to the balance.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

Luke Webber

I realize that this program is for beginners. In any case, I can't find
the problem. Here it is:
(It's supposed to print out alice: balance = 625; bob = 436; I don't
think it's adding the interest and I can't see why)
[SNIP]

public void addInterest(double interestRate) {
double Interest = interestRate / 100;

Add the following line here...

balance += Interest;

BTW, by convention, that should be "interest" rather than "Interest".


HTH,
Luke
 
L

Lee Weiner

I realize that this program is for beginners. In any case, I can't find
the problem. Here it is:
(It's supposed to print out alice: balance = 625; bob = 436; I don't
think it's adding the interest and I can't see why)


public void addInterest(double interestRate) {
double Interest = interestRate / 100;
}

You can't see why it's not adding the interest? This method calculates the
interest, but it never adds it to the balance.

Lee Weiner
lee AT leeweiner DOT org
 
T

Thomas Hawtin

I realize that this program is for beginners. In any case, I can't find
the problem. Here it is:

comp.lang.java.help would have been a better newsgroup, but as you are
here...
public BankAccount(double initialBalance) {
double balance = initialBalance;
}

You will need to store the balance in an instance variable, rather than
local, variable. You got lucky because your test sets the balance to zero.
public void withdraw(double amount) {
balance = balance - amount;
}

Or, more clearly, once you know Java:

balance -= amount;
private double balance = 0;

It's usual to put member variables at the top of classes, where everyone
knows where to find them.

I feel bound to denounce the use of floating point types in financial
calculations. At least use BigDecimal.
public void addInterest(double interestRate) {
double Interest = interestRate / 100;
}

You might want to add this to the balance (and drop the upper case I).

Tom Hawitn
 
I

IchBin

(e-mail address removed) wrote:

[snip]
public void addInterest(double interestRate) {
double Interest = interestRate / 100;
}

[snip]

Do what all suggestions already by others

plus.. to calculate percentage you want this..

public void addInterest(double interestRate) {
balance += (balance * interestRate)/100;
}
--


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Ad

Advertisements

R

Roedy Green

public void addInterest(double interestRate) {
double Interest = interestRate / 100;
}

You have a method naming problem. This calculates interest. It does
not add it. Further you made Interest a local variable. To be useful
the method should have either put it in an instance variable or
returned it.

Finally Interest is the name of a class. interest is the name of a
variable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top