Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CompSci 251: Assignment 2 Java Due 2/13, 2017 10:00am Topics Covered: Programming with the String class 1 Introduction Many programs have to manipulate String objects

CompSci 251: Assignment 2 Java

Due 2/13, 2017 10:00am

Topics Covered: Programming with the String class

1 Introduction

Many programs have to manipulate String objects in interesting ways. The String class is also well designed and provides many useful methods. So, this assignment will give you some practice with it. Your task will be to parse dates written in a variety of formats and convert them into a standard format. Your program will read a line of dates separated by pound signs (#) and determine which kind of date was read.

2 Date formats

Your program will parse a line of dates written in three different styles: Dash-dates, month-first,and day- first. Here is a sample input line that you will have to handle:

25 Sept 2014 #February 25, 1960#12 -25 -2000# Janu 27,2017

This line contains four dates written in the three different formats. Notice also that space characters can appear in lots of places. Your program must handle three different date formats:

Dash-dates: month, day, and year entered as numbers with Dashes between them. There can be spaces around the numbers and Dashes, but spaces are not allowed within the numbers. Example of a valid date:12 -25 -2017

Day first: The day of the month is given as a number, followed by the month written as a word or abbreviation (of at least three letters), followed by the year. The month string can be in any mix of lower-case and upper-case characters. Spaces should separate the three parts. No spaces are allowed within the numbers or month name.

Example of a valid date:12 September 2017

Month first: The month written as a word or abbreviation, followed by the day, then a comma, and then the year. A space must separate the month and day and there may be any number of spaces around the three parts of the date and the comma.

Example of a valid date:March 27,1990 You may assume that any date containing a Dashes is in Dashes date format. You may assume that any date containing a comma is in Month-First format. You may assume that any other date is in Day-First format.

3 Requirements

Your program must read a single line that may contain many dates in any of the formats. It will print each date that is valid in Day-First format, always with the full month name, with the first letter of the month name in upper-case. The Sample Output below gives an example of what your program should produce.

1

Your program must give an error message whenever a date is invalid. It should give a specific error message in the following cases:

A month name or abbreviation is misspelled or too short(less that three characters).

A day or month number is too low or too high. Months must be between 1 and 12. Days must

be valid for the month (ignoring leap years).

A year number is too low(years less than 1900 are too low).

There are too many Dashes in a Dashes-Date.

For this assignment, it is OK if your program crashes when calling Integer.parseInt and the argument is not a valid integer.

These crashes can be avoided. One way would be to write your own code to check whether the String is valid for an integer. Another way would be to catch the exception that is thrown, but we wont teach how to do that until Chapter 15.

4 Implementation

We have some specific requirements for how you write the program. We are trying to show you a way to write programs that is more organized and is typical of what professional programmers would do. Here are the requirements:

Use the below build-in methods From the String class: substring, trim, split, toLowerCase, indexOf, or lastIndexOf

From the Integer class: parseInt

In the main(), prompt the user, reads in the line of dates, breaks the line into separate date strings,

and for each strings, prints Date : and calls the appropriate parsing method.

One method for parsing each kind of date. These methods either output an error message or the

corresponding standard date string.

public static void parseDashDate(String dateStr) public static void parseMonthFirstDate(String dateStr) public static void parseDayFirstDate(String dateStr)

public static int monthToNum(String mStr) takes a string that should be the name or abbreviation of the month name and returns the number

of the month. It returns zero if the name doesnt match any month or is too short.

public static boolean isValidMonthDay(int day, int month)

returns true if the month and day numbers form a valid day of a year (in a non-leap-year).

public static boolean isValidYear(int year)

returns true if the year is valid.

public static String standardDateString(int day, int month, int year)

takes the day, month, and year as numbers and returns a String containing that date in day- month-year format (e.g. 21 March 2017)

public static boolean isValidMonthAbbreviation(String month) takes the month as a string and returns boolean value. True, if the passed month string represent

a valid month string(the length of the trimmed month string is greater than or equals to 3, and the month string abbreviation is correctly spelled.

2

5 Sample Output

Welcome to the CS251 Date Parser! 
Enter line of dates: 25 Jan 2017# Date 1: 25 January 2017 Date 2: 25 February 1970 Date 3: 16 February 2017 
Goodbye!! Welcome to the CS251 Date Parser! 
February 25,1970# 2 - 16 - 2017 
Enter line of dates:16 augus 2020 # December 14, 1961 Date 1: 16 August 2020 Date 2: 14 December 1961 
Goodbye! Welcome to the CS251 Date Parser! 
Enter line of dates: ERROR: Empty input line 
Goodbye! Welcome to the CS251 Date Parser! 
Enter line of dates:15 xyz 2222 # 2-2-22222- # Ja 13, 2000 # Feb 30 , 2017 Date 1: ERROR: Invalid month string Date 2: ERROR: Too many Dashes Date 3: ERROR: Invalid month string 
Date 4: ERROR: Invalid month or day number Goodbye! Welcome to the CS251 Date Parser! 
Enter line of dates:# 2-0-2222 # Ja 13, 1190 # March 12,1880 Date 1: ERROR: No date entered Date 2: ERROR: Invalid month or day number Date 3: ERROR: Invalid month string 
Date 4: ERROR: Invalid Year(too low) Goodbye! 

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

Students also viewed these Databases questions