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 / February 2006

Tip: Looking for answers? Try searching our database.

Various Interest Rates

Thread view: 
shannon - 31 Jan 2006 10:13 GMT
hi all,

Working on this compound Interest Program and want to repeat the
program for  10 different interest rates, I think i should be using a
loop but not quite sure how to go about it. The following is what i
have so far.

Cheers
Shannon

____________________________________________________________________

//Java Packages

import java.text.NumberFormat; // class for numeric formatting
import java.util.Locale; // class for country-specific information

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class CompoundInterestProgram {

   public static void main( String args[] )
   {

    double amount; //amount on deposit at end of each year
    double principal = 20000.0; // initial amount before interest

    //create NumberFormat for currency in US dollar format
    NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US
);

    // create JTextArea to display output
    JTextArea outputTextArea = new JTextArea();

    // set first line of text in outputTextArea
    outputTextArea.setText( "Year\tAmount on deposit\n" );

    // calculate amount of deposit for each of ten years
    for ( int year = 1; year <= 10; year ++ ) {

       //
       for ( int rate = 1; rate <= 10; rate ++)

          // calculate new amount for specified year
          amount = principal * Math.pow( 1.0 + rate, year );

          // append one line of text to outputTextAea
         outputTextArea.append( year + "\t" + moneyFormat.format( amount
) + "\n" );

    }// end for

      // display results

      JOptionPane.showMessageDialog( null, outputTextArea, "Compound
Interest", JOptionPane.INFORMATION_MESSAGE );

      System.exit ( 0 ); // terminates the application
       
    } // end main
} // end class Interest
mikm - 31 Jan 2006 13:10 GMT
You could set up an array that contains all the interest rates then
iterate through that.
shannon - 31 Jan 2006 21:49 GMT
The for loop below seems to be working for me, at the moment. I have
the interest rates at full numbers eg 5, 6 etc. but i need to change
them to 0.05, 0.06 but not sure how to go about this. Think it might be
something to do with precision?

// calculate amount of deposit for each of ten years
    for ( int year = 1; year <= 10; year ++ ) {

          //calculate interest rates
       for ( int rate = 5; rate <=10; rate ++) {

           // calculate new amount for specified year
          amount = principal * Math.pow( 1.0 + rate, year );

          // append one line of text to outputTextAea
         outputTextArea.append( year + "\t" + moneyFormat.format( amount
) + "\t" + moneyFormat.format( amount )
         + "\t" + moneyFormat.format( amount ) + "\t" +
moneyFormat.format( amount ) + "\t" + moneyFormat.format( amount ) +
          "\t" + moneyFormat.format( amount ) + "\n" );
     
        }// end for
    }  //end for
Rajah - 31 Jan 2006 22:14 GMT
Shannon, it's not so much the precision, but rather the declaration
that you have for rate.

Since you declared it as (int rate = 5; rate <= 10; rate++) rate can
only be an integer.

Without giving you the answer, could you think of another way to
declare rate so that it might hold fractional parts?

By the way, the "rate <= 10" will end the loop with rate = 11. I don't
know if you want to write this as "rate < 10" instead.

> The for loop below seems to be working for me, at the moment. I have
> the interest rates at full numbers eg 5, 6 etc. but i need to change
> them to 0.05, 0.06 but not sure how to go about this. Think it might be
> something to do with precision?
shannon - 01 Feb 2006 09:02 GMT
Should I use double instead of int?
(double rate =5; rate > 10; rate ++)

> Shannon, it's not so much the precision, but rather the declaration
> that you have for rate.
[quoted text clipped - 12 lines]
> > them to 0.05, 0.06 but not sure how to go about this. Think it might be
> > something to do with precision?
Steve Bosman - 01 Feb 2006 09:39 GMT
> Should I use double instead of int?
> (double rate =5; rate > 10; rate ++)

I'd strongly recommend never using doubles in loops, for this problem
it isn't too much of an issue, but eventually the practice will lead
you into trouble.

Steve
Steve Bosman - 01 Feb 2006 09:37 GMT
> By the way, the "rate <= 10" will end the loop with rate = 11. I don't
> know if you want to write this as "rate < 10" instead.

No it won't - try it

Steve
Rajah - 01 Feb 2006 21:47 GMT
You're right, Steve. I stand corrected.

Shannon, if you wrote the for loop as
 for ( double rate = 5; rate <=10; rate ++)
then it would usually go through the loop the last time with rate =
10.0.

Steve's warning probably refers to the notion that if it looped a large
number of times or using a very small increment (like .01) then you
might end at 10.0, or you might end at something lower.

I tried this code:
 for (double rate = 5; rate <= 10; rate+= .01)
System.out.println("Value: " + rate );
and the last time through, rate was 9.999999999999893.

If you're going from .05 to .10 by .01, you probably won't face this
kind of roundoff error. Your program results would look correct,
especially with the moneyFormat function rounding things off.

(Why is there roundoff error? In part, it is because numbers that look
even in our decimal system,  like 0.1, cannot be represented exactly in
binary, because they have repeating digits.)

In contrast with doubles, ints are represented exactly. If you and your
prof don't mind being off by a tiny fraction, or if your moneyFormat
function is going to round off correctly, then your for loop with the
double will work just fine.

Best wishes.
Steve Bosman - 01 Feb 2006 09:35 GMT
> The for loop below seems to be working for me, at the moment. I have
> the interest rates at full numbers eg 5, 6 etc. but i need to change
[quoted text clipped - 9 lines]
>            // calculate new amount for specified year
>           amount = principal * Math.pow( 1.0 + rate, year );

This line is going to give you problems, ask yourself the question what
value does 1.0 + rate give me for each value of rate and what value do
I want it to give me?

>           // append one line of text to outputTextAea
>          outputTextArea.append( year + "\t" + moneyFormat.format( amount
[quoted text clipped - 5 lines]
>         }// end for
>      }  //end for


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



©2009 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.