I have a stopwatch program and the last of the code has to be a static class
can someone help running out of time.

I've tried all i know examples aren't helping.
Here is the code I've written.
I hope someone can help. TY...

// import java.io package and utility packages


import java.io.*;


import java.util.*;



class Stopwatch{



public void start()  {
GregorianCalendar now = new GregorianCalendar();
startTime = now.getTimeInMillis();
}


public void showElapsedTime() {
GregorianCalendar now = new GregorianCalendar();
long currTime = now.getTimeInMillis();
long difference = (currTime - startTime)/1000;
System.out.print(difference);
System.out.println("  seconds have elapsed.");
}


//declared outside of all methods


private long startTime;
}


class Irritating_stopwatch {



public static void main(String arg[]) throws Exception {
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));


Stopwatch timer = new Stopwatch();
Counter countDown = new Counter();


for (int i=9; i>=0; i--){


System.out.println("Hit the Enter key to Start timer");
keyboard.readLine();
timer.start();


System.out.println("Hit the Enter key to Stop timer");
keyboard.readLine();
timer.showElapsedTime();


countDown.decrease();
System.out.print(countDown.getValue());
System.out.println(" More Tries");
System.out.println();


System.out.println("Hit the Enter key to initialize timer");
keyboard.readLine();
}



System.out.println("Due to planned obsolescence, this Stopwatch is no longer operational");
System.out.println();


}
}


class Counter {


public void decrease() {
callCount --;
}public int getValue() {
return callCount;
}
private int callCount = 10;
}

Recommended Answers

All 2 Replies

// import java.io package and utility packages
import java.io.*;
import java.util.*;

class Stopwatch{ 

    //declared outside of all methods
    private long startTime;
    public void start() {
        GregorianCalendar now = new GregorianCalendar();
        startTime = now.getTimeInMillis();
    }

    public void showElapsedTime() {
        GregorianCalendar now = new GregorianCalendar();
        long currTime = now.getTimeInMillis();
        long difference = (currTime - startTime)/1000;
        System.out.print(difference);
        System.out.println(" seconds have elapsed.");
    }
}

class Irritating_stopwatch {
    public static void main(String arg[]) throws Exception {
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
        Stopwatch timer = new Stopwatch();
        for (int i=9; i>=0; i--){
            System.out.println("Hit the Enter key to Start timer");
            keyboard.readLine();
            timer.start();
            System.out.println("Hit the Enter key to Stop timer");
            keyboard.readLine();
            timer.showElapsedTime();
            Counter.decrease();
            System.out.print(Counter.getValue());
            System.out.println(" More Tries");
            System.out.println();
            System.out.println("Hit the Enter key to initialize timer");
            keyboard.readLine();
        }
        System.out.println("Due to planned obsolescence, this Stopwatch is no longer perational");
        System.out.println();
    }
}

class Counter {
    private static int callCount = 10;

    public static void decrease() {
        callCount --;
    }

    public static int getValue() {
        return callCount;
    }
}

Basically, just instead of creating an instance of Counter, you just call the methods direct... Counter.decrease(); and so on and so forth.

TY very much it works great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.