***Please comment what each line does if possible*** Write a program named Compu
ID: 3884302 • Letter: #
Question
***Please comment what each line does if possible***
Write a program named ComputeAge that determine the user’s age in years, months, and days. The user enters his or her birthday, and the program determines how old the person is on the current data. The following example shows what the user will see on the screen, assuming that the current date is September 1, 2015:
The words “years”, “months” and “days” must be printed in singular from when appropriate.
You can assume that the user always enters a correct number (no need to worry that the user’s input is not a number). Nevertheless, your program must detect the following input errors:
-Input number is too small. The year cannot be less than 1800. The month and day cannot be less than 1.
-Input number is too large. The birth year cannot exceed 2015. The birth month cannot exceed 12. The birth day cannot exceed the number of days in the specified birth month. (Note that the number of days may depend on the birth year as well.) We will assume that the current day is 9/1/2015. So if the user was born in 2015, then the birth month cannot exceed 9, which is the current month (September). And the day of birth cannot exceed the current day (September 1, 2015).
If any of these errors occur, the program must print an error message and ask the user to reenter the offending input:
Current date is: 9/1/2015 Enter the year you were born 1980 Enter the month you were born (1-12) 7 Enter the day you were born: 26 You are 35 years, 1 month, and 6 days oldExplanation / Answer
package chegg;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class ComputeAge {
private static boolean checkLeapYear(int year) {
boolean leap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
leap = true;
else
leap = false;
} else
leap = true;
} else
leap = false;
if (leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
return leap;
}
private static AgeBean calculateAge(Date birthDate) {
int calculatedYears = 0;
int calculatedMonths = 0;
int calculatedDays = 0;
Calendar birthDayCalInstance = Calendar.getInstance();
birthDayCalInstance.setTimeInMillis(birthDate.getTime());
long currentTime = System.currentTimeMillis();
Calendar currentCalInstance = Calendar.getInstance();
currentCalInstance.setTimeInMillis(currentTime);
// Get difference between years
calculatedYears = currentCalInstance.get(Calendar.YEAR)
- birthDayCalInstance.get(Calendar.YEAR);
int currMonth = currentCalInstance.get(Calendar.MONTH) + 1;
int birthMonth = birthDayCalInstance.get(Calendar.MONTH) + 1;
// Get difference between months
calculatedMonths = currMonth - birthMonth;
// if month difference is in negative then reduce years by one and
// calculate the number of months.
if (calculatedMonths < 0) {
calculatedYears--;
calculatedMonths = 12 - birthMonth + currMonth;
if (currentCalInstance.get(Calendar.DATE) < birthDayCalInstance
.get(Calendar.DATE))
calculatedMonths--;
} else if (calculatedMonths == 0
&& currentCalInstance.get(Calendar.DATE) < birthDayCalInstance
.get(Calendar.DATE)) {
calculatedYears--;
calculatedMonths = 11;
}
// Calculate the days
if (currentCalInstance.get(Calendar.DATE) > birthDayCalInstance
.get(Calendar.DATE))
calculatedDays = currentCalInstance.get(Calendar.DATE)
- birthDayCalInstance.get(Calendar.DATE);
else if (currentCalInstance.get(Calendar.DATE) < birthDayCalInstance
.get(Calendar.DATE)) {
int today = currentCalInstance.get(Calendar.DAY_OF_MONTH);
currentCalInstance.add(Calendar.MONTH, -1);
calculatedDays = currentCalInstance
.getActualMaximum(Calendar.DAY_OF_MONTH)
- birthDayCalInstance.get(Calendar.DAY_OF_MONTH) + today;
} else {
calculatedDays = 0;
if (calculatedMonths == 12) {
calculatedYears++;
calculatedMonths = 0;
}
}
return new AgeBean(calculatedDays, calculatedMonths, calculatedYears);
}
public static void main(String[] args) throws ParseException {
Scanner scanner = null;
String year = null;
String month = null;
String day = null;
boolean isYear = false;
boolean isMonth = false;
boolean isDay = false;
try {
while (!isYear) {
scanner = new Scanner(System.in);
System.out.print("Enter The Year you were born : ");
year = scanner.next();
if (Integer.parseInt(year) < 1800
|| Integer.parseInt(year) > 2015) {
System.out.println("Input must be atleast 1800.");
} else {
isYear = true;
}
}
while (isYear && !isMonth) {
scanner = new Scanner(System.in);
System.out.print("Enter The Month you were born : ");
month = scanner.next();
if (Integer.parseInt(month) < 1) {
System.out.println("Input must be from 1 to 12.");
} else {
isMonth = true;
}
}
while (isMonth && !isDay) {
scanner = new Scanner(System.in);
System.out.print("Enter The Day you were born : ");
day = scanner.next();
if (Integer.parseInt(month) != 2) {
if (Integer.parseInt(day) < 1 && Integer.parseInt(day) < 31) {
System.out
.println("Input must be not less then 1 or not more than 31.");
} else {
isDay = true;
}
} else {
if (checkLeapYear(Integer.parseInt(year))) {
if (Integer.parseInt(day) > 1
&& Integer.parseInt(day) <= 29) {
isDay = true;
} else {
System.out
.println("Input must be not less then 1 or not more than 29.");
}
} else {
if (Integer.parseInt(day) > 1
&& Integer.parseInt(day) <= 28) {
isDay = true;
} else {
System.out
.println("Input must be not less then 1 or not more than 28.");
}
}
}
}
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date birthDate = sdf.parse(day + "/" + month + "/" + year);
AgeBean age = ComputeAge.calculateAge(birthDate);
System.out.println(age);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}
class AgeBean {
private int years;
private int months;
private int days;
private AgeBean() {
}
public AgeBean(int days, int months, int years) {
this.days = days;
this.months = months;
this.years = years;
}
public int getDays() {
return this.days;
}
public int getMonths() {
return this.months;
}
public int getYears() {
return this.years;
}
@Override
public String toString() {
return years + " Years, " + months + " Months, " + days + " Days";
}
}
Output
---------
Run 1 : Non Leap Year case
Enter The Year you were born : 1700
Input must be atleast 1800.
Enter The Year you were born : 1984
Enter The Month you were born : 2
Enter The Day you were born : 31
Input must be not less then 1 or not more than 28.
Enter The Day you were born : 23
33 Years, 7 Months, 13 Days
Run 2: Leap Year case
----------
Enter The Year you were born : 2000
Enter The Month you were born : 2
Enter The Day you were born : 31
2000 is a leap year.
Input must be not less then 1 or not more than 29.
Enter The Day you were born : 29
2000 is a leap year.
17 Years, 7 Months, 7 Days
Description
----------------
1. We have main method in ComputeAge class. we need to run the same to check the program output.
2. Program will continuous promot to user to enter the correct year , month and day value till all are correct.
3. Once the input validated, program will calculate the total age of user.
4. Added one helper method to check whether year is leap year or not in case of feb month.
please let me know if you need more help on this.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.