Stop Watch Class

Hello ladies and gents,

I am attempting to make a basic stop watch program that.

Here's the code:

String[] animals;

void setup() {
  StopWatch.start();

  animals = new String[3];
  animals[0] = "Cat";
  animals[1] = "Dog";
  animals[2] = "Turtle";

  StopWatch.stop();
  print(StopWatch.time());

}

//void bubbleSort (String[] ar) {
//  for (int i = 0; i < ar.length-1; i++) {
//    for (int j = 0; j < ar.length-1; j++) {
//      if (ar[j].compareTo(ar[j+1]) > 0) {
//        swap (ar, j, j+1);
//        // you need a swap method
//      }
//    }
//  }
//}

class StopWatch {
  long startTime, endTime;

  void start () {
    startTime = millis();

  }

  void stop() {
    endTime = millis();
  }

  double time() {
    return endTime - startTime;
  }

  void reset() {
    startTime = 0;
    endTime = 0;
  }

}

I get the error: Cannot make a static reference to the non-static method start() from the type SSE.StopWatch on line 4 of the main window.

Tagged:

Answers

  • since it's class

    you need

    StopWatch watch = new StopWatch();

    ?

  • or so

    String[] animals;
    
    StopWatch watch = new StopWatch();
    
    void setup() {
    
      watch.start();
    
      size(10, 10);
    
      for (int i = 0; i <19000;i++) {
        animals = new String[3];
        animals[0] = "Cat";
        animals[1] = "Dog";
        animals[2] = "Turtle";
      }
    
    
      watch.stop();
      print(watch.time());
    }
    
    //void bubbleSort (String[] ar) {
    //  for (int i = 0; i < ar.length-1; i++) {
    //    for (int j = 0; j < ar.length-1; j++) {
    //      if (ar[j].compareTo(ar[j+1]) > 0) {
    //        swap (ar, j, j+1);
    //        // you need a swap method
    //      }
    //    }
    //  }
    //}
    
    class StopWatch {
      double startTime=0, endTime=0;
    
      void start () {
        startTime = millis();
      }
    
      void stop() {
        endTime = millis();
      }
    
      double time() {
        return (endTime - startTime);
      }
    
      void reset() {
        startTime = 0;
        endTime = 0;
      }
    }
    
  • edited April 2014

    Tweaked version: :D

    //forum.processing.org/two/discussion/4824/stop-watch-class
    
    String[] animals = {
      "Cat", "Dog", "Turtle"
    };
    
    void setup() {
      StopWatch.start();
      println(animals);
      StopWatch.stop();
    
      println("\nNanoseconds:  " + StopWatch.time());
      println("Milliseconds: " + nf(StopWatch.time()/1e6, 0, 4));
      println("Seconds:\t  " + nf(StopWatch.time()/1e9, 0, 3));
      exit();
    }
    
    static final class StopWatch {
      static long startTime, endTime;
    
      static void start() {
        startTime = System.nanoTime();
      }
    
      static void stop() {
        endTime = System.nanoTime();
      }
    
      static long time() {
        return endTime - startTime;
      }
    
      static void reset() {
        startTime = endTime = 0;
      }
    }
    

    Also take a look at this thread:
    http://forum.processing.org/two/discussion/1725/millis-and-timer

  • edited April 2014

    Same tweaked version. But w/ currentTimeMillis() in place of nanoTime():

    //forum.processing.org/two/discussion/4824/stop-watch-class
    
    String[] animals = {
      "Cat", "Dog", "Turtle"
    };
    
    void setup() {
      StopWatch.start();
      println(animals);
      StopWatch.stop();
    
      println("\nMilliseconds:  " + StopWatch.time());
      println("Seconds:\t  " + nf(StopWatch.time()/1e3, 0, 3));
      exit();
    }
    
    static final class StopWatch {
      static long startTime, endTime;
    
      static void start() {
        startTime = System.currentTimeMillis();
      }
    
      static void stop() {
        endTime = System.currentTimeMillis();
      }
    
      static long time() {
        return endTime - startTime;
      }
    
      static void reset() {
        startTime = endTime = 0;
      }
    }
    
  • Thanks for the suggestions everybody! I can't believe I missed actually creating the Stop Watch. The things you miss when looking at your own code.

  • edited April 2014

    I can't believe I missed actually creating the StopWatch.

    Yup, you've forgotten to instantiate the class 1st!.
    However, in my tweaked versions, that's not needed at all! \m/

  • How does the may you filled the array with Strings work vs the way I did it? Also, what's the difference between a string and an array of Strings? A normal String would just hold the three terms while the Array would hold each String as an element?

    startTime = endTime = 0;

    Does this just say startTime is = to the value of endTime - which is = 0, and therefore they both = 0?

  • edited May 2014

    http://processing.org/reference/Array.html
    http://processing.org/reference/arrayaccess.html
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

    A normal String would just hold the three terms while the array would hold each String as an element?

    I don't get what you mean, sorry!
    A String object has a char[] field internally, which all of its char values are actually hold.
    While an array object provides a fixed # of slots to store values of a single data-type.

    Does this just say startTime is = to the value of endTime - which is = 0, and therefore they both = 0?

    http://processing.org/reference/assign.html

    The assignment operator = --- http://processing.org/reference/assign.html
    diff. from other operators, evaluates from right to left!

    So in startTime = endTime = 0;, 1st 0 is assigned to endTime, and returns that very same value.
    That returning evaluated value, which is 0, is finally assigned to startTime too via the 2nd = operator.
    Now all of them is 0 in the end! (*)

  • edited May 2014

    Does the char function of a String access the character at that location or the whole word?

    How does assigning values to the Array of Strings using {"Thing1", "Thing2"} work vs Stringname[#] = "Thing1" Stringname[#] = "Thing2" etc

    Can the values still be accessed like normal for an Array when using option 1?

  • There's no method named char in the String class!
    I said that a String object has a field of type char[]!

    See its available methods for yourself:
    http://docs.oracle.com/javase/8/docs/api/java/lang/String.html

    Perhaps you're actually interested in the charAt() method:
    http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-

  • Ok.

    My main concern now is the difference between

    String[] animals = {
      "Cat", "Dog", "Turtle"
    };
    

    and

    String[] animals[#] = ("Things");

    Also, why does the former not require new ?

  • By reading this:

    Use the above “some methods" to create and show a list of strings. The list length should be a variable so that you can easily change its length. Create a Timer object and test it by creating, showing, and sorting your list with the given bubble sort (or selection sort). You will need to write a “swap" method to implement the sort(s).

    would you say that I should be actually creating a String array at random?

  • edited May 2014

    Also, why does the former not require new?

    It's 1 of 2 Java's special instantiations w/o new. The other is called string literal.
    Read the links I've posted to know further!

    The list length should be a variable so that you can easily change its length.
    Would you say that I should be actually creating a String array at random?

    In order to create the structure, you're gonna need actual data elements.
    To find a bunch of it, perhaps you're gonna need some text file and pick a specified # of random words outta it?
    Or even from a URL?

  • So do you even need the new when making an array of String?

  • So do you even need the new when making an array of String?

    I believe you're mixing up array & String data-types. :-?

    The {} w/o new is a special alternative array object instantiation.
    And "" w/o new is the same thing, but for String objects!

  • edited May 2014 Answer ✓
    String[] animals = {
      "Cat", "Dog", "Turtle"
    };
    

    The {} is instantiating the array, whose reference is about to be stored in variable animals.
    Now, each of those "" is a String object instantiation. And the wrapped char values are stored in them.
    And finally the just instantiated String objects' references are stored in the array's indexed "slots"!

  • I may be mixing them up, but aren't I making an Array of Strings in my code?

  • edited May 2014 Answer ✓

    Yes you are: String[] is an array type which stores references for String objects! :-B

Sign In or Register to comment.