Question: JAVA only please ********************************PROVIDED CODE**************************** // This is the test harness to use to validate/test the ValidDate class import java.util.Scanner; // import Scanner class for
JAVA only please



********************************PROVIDED CODE****************************
// 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 keyboard.close(); } // end main } // end DateConversion
****************************************************************************
// 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
*****************************************************************************
public class ValidDate { public int validYear; public int validMonth; public int validDay; public ValidDate(String date) throws InvalidDateException { String dateArray[] = date.split("/"); String errorMessage = ""; try { int year = Integer.parseInt(dateArray[0]); int month = Integer.parseInt(dateArray[1]); int day = Integer.parseInt(dateArray[2]); if (isValidYear(year)) { validYear = year; } else { errorMessage = "Year must be greater than 2009."; throw new InvalidDateException(errorMessage); } if (isValidMonth(month)) { validMonth = month; } else { errorMessage = "Month must be between 1 to 12"; throw new InvalidDateException(errorMessage); } if (isValidDay(day,month)) { validDay = day; } else { errorMessage = "Invalid day for month "+month; throw new InvalidDateException(errorMessage); } } catch(Exception e) { throw new InvalidDateException(errorMessage); } } public int getValidYear() { return validYear; } public int getValidMonth() { return validMonth; } public int getValidDay() { return validDay; } public void setValidYear(int year) throws InvalidDateException { if (isValidYear(year)) { validYear = year; } else { throw new InvalidDateException("Invalid year."); } } public void setValidMonth(int month) throws InvalidDateException { if (isValidMonth(month)) { validMonth = month; } else { throw new InvalidDateException("Invalid month."); } } public void setValidDay(int day) throws InvalidDateException { if (isValidDay(day,validMonth)) { validDay = day; } else { throw new InvalidDateException("Invalid day."); } } public boolean isValidDay (int day,int month) { if ((day >= 1 && day = 1 && day = 1 && day 12) { return false; } else { return true; } } public boolean isValidYear (int year) { if (year > 2009) { return true; } else { return false; } } public String getMonthName() { switch (validMonth) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December "; } return "January"; } public String getValidDate() { return getValidMonth() +"/" + getValidDay() + "/" + getValidYear(); } public String getLongDate() { return getMonthName() +" " + getValidDay() + ", " + getValidYear(); } }
****************************************END CODE************************************************
Thanks!
Purpose: Understanding regular expressions General Information Using your Lab 08 code as a starting point for this assignment, you will rewrite data validations by applying regular expressions. The validations themselves will not change significantly, and regular expressions will not suffice for all validations. The UML for ValidDate and the test harness have not changed. The expected output has changed only slightly Provided Resources 1. Lab 10 Instructions, including requirements 2. A test harness for your code 3 InvalidDateException class 4. UML for class ValidDate 5. Sample output Requirements ValidDate Class Constructor There will be only one constructor. It will accept a single String value. Validate the incoming String using a regular expression. Check for the following -The incoming String value must contain three groups of digits - The digit groupings for month and day should be 1 or 2 digits long - The digit grouping for year must be exactly 4 digits long - The individual groupings must be separated by a slash "/) . If the user-provided String value meets the above requirements, the constructor will then parse the String into three parts (each to be stored as an int value) based on a separator character (/) a month value, a day value, and a year value . Else, the constructor will throw an InvalidDateException with a message describing the required input
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
