Java Programming Create a program to predict the amount of money you will have i
ID: 640788 • Letter: J
Question
Java Programming
Create a program to predict the amount of money you will have in your IRA when you retire.
Inputs are:
Number of years until you retire, it must be a whole number (integer) in the range 1 to 70. Any number outside of this range is to be rejected and the user will be required to enter the value again, this rejection and repeat of the input request must be done until a valid number is input.
Expected interest rate as a percent (this the average expected earnings per year until retirement is reached). The number must be 0 or greater, any other value will be rejected and as above, the user must be polled until a valid number is input. If the number is greater than 9.9%, the user must be asked if he/she really expects to earn this much, if the user replies in the affirmative, the number is to be used, otherwise the user will be asked to input the value again and the above checks will be made again.
The initial amount in the IRA. This is a decimal number and may be any non-negative number. If a negative number is entered, the value is to be rejected and the user will be required to enter an acceptable value. Again, loop until a valid value is input.
The amount expected to be added to the IRA each year. This is a decimal number in the range of 0 to $2,500. The program must loop asking for this value until a valid value is entered.
After the data is input and validated, the program is to loop printing out the value of the IRA every 5 years and the value at the end of the time entered as input #1. The value is to be computed in a loop where the value each year is determined by:
value = value + (value)*rate + yearlyInput
Remember the rate in the above equation is the rate entered in #2 above divided by 100, the decimal equivalent of the percentage.
Make certain to check with years that are not multiples of 5 so that you can see the amounts at 5 year intervals as well as the final amount. You can count on me entering bad values to be certain they are rejected. Also make sure that the amounts that your program compute are valid!
Write a brief description of the program are at the top of the code as comments. You must use proper blocking in the code, good variable names, and have meaningful comments in the code as well having a program the works properly to receive full credit.
Explanation / Answer
import java.util.Scanner;
public class IraValues {
public static void main(String agrs[]) {
Scanner in = new Scanner(System.in);
double value, yearlyInput, rate;
int numYears;
int count = 0;
do {
if(count != 0){
System.out.println("Bad Input!");
}
count = 1;
System.out.print("Number of years until you retire: ");
numYears = in.nextInt();
} while (numYears < 1 || numYears > 70);
while(true){
System.out.print("Expected Interest rate as a percent: ");
rate = in.nextDouble();
if(rate < 0){
System.out.println("Enter Your value again");
}
else if(rate > 9.9){
System.out.print("Do you really expect to earn this much? ");
System.out.print("if yes Enter affirmattive: ");
String s = in.nextLine();
s = in.nextLine();
System.out.println(s);
if(s.equalsIgnoreCase("affirmative")){
break;
}
else{
System.out.println("Enter Your value again");
}
}
else{
break;
}
}
while(true){
System.out.print("Enter Initial amount in the IRA: ");
value = in.nextDouble();
if(value < 0){
System.out.println("Enter Your value again");
}
else{
break;
}
}
while(true){
System.out.print("The amount expected to be added to the IRA each year. ");
yearlyInput = in.nextDouble();
if(yearlyInput < 0 || yearlyInput > 2500){
System.out.println("Enter Your value again");
}
else{
break;
}
}
for(int i = 0; i < numYears; ++i){
value = value + (value * (rate / 100)) + yearlyInput;
if((i + 1) % 5 == 0){
System.out.println("Value at the end of " + (i + 1) + " Years is " + value);
}
}
System.out.println("Final value at the end of " + numYears + " is " + value);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.