Jack
Jack

Reputation: 497

Timer to measure array sort

Program to create a timer, and use it to measure how long it takes to create and sort an array. I'm getting red highlights in eclipse. Further explanation in comments.

public class StopWatch {        
    private double startTime;       
    private double endTime;

    public void start() {
        startTime = System.currentTimeMillis();
    }

    public void stop() {
        endTime = System.currentTimeMillis();
    }

    public double getElapsedTime(endTime, startTime) {
        return endTime - startTime;
    }
}

Test class

import java.util.Arrays;

public class TestStopWatch {

    Stopwatch.start();

    int[] array = new int[1000000];
    {
        for (int i = 0; i < 999999; i++) 
        {
            array[i] = (int) Math.random() * 100;
        }

        Arrays.sort(array);
    }

    Stopwatch.stop();
    System.out.println(getElapsedTime());
}

Upvotes: 1

Views: 718

Answers (4)

Am_I_Helpful
Am_I_Helpful

Reputation: 19158

You can't call start(),stop, and getElapsedTime() methods directly in the TestStopWtach class. You need to have a StopWatch Object to call these methods in different class.

There must be a public static void main(String args[]) method or some other method in the TestStopWatch Class over which you be calling these things...

Also, you need to invoke method calls using StopWatch Class object...

Next,you need to take care that both these classes reside under the same package to call the methods of them as shown below in the code...

The StopWatch class must look like :-

public class StopWatch {

    private double startTime;

    private double endTime;

    public long start() {

        startTime = System.currentTimeMillis();

        return startTime;
    }

    public long stop() {

        endTime = System.currentTimeMillis();

        return endTime;

    }

    public double getElapsedTime(endTime, startTime) {
        return endTime - startTime;
    }
}

Also, make some changes in StopWatch class by changing the return-type of the methods start() and stop().

The TestStopWatch class must look like something :-

import java.util.Arrays;

public class TestStopWatch {
    public static void main(String args[]){

        StopWatch sw=new StopWatch();

        long p=sw.start(); // trying to call the start method in the previous class

        // code to create and sort array

        long q=sw.stop(); //stop method in previous class

        // call getElapsedtime and print
        System.out.println(sw.getElapsedTime((double)q,(double)p));

    }
}

Upvotes: 2

Michael Goldstein
Michael Goldstein

Reputation: 618

First, in your TestStopWatch class, you should make a main method like this

public void main(String[] args) { 
    // your code here
}

Then, you need to instantiate your StopWatch class like this StopWatch stopWatch = new StopWatch(); and call stopWatch.start(), stopWatch.stop() and stopWatch.getElapsedTime() respectively. Also, you should remove the two parameters from getElapsedTime as they aren't being used :)

Somewhat unrelated, your StopWatch class itself has a couple minor problems. First, there's no need to store startTime and endTime as doubles. Since they're integers, the best way to store them is in an integer format (in this case as a long).

Also, System.currentTimeMillis() is used for time (as in the time and date) and should not be used for this sort of stopwatch because the system will regularly make corrections to System.currentTimeMillis() so the clock is more accurate. Instead, use System.nanoTime() which doesn't have these sorts of corrections.

Upvotes: 1

kriyeta
kriyeta

Reputation: 705

first of all, you create a method inside 'TestStopWatch' and then call other class method's by creating object.

public class StopWatch {

    private double startTime;

    private double endTime;

    public double start() {

        startTime = System.currentTimeMillis();
        return startTime;

    }

    public double stop() {

        endTime = System.currentTimeMillis();
        return endTime;

    }

    public double getElapsedTime(double endTime, double startTime) {
        return endTime - startTime;
    }

}

chnges in second class

import java.util.Arrays;

public class TestStopWatch {

    public void measureTime(){

    StopWatch stopWatch = new StopWatch();

    double startTime = stopWatch.start(); //trying to call the start method in the previous class

    //code to create and sort array

        double endTime = stopWatch.stop(); //stop method in previous class

    System.out.println(getElapsedTime(startTime,endTime)); //call getElapsedtime and print

    }
}

Upvotes: 1

marcelv3612
marcelv3612

Reputation: 663

This will fix it:

TestStopWatch class changes:

public static void main(String[] args) {
    StopWatch watch = new StopWatch();

    watch.start();

    watch.stop();

    System.out.println(watch.getElapsedTime());
}

StopWatch class changes:

public double getElapsedTime() {
    return endTime - startTime;
}

Upvotes: 1

Related Questions