Results 1 to 8 of 8
- 09-11-2012, 05:04 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Java Calculate Future Investment Amount
I do not know where I'm going wrong here. Pleaqse take a look and let me know what suggestions you have. Thank you. #So Frustrated
import java.util.Scanner;
public class FutureInvestment {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
//Obtain Investment Amount a
System.out.println("Enter investment amount: ");
double a = input.nextDouble();
//Obtain Interest Rate r
System.out.println("Enter interest rate: ");
double r = input.nextDouble();
//Obtain number of Years y
System.out.println("Enter number of years: ");
double y = input.nextDouble();
//Compute Future Investment Amount f
double f = a * Math.pow(1 + r, (y * 12));
//Display result
System.out.println("Accumulated value is " + f);
}
}
OUTPUT IS...........................
Enter investment amount:
1000
Enter interest rate:
.0425
Enter number of years:
1
Accumulated value is 1647.8313602236476
- 09-11-2012, 05:08 AM #2
Re: Java Calculate Future Investment Amount
We have no idea what is wrong unless you tell us. Are you getting incorrect output? Then you should post what you expect the output should be. If it is wrong then most likely your equation is incorrect. Where did you get it? Are you sure you entered it into your code correctly?
- 09-11-2012, 05:11 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: Java Calculate Future Investment Amount
Expected Output is:
Enter investment amount: 1000
Enter annual interest rate: 4.25
Enter number of years: 1
Accumulated value is 1043.34
Actual OUTPUT IS...........................
Enter investment amount: 1000
Enter interest rate: .0425
Enter number of years: 1
Accumulated value is 1647.8313602236476
- 09-11-2012, 05:32 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Re: Java Calculate Future Investment Amount
There is a difference between 4.25 and .0425 , please correct your input data and validate the output. If the equation you are using is correct then you should get the same output.
- 09-11-2012, 05:44 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: Java Calculate Future Investment Amount
Enter investment amount:
1000
Enter interest rate:
4.25
Enter number of years:
1
Accumulated value is 4.384414858452464E11
The r must be input .01...etc
- 09-11-2012, 06:45 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
How do I do a Hard Truncate the output in this Java code after the second decimal?
Please help Me. My instructions are at the bottom.
import java.util.Scanner;
public class FutureInvestment {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
//Obtain Investment Amount a
System.out.println("Enter investment amount: ");
double a = input.nextDouble();
//Obtain Monthly Interest Rate r
System.out.println("Enter interest rate: ");
double r = input.nextDouble();
//Obtain number of Years y
System.out.println("Enter number of years: ");
double y = input.nextDouble();
double f = a * Math.pow(1 + (r/12),(y *12));
//Display result
System.out.println("Accumulated value is " + f);
}
}
OUTPUT IS
Enter investment amount: 1000
Enter interest rate: .0325
Enter number of years: 1
Accumulated value is 1032.9885118105894
I want it to say 1032.98
- 09-11-2012, 07:10 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: How do I do a Hard Truncate the output in this Java code after the second decimal
Have a read of the options discussed in Oracle's Tutorial: Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
This sort of rounding where you simply truncate (== throw away insignificant digits) can be done using a RoundingMode.Accumulated value is 1032.9885118105894
I want it to say 1032.98
In this context "DOWN" means "towards zero". Notice that pi is actually closer to 3.142 than it is to 3.141.Java Code:import java.math.RoundingMode; import java.text.DecimalFormat; public class Test { public static void main(String[] args) { DecimalFormat fmt = new DecimalFormat(".000"); fmt.setRoundingMode(RoundingMode.DOWN); // prints 3.141 System.out.println(fmt.format(Math.PI)); } }
- 09-11-2012, 08:42 AM #8
Re: Java Calculate Future Investment Amount
Don't start a new thread for what is essentially a follow-on question, and moreover one which you already asked in the first thread. I've merged the two threads.
Please go through the following:
Forum Rules
Guide For New Members
BB Code List - Java Programming Forum
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Associate Conversion Program - European Investment Bank (Java) - Automated Trading
By ssinclair in forum Jobs OfferedReplies: 0Last Post: 05-08-2012, 01:03 PM -
Java Developer Positions New York City (Investment Banking Domain)
By Dave IRIS in forum Jobs OfferedReplies: 0Last Post: 12-28-2010, 05:08 PM -
Future of Java
By Skynet.Boy in forum New To JavaReplies: 0Last Post: 03-06-2010, 06:16 PM -
Java is 15 years old - what is the Future?
By mikeVB in forum Jobs DiscussionReplies: 1Last Post: 03-04-2010, 04:21 AM -
Future of Java ??
By DevzAbhi in forum Java SoftwareReplies: 16Last Post: 02-26-2009, 10:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks