Classes and Objects: Build a watch Stopwatch

Question: Design a class named StopWatch . The class contains:
o Private data fields startTime and endTime with getter methods.
o A no-arg constructor that initializes startTime with the current time.
o A method named start() that resets the startTime to the current
time.
o A method named stop() that sets the endTime to the current time.
o A method named getElapsedTime() that returns the elapsed time for
the stopwatch in milliseconds.
Write a test program that measures the execution time of sorting 100,000 numbers using
selection sort.

package Ch9;

import java.util.Date;

/**
 * Q: Design a class named StopWatch . The class contains:
     o Private data fields startTime and endTime with getter methods.
     o A no-arg constructor that initializes startTime with the current time.
     o A method named start() that resets the startTime to the current
     time.
     o A method named stop() that sets the endTime to the current time.
     o A method named getElapsedTime() that returns the elapsed time for
     the stopwatch in milliseconds.
     Write a test program that measures the execution time of sorting 100,000 numbers using
     selection sort.
 * Created by aarushi on 17/6/21.
 */
public class Ex06Stopwatch {

    private long startTime;
    private long endTime;

    public Ex06Stopwatch(){
        startTime= new Date().getTime();
    }

    public long getStartTime() {
        return startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public void start(){
        startTime= new Date().getTime();
    }

    public void stop(){
        endTime= new Date().getTime();
    }

    public long getElapsedTime(){
        return (endTime-startTime);
    }
}
package Ch9;

import java.util.Random;

/**
 * Created by aarushi on 19/6/21.
 */
public class Ex06StopwatchTest {
    public static void main(String[] args){
        Ex06Stopwatch stopwatch= new Ex06Stopwatch();
        Random random= new Random(5);
        int[] array= new int[100000];

        for(int i=0; i<100000; i++) {
            array[i] = random.nextInt(100);
        }

        stopwatch.start();
        selectionSort(array);
        stopwatch.stop();

        System.out.println("Time taken to sort an integer array of 100000 integers is " + stopwatch.getElapsedTime() + " milliseconds");

    }

    public static void selectionSort(int[] array){
        int index=0;
        for(int i=0; i<100000; i++) {
            int small = array[i];
            for (int j = index; j < 100000; j++) {
                if (array[j] > array[small]) {
                    small = j;
                }
            }

            if (small != i) {
                int temp = array[i];
                array[i] = array[small];
                array[small] = temp;
            }
            index++;
        }

    }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: