Question: DO NOT use exception handling. The DaysBetween application is not very robust. That is, it does not gracefully handle erroneous input. This exercise asks you

 DO NOT use exception handling. The DaysBetween application is not veryrobust. That is, it does not gracefully handle erroneous input. This exerciseasks you to revise the application to make bit more robust. a)

DO NOT use exception handling.

The DaysBetween application is not very robust. That is, it does not gracefully handle erroneous input. This exercise asks you to revise the application to make bit more robust.

a) When a user is prompted to enter a date, the program does not check whether the user entered an integer. If the user does not enter an integer, the program will crash due to an input mismatch error. Revise the program so that it uses the Scanner class hasNextInt method to check that the input value is an integer before reading it. If it is not, the program should detect it but not exit. Which class does this modification belong to?

b) Users can enter any combination of three integers to represent a date, even those that do not make any sense. For example, 15 37 2006 would represent the 15th month, 37th day of 2006. To remedy this potential problem, you should first add a public instance method valid to the Date class. The method should return true if the date represented by the object is a valid date, and false otherwise. Valid dates are real dates that occur after October 14, 1582. Dont forget about leap years (and valid dates of 29th February). After creating and testing the valid method, use it to check dates from within the DaysBetween program. If an invalid date is entered, display the message This is not a valid date.

c) Create a compareTo method in the Date class, with the following signature int compareTo (Date anotherDate) This method should return the value 0 if this date (the date of the object upon which the method is invoked) is equal to the argument date; value lesss than 0 of this date is a date earlier than the argument date; and a value greater than 0 if this date is a date later than the argument date. Add some samples that utilize this method in the DaysBetween class.

Complete the following :

1. Read and process 5 date pairs. If the first or second date in a pair is invalid (as described in parts a) and b), stop processing that pair and go onto the next pair, but still count that as one of the five pairs. This calls for a for loop (in the main method). If an invalid date is detected as a result of code added in Parts a) (Illegal input was entered) and b) (This is not a valid date), display the appropriate message and then go on to the next pair of dates to be processed. Hence: the program will NOT be terminated when an illegal date is encountered.

2. Be sure to comment out or remove this code from the DaysBetween class: /* if ((d1.getYear() 8 //package chol.apps; 9import java.util. Scanner; 10 public class DaysBetween i 12 13 14 15 16 17 18 19 20 21 public static void main (String [i args) Scanner scan -new Scanner (System.in); int day, month, year; System.out.println ("Enter two modern' dates: month day year") System.out.println ("For example, January 21, 1939, would be: 1 21 1939") System.out.println ) System.out.println ("Modern dates are not before "+Date.MINYEAR + "."); System.out.println ) System.out.println ("Enter the first date:"): month scan.nextInt ) day scan.nextInt (): year scan.nextInt ) Date dl -new Date (month, day, year) 23 24 25 26 27 28 29 30 31 32 System.out.println ("Enter the second date:") month scan.nextInt ) day scan. next Int(); year scan.nextInt Date d2 new Date (month, day, year): 34 35 36 37 38 39 40 41 42 43 if ( (di . getYear()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!