how come the illegal argument exception doesnt pop out of my output. public clas
ID: 3811626 • Letter: H
Question
how come the illegal argument exception doesnt pop out of my output.
public class Time {
private int mHour;
private int mMinute;
// constructor
public Time(int Hour, int Minute) {
// lets check for invalid input:
if (Hour < 0 || Hour > 23)
throw new IllegalArgumentException("Error. Hour must be between 0 and 23.");
if (Minute < 0 || Minute > 59)
throw new IllegalArgumentException("Error. Hour must be between 0 and 59.");
mHour = Hour;
mMinute = Minute;
}
// Copy Constructor
public Time(Time otherTime) {
// just copy the constructor with "this". shortcut for it.
// this() means call another existing constructor
this(otherTime.mHour, otherTime.mMinute);
// OLD:
// mHour = otherTime.mHour;
// mMinute = otherTime.mMinute;
}
// Default constructor
public Time() {
this(0, 0);
// OLD:
// mHour = 0;
// mMinute = 0;
}
public int getmHour() {
return mHour;
}
public int getmMinute() {
return mMinute;
}
public void setmHour(int mHour) {
this.mHour = mHour;
}
public void setmMinute(int mMinute) {
this.mMinute = mMinute;
}
public void setTime(int Hour, int Minute) {
this.mHour = Hour;
this.mMinute = Minute;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + mHour;
result = prime * result + mMinute;
return result;
}
public boolean equals(Time other) {
// Hour 0 to 23, Minute 0 to 59
if (mHour == other.getmHour() && mMinute == other.getmMinute()) {
return true;
} else {
return false;
}
}
@Override
public String toString() {
DecimalFormat twoDigits = new DecimalFormat("00");
if (isAM())
return "Time [" + twoDigits.format(mHour) + ":" + twoDigits.format(mMinute) + "AM" + "]";
else
return "Time [" + twoDigits.format(mHour) + ":" + twoDigits.format(mMinute) + "PM" + "]";
}
// Miscellaneous
public boolean isAM() {
// AM (hours < 12)
if (mHour < 12) {
return true;
}
// PM (hours > 12)
else {
return false;
}
}
private boolean isValid(int Hour, int Minute) {
if ((Hour >= 0 || Hour <= 23) && (Minute >= 0 || Minute <= 59))
return true;
else
return false;
}
}
DEMO
---------------
public class TImeDemo {
public static void main(String[] args) {
Time now = new Time(15, 56);
Time midnight = new Time();
Time CopyOfNow = new Time(now);
System.out.println(now);
System.out.println(CopyOfNow);
System.out.println(midnight);
// change copyOfNow to test illegal hour!
CopyOfNow.setmHour(25);
System.out.println(CopyOfNow);
}
}
------------
output
-------------
Time [15:56PM]
Time [15:56PM]
Time [00:00AM]
Time [25:56PM]
Explanation / Answer
Ilegal argument exception is not displayed when invalid value for hour is entered because you are setting the variable mHour after calling the copy constructor with values 15, 56. The error will pop up when you are sending invalid values to the constructor and not when calling the method because you are not validating the values befoe changing them in the methods.
Time now = new Time(15, 56); -----> Here the values that you are sending are 15, 56 as hours and minutes respectively.
Time midnight = new Time(); -----> Here the values that you are sending are 0, 0 as hours and minutes respectively.
Time CopyOfNow = new Time(now); -----> Here the values that you are sending are 15, 56 as hours and minutes respectively and hence no error is poped.
System.out.println(now);
System.out.println(CopyOfNow);
System.out.println(midnight);
// change copyOfNow to test illegal hour!
CopyOfNow.setmHour(25); ----> Here you are just setting the value and not validating
System.out.println(CopyOfNow);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.