Gets the current year using the following code: Note that you must import the Lo
ID: 3678608 • Letter: G
Question
Gets the current year using the following code:
Note that you must import the LocalDate class:
import java.time.LocalDate;
Receives as input an age (as an int).
Receives as input another year.
Calculate the age in the year retrieved in the previous step.
Returns the age in this year.
For example, suppose the current year is 2015 and the method receives an argument of 20 for the age and 2020 for the other year. Your method should return 25, that is, the person will be 25-years-old in the year 2020.
HINT: The method argument can be any year in the past. For instance, in the above example, suppose the year argument is 2010 instead of 2020. In this case, your method should return 15.
HINT: Valid ages are non-negative.
Explanation / Answer
Program to calculate the age based on the year given as input by the user:
this is program Based on java 7,means LocalDate.now().getYear(); this class is introduced in java 8.so it will not work in 7.
CalAge.java ( Based on java 7)
package org.students;
import java.util.Calendar;
import java.util.Scanner;
public class CalAge {
static int diff=0,cage=0;
// This method will calculate the age depending on the year which entered as input by the user.
public static int calculateAge(int currentyear,int age,int year1)
{
if(currentyear>year1)
{
diff=currentyear-year1;
cage=age-diff;
}
else
{
diff=year1-currentyear;
cage=age+diff;
}
return cage;
}
public static void main(String[] args)
{
//Declared and intitialzed the instance variables.
int currentyear=0;
int age=0;
//Here I used Calender class to get the current year.
currentyear = Calendar.getInstance().get(Calendar.YEAR);
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.print("Enter your Present age :");
//Receives the year as input from the user.
age=sc.nextInt();
if(age<0)
{
System.out.print("Enter valid age :");
continue;
}
else
break;
}//end of while(true)
System.out.print("Enter an year to calculate your age at that time :");
int year1=sc.nextInt();
int resultage=calculateAge(currentyear,age,year1);
System.out.println("The Age in the year "+year1+" is :"+resultage);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
CalAge.java (Based on java 8)
package org.students;
import java.util.Calendar;
import java.util.Scanner;
public class CalAge
{
static int diff=0,cage=0;
public static int calculateAge(int currentyear,int age,int year1)
{
if(currentyear>year1)
{
diff=currentyear-year1;
cage=age-diff;
}
else
{
diff=year1-currentyear;
cage=age+diff;
}
return cage;
}
public static void main(String[] args)
{
int currentyear=0;
int age=0;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.print("Enter your Present age :");
age=sc.nextInt();
if(age<0)
{
System.out.print("Enter valid age :");
continue;
}
else
break;
}//end of while(true)
System.out.print("Enter an year to calculate your age at that time :");
int year1=sc.nextInt();
int resultage=calculateAge(currentyear,age,year1);
System.out.println("The Age in the year "+year1+" is :"+resultage);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.