7.10″Modify class Account to provide a method called withdraw that withdraws money from an Account. Ensure that the withdrawal method does not exceed the Account’s balance. If it does, the balance should be left unchanged and the method should print a message indicating “Withdrawal amount exceeded account balance.” Modify class AccountTest to test method withdraw.”
8.7Modify 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. Be sure to test the following cases: a) incrementing into the next minute, b) incrementing into the next hour and c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
1
// Fig. 8.5: Time2.java
// Time2 class declaration with overloaded constructors.
2
3
4
public class Time2
{
5
6
7
private int hour; // 0 – 23
private int minute; // 0 – 59
private int second; // 0 – 59
8
9
10
11
12
// Time2 no-argument constructor:
// initializes each instance variable to zero
public Time20
{
this(0, 0, 0); // invoke constructor with three arguments
}
13
14
15
16
17
18
19
// Time2 constructor: hour supplied, minute and second defaulted to 0
public Time2(int hour)
{
this(hour, 0, 0); // invoke constructor with three arguments
}
20
21
22
23
24
25
// Time2 constructor: hour and minute supplied, second defaulted to 0
public Time2(int hour, int minute)
{
this(hour, minute, 0); // invoke constructor with three arguments
}
26
27
28
29
30
31
// Time2 constructor: hour, minute and second supplied
public Time2(int hour, int minute, int second)
{
if (hour = 24)
throw new IllegalArgumentException(“hour must be 0-23”);
32
33
34
35
if (minute =60)
throw new IllegalArgumentException(“minute must be 0-59”);
36
37
38
if (second =60)
throw new IllegalArgumentException(“second must be 0-59”);
39
40
41
this.hour = hour;
42
this.minute = minute;
this.second = second;
43
44
}
45
46
47
48
// Time2 constructor: another Time2 object supplied
public Time2(Time2 time)
{
// invoke constructor with three arguments
this(time.getHour(), time.getMinute(), time.getSecond());
}
49
50
51
52
53
// Set Methods
54
55
// set a new time value using universal time;
// validate the data
public void setTime(int hour, int minute, int second)
56
57
58
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException(“hour must be 0-23”);
59
60
61
62
if (minute < 0 || minute >=60)
throw new IllegalArgumentException(“minute must be 0-59”);
63
64
if (second < 0 || second >=60)
throw new IllegalArgumentException(“second must be 0-59”);
65
66
67
68
this.hour = hour;
this.minute = minute;
this.second = second;
69
70
71
72
// validate and set hour
73
74
public void setHour(int hour)
{
if (hour = 24)
throw new IllegalArgumentException(“hour must be 0-23”);
75
76
77
78
this.hour = hour;
79
80
81
// validate and set minute
82
83
public void setMinute(int minute)
{
if (minute =60)
throw new IllegalArgumentException(“minute must be 0-59”);
84
85
86
87
this.minute = minute;
88
}
89
90
// validate and set second
91
92
public void setSecond(int second)
{
if (second >= 0 && second