954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

last section of code has to a static class

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;
}

Bill T
Newbie Poster
14 posts since Jul 2004
Reputation Points: 10
Solved Threads: 1
 

Ok dude, first you need to use the [*code] and [/code] tags...

// 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.

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

TY very much it works great.

Bill T
Newbie Poster
14 posts since Jul 2004
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You