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

Create a program called Date.java to perform error-checking on the initial value

ID: 3546247 • Letter: C

Question

Create a program called Date.java to perform error-checking on the initial values, for instance: fields month, day, and year. Also, provide a method nextDay() to increment the day by one. The Date object should always remain in a consistent state.

Write a program called DateTest.java that prompts the user to enter the month, day, and year as numeric values. This program then creates a Date object using the Date class you just created and tests the nextDay() method. This can be done in a loop of 40 iterations: the Date object calls the nextDay() method and prints the date during each iteration of the loop. This loop is to illustrate that the nextDay() method works correctly. Test the following cases:

Sample Program Output:

Checking increment
Date object constructor for date 11/27/2011
Incremented Date:11/28/2011
Incremented Date:11/29/2011
Incremented Date:11/30/2011
Day 31 invalid. Set to day 1.
Incremented Date:12/1/2011
Incremented Date:12/2/2011
...
Incremented Date:12/30/2011
Incremented Date:12/31/2011
Day 32 invalid. Set to day 1.
Incremented Date:1/1/2012
Incremented Date:1/2/2012
Incremented Date:1/3/2012
Incremented Date:1/4/2012
Incremented Date:1/5/2012
Incremented Date:1/6/2012

PLS HELP

Explanation / Answer

//Date.java
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Date {
//to hold date
public static Calendar cal;
public static void main(String[] args) {

try {
//setting values
DisplayDate(11, 27, 1988);
//printing inital value
System.out.println("Date object constructor for date: "+print());
//incrementing date
for (int i = 1; i < 41; i++) {
nextDay();
System.out.println("Incremented Date: "+print());
}
} catch (Exception e) {
}

is on one file

and
public static void DisplayDate(int month, int day, int year) throws Exception {
cal = new GregorianCalendar();
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
cal.set(Calendar.YEAR, year);

if (cal.get(Calendar.YEAR) != year || cal.get(Calendar.MONTH) + 1 != month || cal.get(Calendar.DATE) != day) {
System.out.println("Invalid Date");
}
}

//calculating next day
public static void nextDay() {
int date = cal.get(Calendar.DATE);
cal.add(Calendar.DATE, 1);
if(date==31 && cal.get(Calendar.DATE)==1)
System.out.println("Day 32 invalid. Set to day 1. ");
if(date==30 && cal.get(Calendar.DATE)==1)
System.out.println("Day 31 invalid. Set to day 1. ");
if(date==29 && cal.get(Calendar.DATE)==1)
System.out.println("Day 30 invalid. Set to day 1. ");
if(date==28 && cal.get(Calendar.DATE)==1)
System.out.println("Day 29 invalid. Set to day 1. ");

}

//returning string form of date
public static String print() {
return String.format("%d/%d/%d", cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DATE), cal.get(Calendar.YEAR));
}

}

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