In: Computer Science
how do i program this to Java:
Company X offers a 401k (Links to an external site.) retirement plan that allows you to save a percentage of your salary every year.
Your investment will grow using a flat interest rate that you will provide as input.
The goal is to compute how much money you will have for retirement after a specified number of years.
This is just a basic calculation. No loops yet!
Input
Read the following from input. Make sure you prompt for input.
Salary - your salary will remain the same for all of your years of employment. Read in as int.
Percentage of Salary you will save for retirement each year. Read in as double so 6% = .06.
Interest Rate - the rate at which your retirement plan grows over the duration. Read in as double so 5% interest rate = .05.
Employment Years - the number of years you will work at Company X. Read in as int.
For the interest formula, just use the below:
Total Savings = Years of Employment * Percentage of Salary you save * Salary * (1 + Interest Rate)
Output
Your input variables as well as the amount you will have saved in retirement.
Please prompt the user. Include a line that says please enter in your salary percentage saved interest rate and employment years separated by a space.
Sample Run
Enter input: salary savings_rate interest_rate
years_employed
> 70000 .10 .06 30
Salary: 70000
Savings rate:0.1
Interest rate:0.06
Years of Employment: 30
Retirement Savings: 222600.0
// Java program to calculate the retirement savings after a specified number of years for the given interest rate and percentage of salary saved
public class RetirementSavings {
public static void main(String[] args) {
double savings_rate, interest_rate
;
int salary, years_employed;
Scanner scan = new
Scanner(System.in);
// Input of salary, percentage of
salary saved, interest rate and years employed
System.out.print("Enter input:
salary savings_rate interest_rate years_employed : " );
salary = scan.nextInt(); // read
the salary
savings_rate = scan.nextDouble();
// read the percentage of salary saved
interest_rate = scan.nextDouble();
// read the interest rate
years_employed = scan.nextInt(); //
read the years employed
// calculate retirement
savings
double retirement_savings =
years_employed * savings_rate*salary * (1+interest_rate);
// output
System.out.println("Salary:
"+salary);
System.out.println("Savings rate:
"+savings_rate);
System.out.println("Interest rate:
"+interest_rate);
System.out.println("Years of
Employment: "+years_employed);
System.out.println("Retirement
Savings: "+retirement_savings);
scan.close();
}
}
//end of program
Output: