For the given class below, I was asked to test this class with a new TimeTest class that has a method doTest() to try out these methods in my Time class. I have tested classes before via the main method, but I have no idea how to test a class using a class. Could anyone help me get started?
public class Time {
private int minute;
private int hour;
private int totalMinute;
public Time() {
minute = 0;
hour = 0;
}
public Time(int hours, int minutes) {
setHour(hours);
setMinute(minutes);
}
private void setHour(int hours) {
if (hours < 24 && hours >= 0) {
hour = hours;
} else {
hour = 0;
}
}
private void setMinute(int minutes) {
if (minutes < 60 && minutes >= 0) {
minute = minutes;
} else {
minute = 0;
}
}
public void setTime(int hours, int minutes) {
setHour(hours);
setMinute(minutes);
}
public int getElapsedTime(Time that) {
int thisTime = this.getTotalMinutes();
int thatTime = that.getTotalMinutes();
if (thisTime > thatTime) {
return thisTime - thatTime;
}
return thatTime - thisTime;
}
private int getTotalMinutes() {
totalMinute = (hour * 60) + minute;
return totalMinute;
}
public String getAsString() {
if (hour < 10 && minute < 10) {
return "0" + hour + ":0" + minute;
} else if (hour < 10 && minute >= 10) {
return "0" + hour + ":" + minute;
} else if (hour >= 10 && minute < 10) {
return hour + ":0" + minute;
} else {
return hour + ":" + minute;
}
}
}
Here is the class which may meet your requirement. If you do not want main method in this class then write the main method in another class and Create object of TimeTest and call the doTest(time) method from that class which has main method.
}
Thanks