Name: Rox 2009-12-13 7:47
Riiight, started doing Java few days ago and having troubles with this. I know the solution is really simple, I just can't work it out since I'm too much of a noob when it comes to programming.
Basically, I've got 2 private methods to assign time elements in hours and minutes and I wanna call them both through the public method setTime without having to duplicate the code in that method, how does one do this? What I've done with it kinda works, but it still let's values outside the range to be assigned so I have no clue what to do.
I'll be greatful for any pointers/help.
/**
* Sets the value of the hour field.
* Values not in range (0 - 24) are discarded and replaced with zero.
*/
private void setHour (int setHourTo)
{
if(setHourTo > 23) {
this.hour = 0;
}
if(setHourTo < 0) {
this.hour = 0;
}
else {
this.hour = setHourTo;
}
}
/**
* Sets the value of the minute field.
* Values not in range (0 - 59) are discarded and replaced with zero.
*/
private void setMinute (int setMinuteTo)
{
if(setMinuteTo > 59) {
this.minute = 0;
}
if(setMinuteTo < 0) {
this.minute = 0;
}
else {
this.minute = setMinuteTo;
}
}
/**
* Uses parameters from setHour and setMinute to set the time.
* Only accepts values 0 - 23 for hours and 0 - 59 for minutes.
* Any other values are discarded and replaced with a zero
*/
public void setTime (int setHour, int setMinute)
{
this.hour = setHour;
this.minute = setMinute;
}
Basically, I've got 2 private methods to assign time elements in hours and minutes and I wanna call them both through the public method setTime without having to duplicate the code in that method, how does one do this? What I've done with it kinda works, but it still let's values outside the range to be assigned so I have no clue what to do.
I'll be greatful for any pointers/help.
/**
* Sets the value of the hour field.
* Values not in range (0 - 24) are discarded and replaced with zero.
*/
private void setHour (int setHourTo)
{
if(setHourTo > 23) {
this.hour = 0;
}
if(setHourTo < 0) {
this.hour = 0;
}
else {
this.hour = setHourTo;
}
}
/**
* Sets the value of the minute field.
* Values not in range (0 - 59) are discarded and replaced with zero.
*/
private void setMinute (int setMinuteTo)
{
if(setMinuteTo > 59) {
this.minute = 0;
}
if(setMinuteTo < 0) {
this.minute = 0;
}
else {
this.minute = setMinuteTo;
}
}
/**
* Uses parameters from setHour and setMinute to set the time.
* Only accepts values 0 - 23 for hours and 0 - 59 for minutes.
* Any other values are discarded and replaced with a zero
*/
public void setTime (int setHour, int setMinute)
{
this.hour = setHour;
this.minute = setMinute;
}