- Dec 6, 2001
- 2,171
- 1
- 0
Hey Everyone, Need some help. I have to edit this code I've written to work with this following instructions:
Instead of automatically calculating when you will reach full retirement age, change the program to allow the user to input the age at which they would like to retire, calculate the year they will reach that age based on their current age, and then display to the monitor when that will be. HINT: Prompt user for year born, prompt user for age they wish to retire, add year born to wish age for retirement. Display result to screen. Make sure you control for any unrealistic conditions (such as if the desired age of retirement is 20 and they are already 21 or older, you should inform them that they have passed their desired retirement year (but do tell them when it was)).
How can I do this? Can I just use the input and use a subratiction of a display println from the 2 variables? I understand the concepts behind it but not quite sure of the coding, any help is VERY appreciated THANK YOU!
-Chris
import java.io.*;
public class Retirement {
//Written by Chris C
//Assignment 4
//This is a program that obtains a name and birth year, and estimates year of retirement
//with input from the keyboard
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void main(String arg[])throws IOException {
String name=null;
int birthYear=0;
name=getName();
birthYear=getBirthYear();
display(name+" will be eligible for full retirement in "+getRetirementYear(birthYear));
}//end main
public static String getName()throws IOException{
prompt("Please enter your name: ");
return keyboard.readLine();
}//close getName
public static int getBirthYear(){
int birthYear=0;
prompt("Please enter the full year you were born (e.g., 1986, not 86: )");
try {
String yearString=keyboard.readLine();
if((yearString.length()<4)|(yearString.length()>4)){
display("Year must be four digits! Sorry!");
exitProgram();
}
birthYear=Integer.parseInt(yearString);
if (birthYear<1922){
display("You have already passed your retirement year!");
exitProgram();
}//close if 1922
if(birthYear>2002){
display("You have not been born yet!");
exitProgram();
}
}//close try
catch(Exception n){
display("You need to enter an integer");
getBirthYear();
}//close catch
return birthYear;
}//close getBirthYear
public static void exitProgram(){
display("Exiting the program");
System.exit(0);
}
public static int getRetirementYear(int birthYear){
int fullRetirementAge = 67;
return birthYear+fullRetirementAge;
}//close getRetirementYear
public static void prompt(String n){
System.out.print(n);
}//close prompt
public static void display(String n){
System.out.println(n);
}//close display
}//close Retirement
Instead of automatically calculating when you will reach full retirement age, change the program to allow the user to input the age at which they would like to retire, calculate the year they will reach that age based on their current age, and then display to the monitor when that will be. HINT: Prompt user for year born, prompt user for age they wish to retire, add year born to wish age for retirement. Display result to screen. Make sure you control for any unrealistic conditions (such as if the desired age of retirement is 20 and they are already 21 or older, you should inform them that they have passed their desired retirement year (but do tell them when it was)).
How can I do this? Can I just use the input and use a subratiction of a display println from the 2 variables? I understand the concepts behind it but not quite sure of the coding, any help is VERY appreciated THANK YOU!
-Chris
import java.io.*;
public class Retirement {
//Written by Chris C
//Assignment 4
//This is a program that obtains a name and birth year, and estimates year of retirement
//with input from the keyboard
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void main(String arg[])throws IOException {
String name=null;
int birthYear=0;
name=getName();
birthYear=getBirthYear();
display(name+" will be eligible for full retirement in "+getRetirementYear(birthYear));
}//end main
public static String getName()throws IOException{
prompt("Please enter your name: ");
return keyboard.readLine();
}//close getName
public static int getBirthYear(){
int birthYear=0;
prompt("Please enter the full year you were born (e.g., 1986, not 86: )");
try {
String yearString=keyboard.readLine();
if((yearString.length()<4)|(yearString.length()>4)){
display("Year must be four digits! Sorry!");
exitProgram();
}
birthYear=Integer.parseInt(yearString);
if (birthYear<1922){
display("You have already passed your retirement year!");
exitProgram();
}//close if 1922
if(birthYear>2002){
display("You have not been born yet!");
exitProgram();
}
}//close try
catch(Exception n){
display("You need to enter an integer");
getBirthYear();
}//close catch
return birthYear;
}//close getBirthYear
public static void exitProgram(){
display("Exiting the program");
System.exit(0);
}
public static int getRetirementYear(int birthYear){
int fullRetirementAge = 67;
return birthYear+fullRetirementAge;
}//close getRetirementYear
public static void prompt(String n){
System.out.print(n);
}//close prompt
public static void display(String n){
System.out.println(n);
}//close display
}//close Retirement