JAVA Assignment. The assignment is to create a program to predict the amount of
ID: 670808 • Letter: J
Question
JAVA Assignment.
The assignment is to 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 is the average expected rate of 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 test your program before you have me check it. 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!
Explanation / Answer
import java.util.Scanner;
public class IRAPrediction {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int years = 0;
System.out.println("Enter Number of years until you retire (Range: 1 to 70 ):");
while(true) {
years = scan.nextInt();
if(years >= 1 && years <= 70)
break;
else
System.out.println("Enter valid no of years ( 1 to 70)");
}
float rateOfInterest = 0;
System.out.println("Expected interest rate as a percent:");
while(true) {
rateOfInterest = scan.nextFloat();
if(rateOfInterest >= 0 && rateOfInterest <= 9.9)
break;
else if(rateOfInterest > 9.9) {
System.out.print("Are you really expecting to earn interest of "+rateOfInterest);
System.out.println(" . Enter Y for Yes or N for No");
String str = scan.next();
if(str.equalsIgnoreCase("y"))
break;
else
System.out.println("Enter valid rate of interest");
}
else
System.out.println("Enter valid rate of interest");
}
float initialAmt = 0;
System.out.println("Enter The initial amount in the IRA:");
while(true) {
initialAmt = scan.nextFloat();
if(initialAmt > 0)
break;
else
System.out.println("Enter valid amount");
}
float yearlyInput = 0;
System.out.println("The amount expected to be added to the IRA each year( $0 to $2500):");
while(true) {
yearlyInput = scan.nextFloat();
if(yearlyInput >= 0 && yearlyInput <= 2500)
break;
else
System.out.println("Enter valid amount");
}
//calculate value
float value = 0;
for(int i = 1; i <= years; i++ ) {
float rate = rateOfInterest / 100;
value = value + (value)* rate+ yearlyInput;
if(i % 5 == 0) {
System.out.println(" Value accumulated after "+i+" years = $"+value);
}
}
System.out.println(" Total Value accumulated after "+years+" years = $"+value);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.