Write an application that calculates the amount of money and earned on an invest
ID: 3629716 • Letter: W
Question
Write an application that calculates the amount of money and earned on an investment, based on an 8% annual return. Prompt the user to enter an investment amount and the number of years for the investment. Display an error message if the user enters 0 for either value of investment or years; otherwise, display the total amount(balance) for each year of the investment. Also display an error message if the value entered of the interest rate is 0. Save the file as Investment.java. All of the logic cannot be in the main method, as well as the program should run as follows:Explanation / Answer
I used Netbeans to compile this, so omit the "package Investment" part if you plan to use on another compiler. And please rate! Ended up spending 2 hours on this because I just found out that I sucked at joptionpane.
package investment;
import javax.swing.JOptionPane;
import java.text.*;
public class Investment {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter investment amount");
double invest=Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter number of years");
int years= Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter intrest rate (as a decimal");
double interest=Double.parseDouble(input);
calculate(invest,years,interest);
}
public static void calculate(double in, int yrs, double intre)
{
DecimalFormat df= new DecimalFormat("###,###.00");
double sum=in;
String output="Your Investment at "+intre*100+"% = ";
for (int i=1; i<=yrs; i++)
{
sum+=sum*intre;
output+="After year "+i+" $"+df.format(sum)+" ";
}
JOptionPane.showMessageDialog(null,output);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.