Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Validation String using a regular expression goes in ValidDate. java only please ********************************PROVIDED CODE**************************** // This is the test harness to use to validate/test the

image text in transcribed

Validation String using a regular expression goes in ValidDate.

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 {

//VALIDATION GOES HERE 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!

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 ")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Oracle Fusion Applications Installation And Administration

Authors: Tushar Thakker

1st Edition

1484209834, 9781484209837

More Books

Students also viewed these Databases questions