Question

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 a leap year.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

class InvalidDayException extends RuntimeException {
   /**
   * @param aMsg
   */
   public InvalidDayException(String aMsg) {
       super(aMsg);
   }

}

class InvalidMonthException extends RuntimeException {
   /**
   * @param aMsg
   */
   public InvalidMonthException(String aMsg) {
       super(aMsg);
   }

}

public class DateTest {
   private static int getDays(int mm, int aYy) {
       int res = -1;
       switch (mm) {
       case 1:
           res = 31;
           break;
       case 2:
           if (isLeap(aYy))
               res = 29;
           else
               res = 28;
           break;
       case 3:
           res = 31;
           break;
       case 4:
           res = 30;
           break;
       case 5:
           res = 31;
           break;
       case 6:
           res = 30;
           break;
       case 7:
           res = 31;
           break;
       case 8:
           res = 31;
           break;
       case 9:
           res = 30;
           break;
       case 10:
           res = 31;
           break;
       case 11:
           res = 30;
           break;
       case 12:
           res = 31;
           break;

       }
       return res;
   }

   private static boolean isLeap(int year) {
       boolean leap = false;
       // if any year is divisable by 4 than there are many chances for leap
       // year except few
       if (year % 4 == 0) {
           // if it is divisable by 100 than it shoud also divisable by 400
           // like 2000 etc
           if (year % 100 == 0) {
               // year is divisible by 400, so the year is a leap year
               if (year % 400 == 0)
                   leap = true;
               else
                   leap = false;
           } else
               leap = true;
       } else
           leap = false;
       return leap;
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String arr[] = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September",
               "October", "November", "December" };
       System.out.println("Enter date in the format(dd-mm-yyyy)");
       String d = sc.next();
       int day = Integer.parseInt(d.split("-")[1]);
       int month = Integer.parseInt(d.split("-")[0]);
       int year = Integer.parseInt(d.split("-")[2]);
       int days = getDays(month, year);
       if (day < 1 || day > days) {
           throw new InvalidDayException("Invalid day " + day);
       }
       if (month < 1 || month > 12) {
           throw new InvalidMonthException("Invalid month " + month);

       }
       System.out.println(arr[month] + " " + day + ", " + year);
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Write program that prompts the user to enter a person's date of birth in numeric form...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • Write a program that prompts the user to enter a length in feet and inches and...

    Write a program that prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number or a nondigit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Using Python

  • c++ Write a program that prompts the user to enter a length in feet and then...

    c++ Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again.

  • Write a C# program that prompts a user to enter a birth month and day. Display...

    Write a C# program that prompts a user to enter a birth month and day. Display an error message if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message.

  • Write a program named CheckMonth2 that prompts a user to enter a birth month and day....

    Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...

  • Write a program that prompts the user for two inputs: a divisor and a dividend that...

    Write a program that prompts the user for two inputs: a divisor and a dividend that can handle the exceptions of input failure (non-integer input for both dividend and divisor) and dividing by 0 (divisor only). Use try-catch statements and throw statements.

  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • Write java program that prompts the user to enter integers from the keyboard one at a...

    Write java program that prompts the user to enter integers from the keyboard one at a time. Program stops reading integers once the user enters the same value three times consecutively. Program then outputs "Same entered 3 in a row. "

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT