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 nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images
- Javaarrow_forwardjava Assignment Description: Write a class named Car that has the following fields: The yearModel field is an int that holds the car’s year model. The make field references a String object that holds the make of the car. The speed field is an int that holds the car’s current speed. In addition, the class should have the following constructor and other methods. The constructor should accept the car’s year and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field. Constructor: The constructor should accept no arguments. Constructor: The constructor should accept the car’s year as argument.Assign the value to the object’s yearModel. No need to assign anything to the make and assign 0 to speed. Appropriate mutator methods should set the values in an object’s yearModel, make, and speed fields. Appropriate accessor methods should get the values stored in an object’s yearModel, make, and…arrow_forwardpublic class Artwork { // TODO: Declare private fields - title, yearCreated // TODO: Declare private field artist of type Artist // TODO: Define default constructor // TODO: Define get methods: getTitle(), getYearCreated() // TODO: Define second constructor to initialize // private fields (title, yearCreated, artist) // TODO: Define printInfo() method // Call the printInfo() method in Artist.java to print an artist's information }} public class ArtworkLabel { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String userTitle, userArtistName; int yearCreated, userBirthYear, userDeathYear; userArtistName = scnr.nextLine(); userBirthYear = scnr.nextInt(); scnr.nextLine(); userDeathYear = scnr.nextInt(); scnr.nextLine(); userTitle = scnr.nextLine(); yearCreated =…arrow_forward
- Write a member method for the Team class named : calculatePercentage that it will calculate and return the winning percentage of a team. The winning percentage is calculated as follows: wins / ( wins + losses)arrow_forwardpublic class Plant { protected String plantName; protected String plantCost; public void setPlantName(String userPlantName) { plantName = userPlantName; } public String getPlantName() { return plantName; } public void setPlantCost(String userPlantCost) { plantCost = userPlantCost; } public String getPlantCost() { return plantCost; } public void printInfo() { System.out.println(" Plant name: " + plantName); System.out.println(" Cost: " + plantCost); }} public class Flower extends Plant { private boolean isAnnual; private String colorOfFlowers; public void setPlantType(boolean userIsAnnual) { isAnnual = userIsAnnual; } public boolean getPlantType(){ return isAnnual; } public void setColorOfFlowers(String userColorOfFlowers) { colorOfFlowers = userColorOfFlowers; } public String getColorOfFlowers(){ return colorOfFlowers; } @Override public void printInfo(){…arrow_forwardpublic class Accumulator { private int total private String name; public Accummulator (string name , int total) { this .name = name; this .total=total; } } 3. In a main method, create an object of Accumulator with the name as "Mary" and total as 100.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education