Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Date.java class file · Date objects should store the date in two int instance va

ID: 3629055 • Letter: D

Question

Date.java class file

· Date objects should store the date in two int instance variables - day and month, and it should include the String instance variable, error, initialized with null.

Implement a 1-parameter Date constructor that receives a dateStr string parameter and performs complete error checking on the given dateStr value. The Date constructor is the entity that’s responsible for date error checking. That way, if a Date object is instantiated and if there are no errors, then you’re guaranteed that the Date object holds a legitimate date value. If any kind of error occurs, change the value of the error instance variable to a non-null string value, using an appropriate concatenation of a string constant, input substring, and/or API exception message.
Constructors use the same exception handling rules as methods: In a try block, include the parsing of the month and day substrings and other error-checking logic that will not work if parsing fails.

· Take into account the actual number of days in each month, but assume that there are always 28 days in February.

· To extract day and month numbers from the given date string, use String’s indexOf method to find the location of the slash character, and String’s substring method to extract month and day substrings from the input string.

· Include a method for printing the date with a numeric format. Use the zero-pad flag in a printf method call to get exactly two digits for each month and day.

· Include a method for printing the date with an alphabetic format.

Include a getError method which returns the value of the error instance variable.



50%

LastNameFirstNameDateDriver.java class file

In your driver class, include a loop that repeatedly:

· Asks the user to enter a date or “q” to quit.

· If the entry is not “q”, instantiate a Date object.

· If the error variable is null:

o Print the date using numeric format.

o Print the date using alphabetic format.

Otherwise, print the value of the error variable.



35%

Comments / Flow Chart / Pseudocode / Style

Comments are REQUIRED; flow charts and Pseudocode are NOT REQUIRED.
Proper coding style is REQUIRED. Refer to the JavaCodingStyle.doc in Doc Sharing for more information.


10%

Upload your page to the Dropbox.

NOTE: Complete your activity and submit it to the Dropbox.




Total Points


100%

Sample Session:

Enter a date in the form mm/dd ("q" to quit): 5/2
05/02
May 2
Enter a date in the form mm/dd ("q" to quit): 05/02
05/02
May 2
Enter a date in the form mm/dd ("q" to quit): 52
Invalid date format – 52
Enter a date in the form mm/dd ("q" to quit): 5.0/2
Invalid format - For input string: "5.0"
Enter a date in the form mm/dd ("q" to quit): 13/2
Invalid month – 13
Enter a date in the form mm/dd ("q" to quit): 2/x
Invalid format - For input string: "x"
Enter a date in the form mm/dd ("q" to quit): 2/30
Invalid day – 30
Enter a date in the form mm/dd ("q" to quit): 2/28
02/28
February 28
Enter a date in the form mm/dd ("q" to quit): q






Explanation / Answer

please rate - thanks

import java.util.*;
public class LastNameFirstNameDateDriver{
public static void main(String[] args)

{
String input;
Scanner in=new Scanner(System.in);
System.out.print("Enter a date in the form mm/dd ("q" to quit): ");
input=in.nextLine();
while(input.charAt(0)!='q')
   {Date d=new Date(input);
       if(d.getError()==null)
                 { d.printNumbers();
                   d.printWords();
                    }
        else
                   System.out.println(d.getError());           
       
       System.out.print("Enter a date in the form mm/dd ("q" to quit): ");
      input=in.nextLine();
      }   
}
}

--------------------------------------------------


public class Date
{private int month;  
private int day;
private String input;
private String error;       
public Date(String i)
    {error=null;
     boolean good=true;
     int daysInMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
     int n,j;
     String s;
     n=i.indexOf('/');
         if(n==-1)
              error="Invalid date format "+i;
          else
              {s=i.substring(0,n);
                 for(j=0;j<s.length();j++)
                      if(!Character.isDigit(s.charAt(j)))
                            {error="Invalid format - for input string: ""+s+""";
                             good=false;
                             }
                if(good)
                   {
                     month=Integer.parseInt(s);
                    if(month<1||month>12)
                        error="Invalid month –"+month;
                    else
                        {s=i.substring(n+1,i.length());
                          for(j=0;j<s.length();j++)
                        if(!Character.isDigit(s.charAt(j)))
                             {error="Invalid format - for input string: ""+s+""";
                               good=false;
                              }
                    if(good)
                             {day=Integer.parseInt(s);
                           if(day>daysInMonth[month-1])
                                  error="Invalid day – "+day;
                                }
                            }
                }
          }
    }
public void printNumbers()
    {System.out.printf("%02d/%02d ",month,day);
    }
    public void printWords()
    {String m[]={"January ","February ","March ","April ","May ","June ","July ",
               "August ","September ","October ","November ","December "};  
     System.out.println(m[month-1]+" "+day);
     }
public String getError()
{return error;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote