Question: Create class DateAndTime that combines the modified Time2 class of Exercise 8.7 and the modified Date class of Exercise 8.8 . Modify method incrementHour to

Create class DateAndTime that combines the modified Time2 class of Exercise 8.7 and the modified Date class of Exercise 8.8 . Modify method incrementHour to call method nextDay if the time is incremented into the next day. Modify methods toString and toUniversalString to output the date in addition to the time. Write a program to test the new class Date AndTime. Specifically, test incrementing the time to the next day.

Exercise 8.7

Modify class Time2 of Fig. 8.5 to include a tick method that increments the time stored in a Time2 object by one second. Provide method incrementMinute to increment the minute by one and method incrementHour to increment the hour by one. Write a program that tests the tick method, the incrementMinute method and the incrementHour method to ensure that they work correctly.

Exercise 8.8

Modify class Date of Fig. 8.7 to perform error checking on the initializer values for variables month, day and year (currently it validates only the month and day). Provide a method nextDay to increment the day by one. Write a program that tests method nextDay in a loop that prints the date during each iteration to illustrate that the method works correctly. 

Fig. 8.7

I // Fig. 8.7: Date.java // Date class declaration. 2 3 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23

I // Fig. 8.7: Date.java // Date class declaration. 2 3 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 N N N N N M M M M 26 27 28 29 30 31 32 33 34 35 36 37 38 public class Date { 39 40 41 42 43 44 45 } 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 12) { throw new Illegal ArgumentException ( "month (" + month + ") must be 1-12"); } } // check if day in range for month if (day 0 || (day> daysPerMonth [month] && ! (month == 2 && day throw new Illegal ArgumentException ("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 || (year % 4 == 0 && year % 100 != 0))) { throw new Illegal ArgumentException ("day (" + day + ") out-of-range for the specified month and year"); } // return a String of the form month/day/year public String toString() { return String.format("%d/%d/%d", month, day, year); 29))) { } this month = month; this.day = day; this.year = year; System.out.printf("Date object constructor for date %s %n", this);

Step by Step Solution

3.50 Rating (153 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To create a DateAndTime class that combines the functionality of both the Time2 class from Exercise 87 and the Date class from Exercise 88 well need to integrate the features from both classes Since t... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!