CompSci 251 Assignment 2 Java Due 213 2017 1000am Topics Cov

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 doesn’t 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

Solution

Hi buddy, please find the below java program. Comments have been added for better understanding

import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
//This method checks if the given String is a valid integer
static boolean isStringInt(String str){
str = str.trim();
for(int i=0;i<str.length();i++){
if(!(str.charAt(i)>=\'0\'&&str.charAt(i)<=\'9\')){
return false;
}
}
return true;
}
//This array stores the months and days[] store number of days in each month
static String months[] = {\"january\",\"february\",\"march\",\"april\",\"may\",\"june\",\"july\",\"august\",\"september\",\"october\",\"november\",\"december\"};
static int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  
//This method checks if the day and month combination is valid
static boolean isValidMonthDay(int day, int month){
return month<=12&&day<=days[month-1];
}
  
//This method checks if the year is valid
static boolean isValidYear(int year){
return year>=1900;
}
  
//This method converts the given day, month and year into day + Month + Year format
static String standardDateString(int day, int month , int year){
return day+\" \"+((char)(months[month-1].charAt(0)-32)+months[month-1].substring(1,months[month-1].length()))+\" \"+year;
}
  
//This method checks if the given month abbrevation is correct or not
static boolean isValidMonthAbbrevation(String month){
month = month.trim();
for(String x : months){
if(month.toLowerCase().equals(x)){
return true;
}
}
return false;
}

   public static void main (String[] args) throws java.lang.Exception
   {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   System.out.println(\"Welcome to the CS251 Date Parser!\");
   //This method splits all the given dates seperated by #
   String dates[] = br.readLine().trim().split(\"#\");
   int ind = 0;
   for(String date : dates){
   System.out.print(\"Date \"+(++ind)+\":\");
   String inds[] = date.trim().split(\"-\");
  
   //Checking for dashes first format here
   if(inds.length==3){
   int ds = 0;
   //Counting number of dashes
   for(int i=0;i<date.length();i++){
   if(date.charAt(i)==\'-\'){
   ds++;
   }
   }
   //If there are more than 2 dashes exit
   if(ds>2){
   System.out.println(\"Too many dashes\");
   continue;
   }
  
   if(isStringInt(inds[0].trim())&&isStringInt(inds[1].trim())&&isStringInt(inds[2].trim())){
   if(Integer.parseInt(inds[0].trim())>12){
   System.out.println(\"Invalid month string\");
   continue;
   }
   if(isStringInt(inds[2].trim())&&isValidYear(Integer.parseInt(inds[2].trim()))){
   System.out.println(\"Invalid year, too low\");
   continue;
   }
   if(isValidMonthDay(Integer.parseInt(inds[1].trim()),Integer.parseInt(inds[0].trim()))&&isValidYear(Integer.parseInt(inds[2].trim()))){
   System.out.println(standardDateString(Integer.parseInt(inds[1].trim()),Integer.parseInt(inds[0].trim()),Integer.parseInt(inds[2].trim())));
   continue;
   }
   }
  
   }
   //Checking for day first format here
   inds = date.trim().split(\" \");
   if(inds.length==3){
   if(isStringInt(inds[0])){
   for(int i=0;i<months.length;i++){
   if(months[i].startsWith(inds[1].trim().toLowerCase())){
   if(isValidMonthDay(Integer.parseInt(inds[0].trim()),i+1)){
   if(isValidYear(Integer.parseInt(inds[2].trim()))){
   System.out.println(standardDateString(Integer.parseInt(inds[0].trim()),(i+1),Integer.parseInt(inds[2].trim())));
   continue;
   }
   }
   else{
   System.out.println(\"Invalid month or day number\");
   continue;
   }
   }
   }
   }
   }
   //Checking for month first format here
   //If the length of the month abbrevation is less than 3, print error message
   if(inds[0].trim().length()<3){
   System.out.println(\"Invaild month string \");
   continue;
   }
     
   inds = date.trim().split(\",\");
   String tempDate = inds[0].trim().split(\" \")[inds[0].trim().split(\" \").length-1];
   String tempYear = inds[1].trim();
   date = new StringBuilder(date).deleteCharAt(date.indexOf(\",\")).toString();
   String tempMonth = inds[0].trim().split(\" \")[0];
   for(int i=0;i<months.length;i++){
   if(months[i].startsWith(tempMonth.trim().toLowerCase())){
   if(isValidMonthDay(Integer.parseInt(tempDate.trim()),i+1)){
   if(isValidYear(Integer.parseInt(tempYear.trim()))){
   System.out.println(standardDateString(Integer.parseInt(tempDate.trim()),(i+1),Integer.parseInt(tempYear.trim())));
   continue;
   }
   }
   else{
   System.out.println(\"Invalid month or day number\");
   continue;
   }
   }
   }
   }
   System.out.println(\"Good Bye!\");
   }
}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site