This is a Java program that I am having trouble getting to work. Any help would
ID: 641468 • Letter: T
Question
This is a Java program that I am having trouble getting to work. Any help would be very welcomed. The requirements for the program are specific and we must match the output exactly.
Comments are REQUIRED; flow charts and Pseudocode are NOT REQUIRED.
The files must be named as follows:
LastNameFirstNameDateDriver.java>. (driver program)
Date.java (Date class file)
Ensure you include ALL files required to make your program compile and run.
I would like to see your .java files only.
Proper coding conventions required the first letter of the class start with a capital
letter and the first letter of each additional word start with a capital letter.
Overall Requirements
Create a class named Date that stores date values and prints out the date in either a
pure numeric format or a name and number format (see sample session for format details).
Date.java class file
Explanation / Answer
// Date.java
public class Date {
// 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.
private int day = -1;
private int month = -1;
private String error = null;
private String errMsg = null;
private int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private String[] alphaMonth =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
// Implement a 1-parameter Date constructor that receives a dateStr string parameter
// and performs complete error checking on the given dateStr value.
public Date(String dateStr) {
int d1 = 0;
int d2 = 0;
int m1 = 0;
int m2 = 0;
int len = 0;
if (dateStr == null )
errMsg = "Date Error: Parameter dateStr is null.";
else if (dateStr.length() < 3)
errMsg = "Date Error: Parameter dateStr is too short. dateStr = "+dateStr;
else try {
len = dateStr.length();
errMsg = "Date Error: Parameter dateStr does not contain the / separator. dateStr = " + dateStr;
int pos = dateStr.indexOf("/");
if ( pos < 0 ) throw new Exception("String does not contain a / token.");
errMsg = "Date Error: Parameter dateStr does not contain a month. dateStr = " + dateStr;
if ( pos+2 <= len ) m2 = Integer.valueOf(dateStr.substring(pos+1, pos+2));
else throw new Exception(errMsg);
try {
if ( pos+3 <= len ) {
m1 = Integer.valueOf(dateStr.substring(pos+2, pos+3));
month = m2*10 + m1;
}
}
finally {
if ( m2 < 1) month = m1;
}
if (( month < 1 ) || (month > 12)) throw new Exception("Month is invalid.");
errMsg = "Date Error: Parameter dateStr does not contain a day. dateStr = " + dateStr;
if ( pos > 0 ) d2 = Integer.valueOf(dateStr.substring(pos-1, pos));
try {
if ( pos > 1 ) {
d1 = Integer.valueOf(dateStr.substring(pos-2, pos-1));
day = d1*10 + d2;
}
}
finally {
if ( d1 < 1) day = d2;
}
if (( day < 1 ) || (day > 31)) throw new Exception("Day is invalid.");
errMsg = "Date Error: Parameter dateStr has an invalid month. dateStr = " + dateStr;
int days = daysPerMonth[month-1];
errMsg = "Date Error: Parameter dateStr has an invalid day for the month. dateStr = " + dateStr;
if ( day > days ) throw new Exception("Day of month is too big.");
}
catch (Exception e) {
error = errMsg+" "+e.getMessage()+" ";
}
}
// 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.
public void printNumericDate() {
System.out.printf("The date is %02d/%02d ", day, month);
}
//Include a method for printing the date with an alphabetic format.
public void printDdMon() {
System.out.println(String.valueOf(day)+" "+ alphaMonth[month-1]);
}
public void printMonDd() {
System.out.println(alphaMonth[month-1]+" "+String.valueOf(day));
}
// Include a getError method which returns the value of the error instance variable.
public String getError() { return error; }
}
// ErinDateDriver.java
import javax.swing.JOptionPane;
public class ErinDateDriver {
public static void main(String args[]) {
String quit = "q";
boolean again = true;
String error = null;
String dateStr = null;
String msg = " Enter a date in DD/MM format or enter 'q' to quit. ";
while (again) {
dateStr = JOptionPane.showInputDialog(msg);
if ((dateStr != null) && (dateStr.length() > 0)) {
if (!dateStr.equalsIgnoreCase(quit)) {
Date d = new Date(dateStr);
error = d.getError();
if (error == null) {
d.printNumericDate();
d.printDdMon();
d.printMonDd();
System.out.println("");
} else
System.out.println(error);
} else
again = false;
} else
JOptionPane.showMessageDialog(null, msg);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.