Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose: Understanding data validation 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

Purpose: Understanding data validation

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 return 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., January15,2017). (See UML for details.)

Provided Resources:

1.Lab 08Instructions, including requirements

2.A test harness for your code

3.InvalidDateExceptionclass

4.UML for class ValidDate

5.Sample output

Requirements

ValidDateClass:

Constructor

There will be only one constructor. It will accept a single String value. 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. Validate the month value. If the month value is less than 1 or greater than 12, throw exception InvalidDateException, passing the message "Month value must be greater than or equal to 1 and less than or equal to 12".

Validate the day value based on the month, that is, January 31 is valid, but neither February 31 nor April 31 would be. If an invalid day is encountered, throw exception InvalidDateException, passing the message "Day value must be greater than 0 and less than "The year value must be validated to be sure that it is greater than 2009. If an invalid year is encountered, throw exception InvalidDateException, passing the message "Year must be greater than 2009".

Get and Set Methods

Get and set methods for each instance variable must be defined.

getLongDate Method

Using the instance variables as a starting point, return a String using the long-form month name. Example, 09/17/2014would be returned as "September17, 2014". Use StringBuilder to do this.

(It is not necessary to code a toString method for this class; the getValidDate method serves the same purpose.)

Grading: If your program compiles clearly and functions exactly as required, your

grade will be 100%. Otherwise, it will be 0%.

Expected Output:

Following is a sample of expected output when you run DateConversion.java test harness (The items in bluetype are user input):

Enter a date in yyyy/mm/dd format:

2003/14/32

You entered: 2003/14/32

InvalidDateException: Year must be greater than 2009

Do you want to enter another (Y/N)?

Y

Enter a date in yyyy/mm/dd format:

2016/05/12

You entered: 2016/05/12

The long form date reads: May 12, 2016

The short form date reads:5/12/2016

Do you want to enter another (Y/N)?

Y

Enter a date in yyyy/mm/dd format: yyyy/07/24

You entered: yyyy/07/24

WHAT HAPPENED?

Do you want to enter another (Y/N)?

n

image text in transcribed

Provided code:

InvalidDateException:

// 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

DateConversion:

DA // 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

UML Class Diagram ValidDate Attributes ublic validYear: int ublic validMonth: int ublic validDav: int validated year value;> 2009 validated month value;

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

Why is it useful to compare multiple companies at once?

Answered: 1 week ago