Welcome to Dream.In.Code
Become a Java Expert!

Join 149,534 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,512 people online right now. Registration is fast and FREE... Join Now!




Calculate Future Investment value java program

 
Reply to this topicStart new topic

Calculate Future Investment value java program

shivers20
27 May, 2007 - 08:26 PM
Post #1

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 14


My Contributions
I need to write a programthat reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula: futureInvestmentValue = investmentAmount x (1 + monthlyInterestRate)^numberOfYears*12. For example, if you entered amount 1000, annual interest rate 3.25%, and number of years 1, the future investment value is 1032.98. Hint: Use the Math.pow(a,b ) method to compute a raised to the power of b.

CODE


public class Investment
{
   public static void main (String []args)
   {
       double principal;
       double rate;
       int years;

       Scanner input = new Scanner (System.ini);
  
       System.out.print("Enter the intial investment amount:");
       double principal = input.nextDouble();

       System.out.print("Enter the annual interest rate:");
       double rate = input.nextDouble();
      
       System.out.print("Enter number of years:");
       int years = input.nextInt();

       double monthlyInterestRate;
       monthlyInterestRate = annualInterestRate/1200;
       double futureValue;
       futureValue = principal*Math.pow((1+monthlyInterestRate),number of years*12);

       System.out.println("The future value is " +futureValue);
       }
}

I didnt have a chance to use the school computer so I was unable to run the program and check for errors. Any help will be gladly appreciated. Thanks in advance.

This post has been edited by NickDMax: 27 May, 2007 - 08:49 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Calculate Future Investment Value Java Program
28 May, 2007 - 12:56 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, there were few errors which you can see by comparing your code with mine.
This is my code:

CODE
import java.util.Scanner;

public class Investment
{
   public static void main (String []args)
   {
       double principal;
       double rate;
       int years;

       Scanner input = new Scanner (System.in);
  
       System.out.print("Enter the intial investment amount:");
       principal = input.nextDouble();

       System.out.print("Enter the annual interest rate:");
       rate = input.nextDouble();
      
       System.out.print("Enter number of years:");
       years = input.nextInt();

       double monthlyInterestRate;
      
       monthlyInterestRate = rate/1200;
      
       double futureValue;
      
       futureValue = principal*Math.pow((1+monthlyInterestRate),years*12);

       System.out.println("The future value is " +futureValue);
       }
}



Now a little comment to your code:

You have to import java.util.Scanner; in order
Scanner input = new Scanner (System.in); to work.
Instead of System.ini there should be System.in

Once you declare the variables there is no need to do that again.
e.g.
QUOTE
int years;
int years = input.nextInt();

is not going to work.


you use
QUOTE
monthlyInterestRate = annualInterestRate/1200;

annualInterestRate is not declared, but rate is therefore rate should be instead of annualInterestRate.

And the last but not least :
QUOTE
futureValue = principal*Math.pow((1+monthlyInterestRate),number of years*12);

number of years is not declared variable, even if you declare it you'll get an error because of the name. So you could declare it like double numbers_of years. But in this case you don't need to because you already have years
so that line should be:
CODE
futureValue = principal*Math.pow((1+monthlyInterestRate),years*12);


As I said compare your code with mine.
User is offlineProfile CardPM
+Quote Post

Techboy52
RE: Calculate Future Investment Value Java Program
29 Jan, 2008 - 10:50 AM
Post #3

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 2



Thanked: 1 times
My Contributions
I have to do the same thing but after I get the information I have to print a table that displays future value for the years from 1 to 30, for example:

The amount invested: 10000
Annual interest rate: 9%
Years Future Value
1 1093.8
2 1196.41
3
....
29 13467.25
30 14730.57

This is my method I started but I am stuck after that??

public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years)


User is offlineProfile CardPM
+Quote Post

baavgai
RE: Calculate Future Investment Value Java Program
29 Jan, 2008 - 11:18 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,277



Thanked: 135 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Search this site some more, this has certainly been covered. We even have a definative blog post on it: http://www.dreamincode.net/forums/blog/mar...p?showentry=544

Basically, you need to know the starting amount, the payment, and the interest per payment. With those three things, you just keep eating the principal until it's gone. The real trick is finding a payment size that matches the time you want to be paying off the load.

Good luck.

User is offlineProfile CardPM
+Quote Post

Techboy52
RE: Calculate Future Investment Value Java Program
30 Jan, 2008 - 06:32 PM
Post #5

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 2



Thanked: 1 times
My Contributions
Hey thanks for showing me that it help me out a little but I am still getting stuck with my method. Its giving me a Java error "reached end of file while parsing". Basically I got the formula but I just need my method to take the formula and add first year total invesment and display it and add second year investment and display it and third year investment and display it and so on.... so this is what I got:

//so I just need my table to look like this table that displays future value for the //years from 1 to 30, for example:
//The amount invested: 10000
//Annual interest rate: 9%
//Years Future Value
//1 1093.8
//2 1196.41
//3
//....
//29 13467.25
//30 14730.57





import java.util.Scanner;

public class Investment
{

public static void main (String [] args)
{
double principal;
double rate;
int years;
double monthlyInterestRate;
double futureValue;

Scanner input=new Scanner(System.in);

System.out.print("Enter the intial investment amount:");
principal=input.nextDouble();

System.out.print("Enter the annual interest rate:");
rate=input.nextDouble();

System.out.print("Enter the number of years:");
years=input.nextInt();

//obtain monthly interest rate
monthlyInterestRate=rate/1200;

futureValue = principal*Math.pow((1+monthlyInterestRate),years*12);
futureValue=(int)(futureValue * 100)/100.0;

System.out.println("The amount invested:" +principal);
System.out.println("Annual interest rate:" +monthlyInterestRate);
// System.out.println("Years"+years"Future Value"+futureValue);

}


//Return the future value for the years from 1 to 30
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years)
{
double result;
int years=1;
for(years = 1; years<=30;years++)
{
if(years <= 30)
System.out.print(" "+years);
for (investmentAmount=result;result<=years;result++)
System.out.print(" "+result);

return result;

}

}

This post has been edited by Techboy52: 30 Jan, 2008 - 06:34 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month