using the "this" method in a constructor
When I attempt to compile the following class I get an error message
pointing to one of my constructors stating "wrong number of arguments in
constructor. Can anyone shed some light on what I am missing?
Thanks.
/********************************************************
* A public class used for *
* calculating and processing time values. *
********************************************************/
public class MyTime
{
/******************************************
* private class variables to be visible *
* only to the methods in this class. *
******************************************/
private int mHours,
mMinutes,
mSeconds;
/***********************************************************
* Purpose: default constructor method to set the *
* instance var *
* *
*---------------------------------------------------------*
* USAGE: MyTime.setTime () *
*---------------------------------------------------------*
* does not return a value. *
***********************************************************/
public MyTime()
{
mHours = 00;
mMinutes = 00;
mSeconds = 00;
}
//public constructor method
public MyTime( int hours )
{
this( hours, 0, 0 ); //compiler complains here.
}
//old comments. ignore.
/***********************************************************
* Purpose: Called constructor method to set the *
* instance var *
* *
*---------------------------------------------------------*
* USAGE: setTime.setTime (int,int,int) *
*---------------------------------------------------------*
* does not return a value. *
***********************************************************/
public void MyTime( int hours, int minutes, int seconds )
{
setTime( hours, minutes, seconds );
}
/***********************************************************
* Purpose: set the instance vars so the methods in MyTime *
* can use them. *
* setTime() accepts arguements from the *
* calling routine. It tests the inputs for validity. *
*if any input is invalid set the instance vars to 0. *
*---------------------------------------------------------*
* USAGE: MyTime.setTime ( int, int, int ) *
*---------------------------------------------------------*
* does not return a value. *
***********************************************************/
public void setTime( int hours, int minutes, int seconds )
{
//set to look like garbage.
//Makes debugging easier.
mHours = -9999;
mMinutes = -9999;
mSeconds = -9999;
//test for valid hours input.
if( hours < 0 || hours > 24 )
{
//we fail and we set the instance vars to 0.
mHours = 0;
mMinutes = 0;
mSeconds = 0;
return;
}
else
//set the hours
mHours = hours;
//test for valid minutes input.
if ( minutes < 0 || minutes > 59 )
{
//we fail and we set the instance vars to 0.
mHours = 0;
mMinutes = 0;
mSeconds = 0;
return;
}
else
//set the minutes
mMinutes = minutes;
//test the seconds input
if ( seconds < 0 || seconds > 59 )
{
//we fail and we set the instance vars to 0.
mHours = 0;
mMinutes = 0;
mSeconds = 0;
return;
}
else
//set the seconds
mSeconds = seconds;
} //end of setTime
//getHour - this method returns the object's hour value.
public int getHour()
{
return mHours;
}//end getHour()
// getMinute - this method returns the object's minutevalue.
public int getMinute()
{
return mMinutes;
}//end getMinute()
//getSecond - this method returns the object's second value.
public int getSecond()
{
return mSeconds;
} //end getSecond()
/**********************************************************
* A public method that converts the time input into
* universal time format. It tests the values of the current
* instance variables for proper digit length and
* then formats the output. Once the
* values are correctly set the method returns.
*----------------------------------------------------------
* usage: x = object.toUnversalString();
* object is a class object.
*----------------------------------------------------------
* returns a string in the 24 hour format of hh:mm:ss
***********************************************************/
public String toUniversalString()
{
String result = "",
tempHours = "",
tempMinutes = "",
tempSeconds = "";
//if the hours is one digit in length
if ( mHours >= 1 && mHours <= 9 )
tempHours = tempHours + "0" + mHours;
//else hours occupies two digits.
else
tempHours = tempHours + mHours;
//get the proper digits for minutes.
tempMinutes = twoDigits(mMinutes);
//get the proper digits for seconds.
tempSeconds = twoDigits(mSeconds);
/* //if minutes occupies two digits
if ( mMinutes >= 0 && mMinutes <= 9 )
tempMinutes = tempMinutes + "0" + mMinutes;
//else minutes occupies two digits
else
tempMinutes = tempMinutes + mMinutes;
//if the seconds is one digit in length
if ( mSeconds >= 0 && mSeconds <= 9 )
tempSeconds = tempSeconds + "0" + mSeconds;
//else seconds occupies two digits.
else
tempSeconds = tempSeconds + mSeconds; */
//build our final string and then return.
result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds;
return result;
}//end String toUniversalString()
/*******************************************************
* A public method that copies the time input into standard
* time format. It tests the values
* for proper digit length and formats the output. Once the
* values are correctly set the method returns.
* returns a string in the 12 hour format of hh:mm:ss AM/PM.
*********************************************************/
public String toString()
{
String result = "",
tempHours = "",
tempMinutes = "",
tempSeconds = "",
amPm = "AM";
//if mHours are more than 11
if( mHours >= 12 )
{
//case: if mHours are 12.
if ( mHours == 12 )
{
amPm = "PM";
tempHours = tempHours + mHours;
}
//case: if mHours are 24
else if ( mHours == 24 )
{
tempHours = tempHours + "00";
}
//mHours are more than 11, but not 12 or 24.
else
{
amPm = "PM";
tempHours = tempHours + (mHours - 12);
}
}
//else mHours are less than 12.
else
tempHours = tempHours + mHours;
//get the proper digits for minutes.
tempMinutes = twoDigits(mMinutes);
//get the proper digits for seconds.
tempSeconds = twoDigits(mSeconds);
//build our final string and then return.
result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds
+ " " +amPm;
//we're done.
return result;
} //end String toString()
/*************************************************************
* A private method that converts any single digit
* integers to a two digit integer by adding a "0" preceeding
* the integer that was passed in as the argument.
* returns a two digit integer
*************************************************************/
private String twoDigits(int digits)
{
String tempDigits = "";
//if digits occupies two digits
if ( digits >= 0 && digits <= 9 )
tempDigits = tempDigits + "0" + digits;
//else digits occupies two digits
else
tempDigits = tempDigits + digits;
return tempDigits;
/*//if the seconds is one digit in length
if ( mSeconds >= 0 && mSeconds <= 9 )
tempSeconds = tempSeconds + "0" + mSeconds;
//else seconds occupies two digits.
else
tempSeconds = tempSeconds + mSeconds; */
}
/***************************************************************
* A public method that tests the current object against one
* that is passed in as an arguement for equality.
* returns true if they are equal and or false if they are not.
****************************************************************/
boolean equals(MyTime otherObject)
{
return ( (this.mHours == otherObject.mHours)
&& (this.mMinutes == otherObject.mMinutes)
&& (this.mSeconds == otherObject.mSeconds) );
} //end equals(MyTime otherObject)
}
Re: using the "this" method in a constructor
You don't have any constructor which has 3 parameters. When you call this(
hours, 0, 0 ); compiler looks for the constructor with 3 int parameters.
In your following code you don't have any constructor which matches the
above. You have a method public void MyTime( int hours, int minutes, int
seconds ) but this is not a constructor. Constructors won't be having any
return type not even void. If you remove the void from the above method
signature your code will be compiled.
"Mike" <MikelupoW@aol.com> wrote:
>
>When I attempt to compile the following class I get an error message
>pointing to one of my constructors stating "wrong number of arguments in
>constructor. Can anyone shed some light on what I am missing?
>Thanks.
>
>
>
>/********************************************************
> * A public class used for *
> * calculating and processing time values. *
> ********************************************************/
>
>public class MyTime
>{
> /******************************************
> * private class variables to be visible *
> * only to the methods in this class. *
> ******************************************/
> private int mHours,
> mMinutes,
> mSeconds;
>
>
>
> /***********************************************************
> * Purpose: default constructor method to set the *
> * instance var *
> * *
> *---------------------------------------------------------*
> * USAGE: MyTime.setTime () *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public MyTime()
> {
>
> mHours = 00;
> mMinutes = 00;
> mSeconds = 00;
> }
>
>
> //public constructor method
> public MyTime( int hours )
> {
> this( hours, 0, 0 ); //compiler complains here.
> }
>
>
> //old comments. ignore.
> /***********************************************************
> * Purpose: Called constructor method to set the *
> * instance var *
> * *
> *---------------------------------------------------------*
> * USAGE: setTime.setTime (int,int,int) *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public void MyTime( int hours, int minutes, int seconds )
> {
> setTime( hours, minutes, seconds );
> }
>
>
>
>
> /***********************************************************
> * Purpose: set the instance vars so the methods in MyTime *
> * can use them. *
> * setTime() accepts arguements from the *
> * calling routine. It tests the inputs for validity. *
> *if any input is invalid set the instance vars to 0. *
> *---------------------------------------------------------*
> * USAGE: MyTime.setTime ( int, int, int ) *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public void setTime( int hours, int minutes, int seconds )
> {
> //set to look like garbage.
> //Makes debugging easier.
>
> mHours = -9999;
> mMinutes = -9999;
> mSeconds = -9999;
>
> //test for valid hours input.
> if( hours < 0 || hours > 24 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the hours
> mHours = hours;
>
> //test for valid minutes input.
> if ( minutes < 0 || minutes > 59 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the minutes
> mMinutes = minutes;
>
>
> //test the seconds input
> if ( seconds < 0 || seconds > 59 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the seconds
> mSeconds = seconds;
>
> } //end of setTime
>
>
> //getHour - this method returns the object's hour value.
> public int getHour()
> {
> return mHours;
> }//end getHour()
>
>
> // getMinute - this method returns the object's minutevalue.
> public int getMinute()
> {
> return mMinutes;
> }//end getMinute()
>
>
> //getSecond - this method returns the object's second value.
> public int getSecond()
> {
> return mSeconds;
> } //end getSecond()
>
> /**********************************************************
> * A public method that converts the time input into
> * universal time format. It tests the values of the current
> * instance variables for proper digit length and
> * then formats the output. Once the
> * values are correctly set the method returns.
> *----------------------------------------------------------
> * usage: x = object.toUnversalString();
> * object is a class object.
> *----------------------------------------------------------
> * returns a string in the 24 hour format of hh:mm:ss
> ***********************************************************/
> public String toUniversalString()
> {
> String result = "",
> tempHours = "",
> tempMinutes = "",
> tempSeconds = "";
>
> //if the hours is one digit in length
> if ( mHours >= 1 && mHours <= 9 )
> tempHours = tempHours + "0" + mHours;
> //else hours occupies two digits.
> else
> tempHours = tempHours + mHours;
>
> //get the proper digits for minutes.
> tempMinutes = twoDigits(mMinutes);
>
> //get the proper digits for seconds.
> tempSeconds = twoDigits(mSeconds);
>
> /* //if minutes occupies two digits
> if ( mMinutes >= 0 && mMinutes <= 9 )
> tempMinutes = tempMinutes + "0" + mMinutes;
> //else minutes occupies two digits
> else
> tempMinutes = tempMinutes + mMinutes;
>
>
> //if the seconds is one digit in length
> if ( mSeconds >= 0 && mSeconds <= 9 )
> tempSeconds = tempSeconds + "0" + mSeconds;
> //else seconds occupies two digits.
> else
> tempSeconds = tempSeconds + mSeconds; */
>
>
> //build our final string and then return.
> result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds;
>
> return result;
>
> }//end String toUniversalString()
>
>
>
> /*******************************************************
> * A public method that copies the time input into standard
> * time format. It tests the values
> * for proper digit length and formats the output. Once the
> * values are correctly set the method returns.
> * returns a string in the 12 hour format of hh:mm:ss AM/PM.
> *********************************************************/
> public String toString()
> {
> String result = "",
> tempHours = "",
> tempMinutes = "",
> tempSeconds = "",
> amPm = "AM";
>
> //if mHours are more than 11
> if( mHours >= 12 )
> {
> //case: if mHours are 12.
> if ( mHours == 12 )
> {
> amPm = "PM";
> tempHours = tempHours + mHours;
> }
> //case: if mHours are 24
> else if ( mHours == 24 )
> {
> tempHours = tempHours + "00";
> }
> //mHours are more than 11, but not 12 or 24.
> else
> {
> amPm = "PM";
> tempHours = tempHours + (mHours - 12);
> }
> }
> //else mHours are less than 12.
> else
> tempHours = tempHours + mHours;
>
> //get the proper digits for minutes.
> tempMinutes = twoDigits(mMinutes);
>
> //get the proper digits for seconds.
> tempSeconds = twoDigits(mSeconds);
>
> //build our final string and then return.
> result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds
>+ " " +amPm;
>
> //we're done.
> return result;
>
> } //end String toString()
>
> /*************************************************************
> * A private method that converts any single digit
> * integers to a two digit integer by adding a "0" preceeding
> * the integer that was passed in as the argument.
> * returns a two digit integer
> *************************************************************/
> private String twoDigits(int digits)
> {
> String tempDigits = "";
>
>
> //if digits occupies two digits
> if ( digits >= 0 && digits <= 9 )
> tempDigits = tempDigits + "0" + digits;
> //else digits occupies two digits
> else
> tempDigits = tempDigits + digits;
>
> return tempDigits;
>
> /*//if the seconds is one digit in length
> if ( mSeconds >= 0 && mSeconds <= 9 )
> tempSeconds = tempSeconds + "0" + mSeconds;
> //else seconds occupies two digits.
> else
> tempSeconds = tempSeconds + mSeconds; */
> }
>
> /***************************************************************
> * A public method that tests the current object against one
> * that is passed in as an arguement for equality.
> * returns true if they are equal and or false if they are not.
> ****************************************************************/
> boolean equals(MyTime otherObject)
> {
> return ( (this.mHours == otherObject.mHours)
> && (this.mMinutes == otherObject.mMinutes)
> && (this.mSeconds == otherObject.mSeconds) );
> } //end equals(MyTime otherObject)
>}
Re: using the "this" method in a constructor
Mike:
I'm not sure you can "this" in that manner. I believe you should just call
the constructor again: MyTime(int,int,int). What is with the double 00
in the default constructor too? Want to just use 0?
Josh
"Mike" <MikelupoW@aol.com> wrote:
>
>When I attempt to compile the following class I get an error message
>pointing to one of my constructors stating "wrong number of arguments in
>constructor. Can anyone shed some light on what I am missing?
>Thanks.
>
>
>
>/********************************************************
> * A public class used for *
> * calculating and processing time values. *
> ********************************************************/
>
>public class MyTime
>{
> /******************************************
> * private class variables to be visible *
> * only to the methods in this class. *
> ******************************************/
> private int mHours,
> mMinutes,
> mSeconds;
>
>
>
> /***********************************************************
> * Purpose: default constructor method to set the *
> * instance var *
> * *
> *---------------------------------------------------------*
> * USAGE: MyTime.setTime () *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public MyTime()
> {
>
> mHours = 00;
> mMinutes = 00;
> mSeconds = 00;
> }
>
>
> //public constructor method
> public MyTime( int hours )
> {
> this( hours, 0, 0 ); //compiler complains here.
> }
>
>
> //old comments. ignore.
> /***********************************************************
> * Purpose: Called constructor method to set the *
> * instance var *
> * *
> *---------------------------------------------------------*
> * USAGE: setTime.setTime (int,int,int) *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public void MyTime( int hours, int minutes, int seconds )
> {
> setTime( hours, minutes, seconds );
> }
>
>
>
>
> /***********************************************************
> * Purpose: set the instance vars so the methods in MyTime *
> * can use them. *
> * setTime() accepts arguements from the *
> * calling routine. It tests the inputs for validity. *
> *if any input is invalid set the instance vars to 0. *
> *---------------------------------------------------------*
> * USAGE: MyTime.setTime ( int, int, int ) *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public void setTime( int hours, int minutes, int seconds )
> {
> //set to look like garbage.
> //Makes debugging easier.
>
> mHours = -9999;
> mMinutes = -9999;
> mSeconds = -9999;
>
> //test for valid hours input.
> if( hours < 0 || hours > 24 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the hours
> mHours = hours;
>
> //test for valid minutes input.
> if ( minutes < 0 || minutes > 59 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the minutes
> mMinutes = minutes;
>
>
> //test the seconds input
> if ( seconds < 0 || seconds > 59 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the seconds
> mSeconds = seconds;
>
> } //end of setTime
>
>
> //getHour - this method returns the object's hour value.
> public int getHour()
> {
> return mHours;
> }//end getHour()
>
>
> // getMinute - this method returns the object's minutevalue.
> public int getMinute()
> {
> return mMinutes;
> }//end getMinute()
>
>
> //getSecond - this method returns the object's second value.
> public int getSecond()
> {
> return mSeconds;
> } //end getSecond()
>
> /**********************************************************
> * A public method that converts the time input into
> * universal time format. It tests the values of the current
> * instance variables for proper digit length and
> * then formats the output. Once the
> * values are correctly set the method returns.
> *----------------------------------------------------------
> * usage: x = object.toUnversalString();
> * object is a class object.
> *----------------------------------------------------------
> * returns a string in the 24 hour format of hh:mm:ss
> ***********************************************************/
> public String toUniversalString()
> {
> String result = "",
> tempHours = "",
> tempMinutes = "",
> tempSeconds = "";
>
> //if the hours is one digit in length
> if ( mHours >= 1 && mHours <= 9 )
> tempHours = tempHours + "0" + mHours;
> //else hours occupies two digits.
> else
> tempHours = tempHours + mHours;
>
> //get the proper digits for minutes.
> tempMinutes = twoDigits(mMinutes);
>
> //get the proper digits for seconds.
> tempSeconds = twoDigits(mSeconds);
>
> /* //if minutes occupies two digits
> if ( mMinutes >= 0 && mMinutes <= 9 )
> tempMinutes = tempMinutes + "0" + mMinutes;
> //else minutes occupies two digits
> else
> tempMinutes = tempMinutes + mMinutes;
>
>
> //if the seconds is one digit in length
> if ( mSeconds >= 0 && mSeconds <= 9 )
> tempSeconds = tempSeconds + "0" + mSeconds;
> //else seconds occupies two digits.
> else
> tempSeconds = tempSeconds + mSeconds; */
>
>
> //build our final string and then return.
> result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds;
>
> return result;
>
> }//end String toUniversalString()
>
>
>
> /*******************************************************
> * A public method that copies the time input into standard
> * time format. It tests the values
> * for proper digit length and formats the output. Once the
> * values are correctly set the method returns.
> * returns a string in the 12 hour format of hh:mm:ss AM/PM.
> *********************************************************/
> public String toString()
> {
> String result = "",
> tempHours = "",
> tempMinutes = "",
> tempSeconds = "",
> amPm = "AM";
>
> //if mHours are more than 11
> if( mHours >= 12 )
> {
> //case: if mHours are 12.
> if ( mHours == 12 )
> {
> amPm = "PM";
> tempHours = tempHours + mHours;
> }
> //case: if mHours are 24
> else if ( mHours == 24 )
> {
> tempHours = tempHours + "00";
> }
> //mHours are more than 11, but not 12 or 24.
> else
> {
> amPm = "PM";
> tempHours = tempHours + (mHours - 12);
> }
> }
> //else mHours are less than 12.
> else
> tempHours = tempHours + mHours;
>
> //get the proper digits for minutes.
> tempMinutes = twoDigits(mMinutes);
>
> //get the proper digits for seconds.
> tempSeconds = twoDigits(mSeconds);
>
> //build our final string and then return.
> result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds
>+ " " +amPm;
>
> //we're done.
> return result;
>
> } //end String toString()
>
> /*************************************************************
> * A private method that converts any single digit
> * integers to a two digit integer by adding a "0" preceeding
> * the integer that was passed in as the argument.
> * returns a two digit integer
> *************************************************************/
> private String twoDigits(int digits)
> {
> String tempDigits = "";
>
>
> //if digits occupies two digits
> if ( digits >= 0 && digits <= 9 )
> tempDigits = tempDigits + "0" + digits;
> //else digits occupies two digits
> else
> tempDigits = tempDigits + digits;
>
> return tempDigits;
>
> /*//if the seconds is one digit in length
> if ( mSeconds >= 0 && mSeconds <= 9 )
> tempSeconds = tempSeconds + "0" + mSeconds;
> //else seconds occupies two digits.
> else
> tempSeconds = tempSeconds + mSeconds; */
> }
>
> /***************************************************************
> * A public method that tests the current object against one
> * that is passed in as an arguement for equality.
> * returns true if they are equal and or false if they are not.
> ****************************************************************/
> boolean equals(MyTime otherObject)
> {
> return ( (this.mHours == otherObject.mHours)
> && (this.mMinutes == otherObject.mMinutes)
> && (this.mSeconds == otherObject.mSeconds) );
> } //end equals(MyTime otherObject)
>}
Re: using the "this" method in a constructor
Oh yeh, it's the void .. that's pretty funny... :)
"Mike" <MikelupoW@aol.com> wrote:
>
>When I attempt to compile the following class I get an error message
>pointing to one of my constructors stating "wrong number of arguments in
>constructor. Can anyone shed some light on what I am missing?
>Thanks.
>
>
>
>/********************************************************
> * A public class used for *
> * calculating and processing time values. *
> ********************************************************/
>
>public class MyTime
>{
> /******************************************
> * private class variables to be visible *
> * only to the methods in this class. *
> ******************************************/
> private int mHours,
> mMinutes,
> mSeconds;
>
>
>
> /***********************************************************
> * Purpose: default constructor method to set the *
> * instance var *
> * *
> *---------------------------------------------------------*
> * USAGE: MyTime.setTime () *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public MyTime()
> {
>
> mHours = 00;
> mMinutes = 00;
> mSeconds = 00;
> }
>
>
> //public constructor method
> public MyTime( int hours )
> {
> this( hours, 0, 0 ); //compiler complains here.
> }
>
>
> //old comments. ignore.
> /***********************************************************
> * Purpose: Called constructor method to set the *
> * instance var *
> * *
> *---------------------------------------------------------*
> * USAGE: setTime.setTime (int,int,int) *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public void MyTime( int hours, int minutes, int seconds )
> {
> setTime( hours, minutes, seconds );
> }
>
>
>
>
> /***********************************************************
> * Purpose: set the instance vars so the methods in MyTime *
> * can use them. *
> * setTime() accepts arguements from the *
> * calling routine. It tests the inputs for validity. *
> *if any input is invalid set the instance vars to 0. *
> *---------------------------------------------------------*
> * USAGE: MyTime.setTime ( int, int, int ) *
> *---------------------------------------------------------*
> * does not return a value. *
> ***********************************************************/
> public void setTime( int hours, int minutes, int seconds )
> {
> //set to look like garbage.
> //Makes debugging easier.
>
> mHours = -9999;
> mMinutes = -9999;
> mSeconds = -9999;
>
> //test for valid hours input.
> if( hours < 0 || hours > 24 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the hours
> mHours = hours;
>
> //test for valid minutes input.
> if ( minutes < 0 || minutes > 59 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the minutes
> mMinutes = minutes;
>
>
> //test the seconds input
> if ( seconds < 0 || seconds > 59 )
> {
> //we fail and we set the instance vars to 0.
> mHours = 0;
> mMinutes = 0;
> mSeconds = 0;
> return;
> }
> else
> //set the seconds
> mSeconds = seconds;
>
> } //end of setTime
>
>
> //getHour - this method returns the object's hour value.
> public int getHour()
> {
> return mHours;
> }//end getHour()
>
>
> // getMinute - this method returns the object's minutevalue.
> public int getMinute()
> {
> return mMinutes;
> }//end getMinute()
>
>
> //getSecond - this method returns the object's second value.
> public int getSecond()
> {
> return mSeconds;
> } //end getSecond()
>
> /**********************************************************
> * A public method that converts the time input into
> * universal time format. It tests the values of the current
> * instance variables for proper digit length and
> * then formats the output. Once the
> * values are correctly set the method returns.
> *----------------------------------------------------------
> * usage: x = object.toUnversalString();
> * object is a class object.
> *----------------------------------------------------------
> * returns a string in the 24 hour format of hh:mm:ss
> ***********************************************************/
> public String toUniversalString()
> {
> String result = "",
> tempHours = "",
> tempMinutes = "",
> tempSeconds = "";
>
> //if the hours is one digit in length
> if ( mHours >= 1 && mHours <= 9 )
> tempHours = tempHours + "0" + mHours;
> //else hours occupies two digits.
> else
> tempHours = tempHours + mHours;
>
> //get the proper digits for minutes.
> tempMinutes = twoDigits(mMinutes);
>
> //get the proper digits for seconds.
> tempSeconds = twoDigits(mSeconds);
>
> /* //if minutes occupies two digits
> if ( mMinutes >= 0 && mMinutes <= 9 )
> tempMinutes = tempMinutes + "0" + mMinutes;
> //else minutes occupies two digits
> else
> tempMinutes = tempMinutes + mMinutes;
>
>
> //if the seconds is one digit in length
> if ( mSeconds >= 0 && mSeconds <= 9 )
> tempSeconds = tempSeconds + "0" + mSeconds;
> //else seconds occupies two digits.
> else
> tempSeconds = tempSeconds + mSeconds; */
>
>
> //build our final string and then return.
> result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds;
>
> return result;
>
> }//end String toUniversalString()
>
>
>
> /*******************************************************
> * A public method that copies the time input into standard
> * time format. It tests the values
> * for proper digit length and formats the output. Once the
> * values are correctly set the method returns.
> * returns a string in the 12 hour format of hh:mm:ss AM/PM.
> *********************************************************/
> public String toString()
> {
> String result = "",
> tempHours = "",
> tempMinutes = "",
> tempSeconds = "",
> amPm = "AM";
>
> //if mHours are more than 11
> if( mHours >= 12 )
> {
> //case: if mHours are 12.
> if ( mHours == 12 )
> {
> amPm = "PM";
> tempHours = tempHours + mHours;
> }
> //case: if mHours are 24
> else if ( mHours == 24 )
> {
> tempHours = tempHours + "00";
> }
> //mHours are more than 11, but not 12 or 24.
> else
> {
> amPm = "PM";
> tempHours = tempHours + (mHours - 12);
> }
> }
> //else mHours are less than 12.
> else
> tempHours = tempHours + mHours;
>
> //get the proper digits for minutes.
> tempMinutes = twoDigits(mMinutes);
>
> //get the proper digits for seconds.
> tempSeconds = twoDigits(mSeconds);
>
> //build our final string and then return.
> result = result + tempHours + ":" + tempMinutes + ":" +tempSeconds
>+ " " +amPm;
>
> //we're done.
> return result;
>
> } //end String toString()
>
> /*************************************************************
> * A private method that converts any single digit
> * integers to a two digit integer by adding a "0" preceeding
> * the integer that was passed in as the argument.
> * returns a two digit integer
> *************************************************************/
> private String twoDigits(int digits)
> {
> String tempDigits = "";
>
>
> //if digits occupies two digits
> if ( digits >= 0 && digits <= 9 )
> tempDigits = tempDigits + "0" + digits;
> //else digits occupies two digits
> else
> tempDigits = tempDigits + digits;
>
> return tempDigits;
>
> /*//if the seconds is one digit in length
> if ( mSeconds >= 0 && mSeconds <= 9 )
> tempSeconds = tempSeconds + "0" + mSeconds;
> //else seconds occupies two digits.
> else
> tempSeconds = tempSeconds + mSeconds; */
> }
>
> /***************************************************************
> * A public method that tests the current object against one
> * that is passed in as an arguement for equality.
> * returns true if they are equal and or false if they are not.
> ****************************************************************/
> boolean equals(MyTime otherObject)
> {
> return ( (this.mHours == otherObject.mHours)
> && (this.mMinutes == otherObject.mMinutes)
> && (this.mSeconds == otherObject.mSeconds) );
> } //end equals(MyTime otherObject)
>}