Modify the Date class below by adding a new method called nextDay() that increments the Date by 1 when called and returns a new Date object. This method should properly increment the Date across Month boundary (i.e from the last day of the month to the first day of the next month). //Date class declaration public class Date { private int month; // 1-12 private int day; // 1-31 based on month private int year; // any year private static final int [] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //constructor: confirm proper value for month and day given the year public Date (int month, int day, int year) { //check if month in range if (month <=0 II month > 12) { throw new IllegalArgumentException( “month (“ + month + “) must be 1-12”); } //check if day in range for month if (day <= 0 II (day > daysPerMonth[month] && ! (month == 2 && day == 29))) { Throw new IllegalArgumentException(”day (“ + day + “) out-of-range for the specified month and year”); } //check for leap year if month is 2 and day is 29 if (month == 2 && day == 29 && !(year % 400 ==0 II (year % 4 == 0 && year % 100 ! = 0 ))) { throw new IllegalArgumentException(“day (“ + day + “) out-of-range for the specified month and year”); } this.month = month; this.day = day; this.year = year; System.out.printf(“Date object constructor for date %s%n”, this); } //return a String of the form month/day/year public String toString() { return String.format(“%d/%d/%d”, month, day, year): } } Write a program called DateTest that asks the user to enter 3 numbers (one at a time) corresponding to Month, Day and Year. The first two will be 2 digits(Month and Day) and 3rd will be 4 digits(Year). Read the numbers and create a corresponding Date object. Write a loop that increments this Date 3 times by calling nextDay()and displays each Date in the format mm/dd/yyyy on the console. You MUST use nextDay() to generate the dates. You CANNOT HARD CODE the output. Your program should work for any valid data that user enters. (Assume that the user will enter VALID month,day and year numbers). You can assume that all dates will be in the same year 2020. Example: If user enters 07 30 and 2020. The following should be displayed: 07/30/2020 07/31/2020 08/01/2020 My first file: // Date.java // importing the required packages import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public class Date { private int month; private int day; private int year; private static final int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public Date(int month, int day, int year) { // check month if(month<=0 || month>12) { throw new IllegalArgumentException("month("+month+")must be betwween 1-12"); } // check day if(day<=0 || (day>daysPerMonth[month] &&!(month==2 && day==29))) { throw new IllegalArgumentException("day("+day+") out-of-range for the specified month and year"); } // Check eap year if(month==2 && day==29 && !(year % 400==0 || (year % 4==0 && year % 100!=0))) { throw new IllegalArgumentException("day("+day+")out-of-range for the specified month and year"); } // choose month, date, and year this.month = month; this.day = day; this.year = year; // System.out.printf("Date object constructor for date %s%n", this); } // return String month/day/year public String toString() { return String.format("%02d/%02d/%04d", month, day, year); } public void nextDay() { // specify no of days in a month { int numDays=daysPerMonth[month]; //if month is Feb and leap year, setnumDays to 29 if(month==2 && (year%400==0 ||(year %4==0 &&year%100!=0))) numDays =29; } //advance day day++; //If day out of range if(day>numDays){ //revert day to 1 day = 1; //advance month month++; //If month is out of range, revert to 1 and add year if(month>12) { month=1; year++; } } My second file: //DateTest.java // import packages import java.util.Scanner; public class DateTest { public DateTest() { //TODO Auto-generated constructor stub } public static void main(String[]args) { //TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter a date in mm/dd/yyyy format:"); int month=in.nextInt(); int day=in.nextInt(); int year=in.nextInt(); // Inputting the string Date date = new Date(month,day,year); for(int i=0; i<3; i++) { System.out.println(date); date.nextDay(); in.close(); } } } Errors received: Exception in thread "main" java.lang.Error: Unresolved compilation problems: numDays cannot be resolved to a variable Syntax error, insert "}" to complete ClassBody at Date.(Date.java:51) at DateTest.main(DateTest.java:20)
Modify the Date class below by adding a new method called nextDay() that increments the Date by 1 when called and returns a new Date object. This method should properly increment the Date across Month boundary (i.e from the last day of the month to the first day of the next month).
//Date class declaration
public class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
private static final int [] daysPerMonth =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//constructor: confirm proper value for month and day given the year
public Date (int month, int day, int year) {
//check if month in range
if (month <=0 II month > 12) {
throw new IllegalArgumentException(
“month (“ + month + “) must be 1-12”);
}
//check if day in range for month
if (day <= 0 II
(day > daysPerMonth[month] && ! (month == 2 && day == 29))) {
Throw new IllegalArgumentException(”day (“ + day +
“) out-of-range for the specified month and year”);
}
//check for leap year if month is 2 and day is 29
if (month == 2 && day == 29 && !(year % 400 ==0 II
(year % 4 == 0 && year % 100 ! = 0 ))) {
throw new IllegalArgumentException(“day (“ + day +
“) out-of-range for the specified month and year”);
}
this.month = month;
this.day = day;
this.year = year;
System.out.printf(“Date object constructor for date %s%n”, this);
}
//return a String of the form month/day/year
public String toString() {
return String.format(“%d/%d/%d”, month, day, year):
}
}
Write a program called DateTest that asks the user to enter 3 numbers (one at a time) corresponding to Month, Day and Year. The first two will be 2 digits(Month and Day) and 3rd will be 4 digits(Year). Read the numbers and create a corresponding Date object. Write a loop that increments this Date 3 times by calling nextDay()and displays each Date in the format mm/dd/yyyy on the console. You MUST use nextDay() to generate the dates. You CANNOT HARD CODE the output.
Your program should work for any valid data that user enters. (Assume that the user will enter VALID month,day and year numbers).
You can assume that all dates will be in the same year 2020.
Example:
If user enters 07 30 and 2020. The following should be displayed:
07/30/2020
07/31/2020
08/01/2020
My first file:
// Date.java
// importing the required packages
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Date {
private int month;
private int day;
private int year;
private static final int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public Date(int month, int day, int year) {
// check month
if(month<=0 || month>12) {
throw new IllegalArgumentException("month("+month+")must be betwween 1-12");
}
// check day
if(day<=0 || (day>daysPerMonth[month] &&!(month==2 && day==29))) {
throw new IllegalArgumentException("day("+day+") out-of-range for the specified month and year");
}
// Check eap year
if(month==2 && day==29 && !(year % 400==0 || (year % 4==0 && year % 100!=0))) {
throw new IllegalArgumentException("day("+day+")out-of-range for the specified month and year");
}
// choose month, date, and year
this.month = month;
this.day = day;
this.year = year;
// System.out.printf("Date object constructor for date %s%n", this);
}
// return String month/day/year
public String toString() {
return String.format("%02d/%02d/%04d", month, day, year);
}
public void nextDay() {
// specify no of days in a month
{
int numDays=daysPerMonth[month];
//if month is Feb and leap year, setnumDays to 29
if(month==2 && (year%400==0 ||(year %4==0 &&year%100!=0))) numDays =29;
}
//advance day
day++;
//If day out of range
if(day>numDays){
//revert day to 1
day = 1;
//advance month
month++;
//If month is out of range, revert to 1 and add year
if(month>12) {
month=1;
year++;
}
}
My second file: //DateTest.java
// import packages
import java.util.Scanner;
public class DateTest {
public DateTest() {
//TODO Auto-generated constructor stub
}
public static void main(String[]args) {
//TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
System.out.print("Enter a date in mm/dd/yyyy format:");
int month=in.nextInt();
int day=in.nextInt();
int year=in.nextInt();
// Inputting the string
Date date = new Date(month,day,year);
for(int i=0; i<3; i++) {
System.out.println(date);
date.nextDay();
in.close();
}
}
}
Errors received:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
numDays cannot be resolved to a variable
Syntax error, insert "}" to complete ClassBody
at Date.<init>(Date.java:51)
at DateTest.main(DateTest.java:20)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images