Question
// This is a custom exception class to be used in Lab 08. // This exception will be thrown if any part of the String
// This is a custom exception class to be used in Lab 08. // This exception will be thrown if any part of the String offered to ValidDate is not valid.
public class InvalidDateException extends Exception { // The following constructor accepts a message as the only input parameter. This message will be displayed back to the user. public InvalidDateException( String message ) { super( message ); } // end one-parm constructor public InvalidDateException( String message, Throwable cause ) { super( message, cause ); } // end two-parm constuctor } // end InvalidDateException
// This is the test harness to use to validate/test the ValidDate class
import java.util.Scanner; // import Scanner class for retrieving user input
public class DateConversion {
public static void main (String [] args) { Scanner keyboard = new Scanner(System.in); // instantiate new object of Scanner class char answer = 'Y'; // declare & initialize variable to hold use response // assume starting point of "Yes"
while (Character.toUpperCase( answer ) == 'Y') // Test user response to continuation prompt { System.out.print("Enter a date in yyyy/mm/dd format: "); String offeredDate = keyboard.nextLine( ); // Accept & store user input
try { System.out.printf( "%nYou entered: %s", offeredDate ); // 'Mirror' input to user ValidDate inDate = new ValidDate ( offeredDate ); // Attempt to instantiate a ValidDate w/ input System.out.printf( "%nThe long form date reads: %s", inDate.getLongDate( ) ); System.out.printf( "%nThe short form date reads: %s%n%n", inDate.getValidDate( ) ); } // end try catch(InvalidDateException badDate) // if the data is 'bad' catch the exception here { System.out.printf( "%n%s%n", badDate.toString() ); } // end InvalidDateException catch catch( Exception xcptn ) // this catch block will catch ALL OTHER exceptions { System.err.printf( "%nWHAT HAPPENED?%n" ); } // end Exception catch System.out.println( "Do you want to enter another (Y/N)? "); // prompt user - additional input?
answer = keyboard.nextLine().charAt(0); // retrieve single character of user input } // end while
} // end main } // end DateConversion
General Information You will write a Java class (ValidDate.java) that will store a user-input date as a String in the format yyyymmdd where yyyy is the four-digit year, mm is a numeric representation of month, and dd is the day of the month. The class constructor will validate that mm is valid, that dd is valid for the month provided, and that yyyy is greater than 2009. Two additional methods will be defined. One will returr the date in mm/dd/yyyy format and one that will return the date with mm converted to the long-form month name (i.e., January 15, 2017). (See UML for details.)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started