Question

Modify the Date class in Fig. 8.7 by adding a new method called nextDay() that increments...

Modify the Date class in Fig. 8.7 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).

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

Upload 2 Java files to Canvas: Date.java and DateTest.java.

Homework Answers

Answer #1

Program Code Screenshot :

Sample Output :

Program Code to Copy

import java.util.Scanner;

class Date{
    int month,day,year;

    //Argumented constructor
    public Date(int month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    @Override
    public String toString(){
        return month/10+""+month%10+"/"+day/10+""+day%10+"/"+year;
    }

    public void nextDay(){
        //Days in the month
        int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        //If year is leap, increase number of days in february
        if(year%400==0 || (year%4==0 && year%100!=0)){
            days[2]++;
        }
        //Increment the day
        this.day++;
        //Move to next month
        if(day>days[month]){
            day = 1;
            month++;
        }
        //Move to next year
        if(month>12){
            month = 1;
            year++;
        }
    }
}

class DateTest{
    public static void main(String[] args) {
        //Scanner to read the data
        Scanner obj = new Scanner(System.in);
        //Read month, day and year
        System.out.print("Enter Month, Day and year ");
        int month = obj.nextInt();
        int day = obj.nextInt();
        int year = obj.nextInt();
        //Create date
        Date date = new Date(month,day,year);
        System.out.println(date);
        //Move to next days
        date.nextDay();
        date.nextDay();
        System.out.println(date);
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
Create your own date class. Create a MyDate class with these methods. A constructor that accepts...
Create your own date class. Create a MyDate class with these methods. A constructor that accepts a month, day, and year A method that determines if the year is a leap year A method that returns the date in this format, "1/9/2020" A method that returns the date in this format, "January 9, 2020" A method that returns the day A method that returns the name of the month A method that returns the number of days in the month...
Software Quality Assurance and Testing: Many applications include a date-field widget where users can enter a...
Software Quality Assurance and Testing: Many applications include a date-field widget where users can enter a date in the form MM/DD/YYYY, representing a two-digit month, two-digit day, and four-digit year. Your boss decided to write their own date-field widget, so now you have to test it. The date-field widget is designed to accept only valid dates while rejecting all invalid dates. You can assume that the validation check is only done when the field is fully-populated with eight numeric digits...
Create the following class in C++ Create a new class call Date. Date will contain the...
Create the following class in C++ Create a new class call Date. Date will contain the date stored as day of the month, month and the year. Provide appropriate set and get methods for the class. Use doxygen comments to document Date.h (Date specification). Think about how the class will be used. Modify the input data file to cater for dates. The date in the data file refers to when the mark for the unit was obtained. Unit test the...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem requires that the digits be re-arranged.In this project, we will reverse the order of the digits in a number. Assignment: Design, develop, and test an Object-Oriented C++program that reads a whole number from the user, removes the sign and all leading and trailing zeroes from that number, and then performs the following operations on that number: 1) reverses the digits, 2) sorts the digits...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT