This discussion is archived
8 Replies Latest reply: Mar 11, 2008 1:48 PM by 807601 RSS

Help with toString method

807601 Newbie
Currently Being Moderated
I am in my first Java class ever, so I'm hoping this should be easy to answer.

I am calculating race times with a method our curriculum likes to call getDiff. The implicit parameter is the start time, and the explicit parameter is the end time. I have figured out the calculation. The method returns a new object for the Time class, but the way my instructor told me to do it, it doesn't have a name. It just returns a new object. The problem I'm having is pulling that new information into the toString method. How do I reference the results from the other method to put that new data into a String?

Here's the text from the getDiff method:

public Time getDiff(Time endTime)
{
// <your code here>
// null is not the correct value to return
int hourConvert = getHour() * 3600;
int minutesConvert = getMinutes() * 60;
int timeConversion = hourConvert + minutesConvert + getSeconds();

int hourConvert1 = endTime.getHour() * 3600;
int minutesConvert1 = endTime.getMinutes() * 60;
int timeConversion1 = hourConvert1 + minutesConvert1
+ endTime.getSeconds();

if (timeConversion1 < timeConversion)
{
timeConversion1 =+ 86400;
}
int getDiff = timeConversion1 - timeConversion;
int raceHour = getDiff / 3600;
getDiff = getDiff - raceHour * 3600;
int raceMinutes = getDiff / 60;
int raceSeconds = getDiff - raceMinutes * 60;
return new Time(raceHour, raceMinutes, raceSeconds);
}

can anyone help?

Edited by: nodirtyrockstar on Mar 11, 2008 12:09 PM

Edited by: nodirtyrockstar on Mar 11, 2008 12:17 PM
  • 1. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    The Time class has a toString method. Simply call it, or, if you are displaying it on the console, System.out.println(myTime);
  • 2. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    Well, unfortunately, I don't think that is how they want us to do it. We are supposed to extract each variable, raceHour, raceMinutes, and raceSeconds, and put it into the time format HH:MM:SS. Seems simple enough? Well, we are also supposed to add leading zeros if any of these values is less than 10. Therefore, we must do a series of if/else statements to ensure that this occurs when it needs to. I have sketched out what these will look like, but the problem I'm having is still how to reference the race time generated with the getDiff method. Here is the sample code I'm working on below. Please keep in mind that we are not supposed to change the names of the methods, or types, parameter types, etc.

    /**
    * format a String with the time as HH:MM:SS.
    *
    * @return String The time in HH:MM:SS format.
    *
    */
    public String toString()
    {

    if (raceHour < 10)
    {
    diffTime = "0" + raceHour + ":";
    }
    else
    {
    diffTime = raceHour + ":";
    }
    if (raceMinutes < 10)
    {
    diffTime = diffTime + "0" + raceMinutes + ":";
    }
    else
    {
    diffTime = diffTime + raceMinutes + ":";
    }
    if (raceSeconds < 10)
    {
    diffTime = diffTime + "0" + raceSeconds + ":";
    }
    else
    {
    diffTime = diffTime + raceSeconds + ":";
    }
    return diffTime;
    }
    }
  • 3. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    What am I missing here. Run the following code and tell me why it's not leading you where you wanna go?
    import java.sql.Time;
    
    public class TimeTime {
      public static void main(String[] argv) {
        Time t = new Time(7, 47, 11);
        System.out.println(t);
      }
    ~Bill
  • 4. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    nodirtyrockstar wrote:
    Well, unfortunately, I don't think that is how they want us to do it. We are supposed to extract each variable, raceHour, raceMinutes, and raceSeconds, and put it into the time format HH:MM:SS. Seems simple enough? Well, we are also supposed to add leading zeros if any of these values is less than 10. Therefore, we must do a series of if/else statements to ensure that this occurs when it needs to.
    The code looks like it will do that, but you should use a new variable to build the return value, instead of using the member variable diffTime.
    I have sketched out what these will look like, but the problem I'm having is still how to reference the race time generated with the getDiff method. Here is the sample code I'm working on below. Please keep in mind that we are not supposed to change the names of the methods, or types, parameter types, etc.
    So you want to print out the Time you get back when you call diffTime()? Create a Time reference, and store the result of diffTime() there. Then call toString() on it to get the String representation, and then print out that String.
  • 5. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    I am not sure I understand why you're telling me to change my variable. Also, I don't know exactly what you mean by create a time reference. Where do I create that? In the getDiff method? or in the toString method. More importantly, how do I get the results from the getDiff method to be stored as something I can reference in the toString method?
  • 6. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    abillconsl wrote:
    What am I missing here. Run the following code and tell me why it's not leading you where you wanna go?
    import java.sql.Time;
    
    public class TimeTime {
    public static void main(String[] argv) {
    Time t = new Time(7, 47, 11);
    System.out.println(t);
    }
    ~Bill
    I am not supposed to change any of the sample code. We are given a program with errors, or missing pieces, and the method is not asking us to use the toString method of the time class. It is trying to get us to extract the raceTime from the getDiff method, and then format it as a string. I do not have the option to do what you are suggesting. Thank you anyway for your help, though!
  • 7. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    nodirtyrockstar wrote:
    I am not sure I understand why you're telling me to change my variable. Also, I don't know exactly what you mean by create a time reference. Where do I create that? In the getDiff method? or in the toString method. More importantly, how do I get the results from the getDiff method to be stored as something I can reference in the toString method?
    I dare you to try the code that I posted just above - no, I double dare you.
  • 8. Re: Help with toString method
    807601 Newbie
    Currently Being Moderated
    nodirtyrockstar wrote:
    abillconsl wrote:
    What am I missing here. Run the following code and tell me why it's not leading you where you wanna go?
    import java.sql.Time;
    
    public class TimeTime {
    public static void main(String[] argv) {
    Time t = new Time(7, 47, 11);
    System.out.println(t);
    }
    ~Bill
    I am not supposed to change any of the sample code. We are given a program with errors, or missing pieces, and the method is not asking us to use the toString method of the time class. It is trying to get us to extract the raceTime from the getDiff method, and then format it as a string. I do not have the option to do what you are suggesting. Thank you anyway for your help, though!
    You posted that just as I posted my response - so forget that last one of mine. Ok, you are not allowed to just call the Time classes toString() method.

    Ok, then go to your src.zip file in your JDK folder and look for the Time class; when yo ufind it open it and read the toString() method - that should help you out.