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.
Write program that prompts the user to enter a person's date of birth in numeric form...
Write 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. Similar conventions for the invalid values of month and year. (Note that your program must handle...
Write a checkbook balancing program. The program will read in, from the console, the following for...
Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There should be three...
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.
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
Write a c++ program that inputs a time from the console. The time should be in...
Write a c++ program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits. Your program should then convert the time into a four-digit military time based on a 24-hour clock. Code: #include <iostream> #include <iomanip> #include <string> using namespace std; int main() { string input; cout << "Enter (HH:MM XX) format time: "; getline(cin, input); int h, m; bool am; int len =...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT