Question

In: Computer Science

In this exercise, you’ll write a program that accepts a person’s birth date from the console...

In this exercise, you’ll write a program that accepts a person’s birth date from the console and displays the person’s age in years. To make that easier to do, we’ll give you a class that contains the code for accepting the birth date. The console output for the program should look something like this:
f Welcome to the age calculator. j
Enter the month you were born (1 to 12) : 5 Enter the day of the month you were born: 16 Enter the year you were born (four digits): 1959
Your birth date is May 16, 1959 Today's date is Feb 9, 2012 Your age is: 52
1. Open the project named chl3_ex2_AgeCalculator that’s in the ex_starts directory. Then, review the code in the AgeCalculatorApp class.
2. Add code to this class that gets the current date and then uses the current year to validate the birth year the user enters. The user should not be allowed to enter a year after the current year or more than 110 years before the current year.
3. Add code to create, format, and print the user’s birth date and to format and print the current date.
4. Add code to calculate and print the user’s age.
5. Test this project with a variety of dates to be sure it works.

Solutions

Expert Solution

CODE:-

// Importing Modules
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Scanner;

public class AgeCalculatorApp {

public static void main(String[] args) {
  
Date d = new Date(); //Date Object
  
Scanner in = new Scanner(System.in);
  
int month,day,year;
String months[] = {"Jan","Feb","Mar","Apr","May","June","July",
"Aug","Sept","Oct","Nov","Dec"};
  
//Taking Inputs From User
System.out.print("Enter the month you were born (1 to 12) : ");
month = in.nextInt();
  
System.out.print("Enter the day of the month you were born: ");
day = in.nextInt();
  
int currentyear = d.getYear()+1900;
year = currentyear+1;
  
//Year Conditions
while(year>currentyear) // Year > Currentyear Condition
{
System.out.print("Enter the year you were born (four digits): ");
year = in.nextInt();
if(currentyear-110 > year) // More Than 110 Condition
year = currentyear+1;
}
  
// Present Date Calculations
String pmonth = months[d.getMonth()];
int pday = d.getDate();
int pyear = d.getYear()+1900;
  
//Extracting Users Month Name
String umonth = months[month-1];
  
//Age Calculation
LocalDate user_date = LocalDate.of(year,month,day);
LocalDate present_date = LocalDate.now();
long age = ChronoUnit.YEARS.between(user_date, present_date);
  
//Printing Output
System.out.println("Welcome to the age calculator.j");
System.out.println("Your birth date is "+umonth+" "+day+", "+year);
System.out.println("Today's date is "+pmonth+" "+pday+", "+pyear);
System.out.println("Your age is: "+age);
}
}

Code Screenshots:-

Sample Outputs:-

Here I Attached Code And Sample Output

For Indentation Please Go Through Screenshots

For Any Queries Please Comment

Please Do Like


Related Solutions

Write a program that prompts the user to enter a person’s date of birth in numeric...
Write a program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Follow similar conventions for the invalid values of month and year. (Note that your program...
Write a Java program that will add the digits of a person’s birth date to obtain...
Write a Java program that will add the digits of a person’s birth date to obtain a single digit to generate a numerology number. Write one separate method for each of the following tasks (it goes w/o saying that you will have a main() method along with these): date validating date crunching First: Get a Date Numerology has been used since ancient times to shed light on relationships, health, and global events. Each element in a birth date is believed...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output should be: ['34', '67', '55', '33', '12', '98'] and ('34',67', '55', '33', '12', '98').
Write a program which accepts a sequence of comma-separated numbers from console and generate a list...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output should be: ['34', '67', '55', '33', '12', '98'] and ('34',67', '55', '33', '12', '98'). Input can be comma separated in one line.
Design a program using a flowchart or pseudocode that accepts a person’s loyalty card number and...
Design a program using a flowchart or pseudocode that accepts a person’s loyalty card number and amount spent in the store last month. Display the data only if the customer is a high spender – a person who spent more than $1000 in the store. A program that accepts customer info continuously until a sentinel value is entered and displays a list of high spenders.
Question 2. Write a MARIE program that accepts an integer from the user, and if it...
Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and...
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT