#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5

    Curious question


    Note: The following uses code from Algorithms, 4th Edition
    by Robert Sedgewick and Kevin Wayne, but the challenge is not from the textbook. It isn't homework, but rather a challenge question.

    Modify Stopwatch data type such that it is comparable by
    elapsed time, supports creation of a Stopwatch object with a String id, and provides a toString() method that returns a String representation (id and elapsed time) of a Stopwatch object.

    Code:
    public class Stopwatch 
    {
     private final long start;
     public Stopwatch()
     { start = System.currentTimeMillis(); }
     public double elapsedTime()
     {
     long now = System.currentTimeMillis();
     return (now - start) / 1000.0;
     }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Hey dude,

    I read your post, but I don't see any questions in it. So, I will give a couple hints for you to answer it.

    • Take a look at the comparable interface (http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html)
    • Info about instance variables (http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html)
    • Tutorial on writing a toString method (http://www.tutorialspoint.com/java/number_tostring.htm)


    Hope that helps!

    Cheers, Cam.

IMN logo majestic logo threadwatch logo seochat tools logo