About class member access illustrated in fig08_03: o Test this example code and explain why the program cannot get o Make necessary changes on the test class MemberAccessTest to i.e., initialize the Timel object time with the same provided value the hour-minute-second values through the object time. o Take a screenshot of your updated code segment in the test class (not the output of the execution). o You may no do any changes on the class Timel.
// Fig. 8.3: MemberAccessTest.java
// Private members of class Time1 are not accessible.
public class MemberAccessTest {
public static void main(String[] args) {
Time1 time = new Time1(); // create and initialize Time1 object
time.hour = 7; // error: hour has private access in Time1
time.minute = 15; // error: minute has private access in Time1
time.second = 30; // error: second has private access in Time1
}
}
// Fig. 8.1: Time1.java
// Time1 class declaration maintains the time in 24-hour format.
public class Time1 {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// set a new time value using universal time; throw an
// exception if the hour, minute or second is invalid
public void setTime(int hour, int minute, int second) {
// validate hour, minute and second
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 ||
second < 0 || second >= 60) {
throw new IllegalArgumentException(
"hour, minute and/or second was out of range");
}
this.hour = hour;
this.minute = minute;
this.second = second;
}
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString() {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
// convert to String in standard-time format (H:MM:SS AM or PM)
public String toString() {
return String.format("%d:%02d:%02d %s",
((hour == 0 || hour == 12) ? 12 : hour % 12),
minute, second, (hour < 12 ? "AM" : "PM"));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images