Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Stumped!

Thread view: 
aver24@hotmail.com - 08 Dec 2005 02:45 GMT
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);
Chris Smith - 08 Dec 2005 03:07 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Luke Webber - 08 Dec 2005 03:12 GMT
> 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
Lee Weiner - 08 Dec 2005 03:15 GMT
>I realize that this program is for beginners. In any case, I can't find
>the problem. Here it is:
[quoted text clipped - 4 lines]
>     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
Thomas Hawtin - 08 Dec 2005 03:25 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

IchBin - 08 Dec 2005 04:03 GMT
[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;
}
Signature


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-)

Roedy Green - 08 Dec 2005 05:20 GMT
>  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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.