# Start a new Project. Name it anything you want. Start a new Class for that project named StopWatch. (It must be named StopWatch with capital S and W and the rest lowercase.) Eclipse will create a file named StopWatch.java and put your source code there.
# INSTANCE FIELDS
* Your StopWatch class must have these instance fields. Be sure to make them private and type long.
startTime, stopTime, diffTime
* Your StopWatch class must have this instance field. Be sure to make it private and type boolean.
running
This variable will tell you if the stopwatch is running or not.
# CONSTRUCTORS
* Your StopWatch class must have a default constructor <- a constructor that expects no parameters.
This constructor's job is to initialize instance fields and class variables.
Initialize all the longs to 0. Initialize running to false.
* Your StopWatch class must have a constructor that expects a boolean.
It should initialize stopTime and diffTime to 0.
It should initialize startTime to System.currentTimeMillis().
It should initialize running to equal the parameter boolean. For example, if the parameter is named b, then running = b;
* Your StopWatch class must have a constructor that expects a long and then a boolean.
It should initialize stopTime and diffTime to 0.
It should initialize startTime to equal the parameter long.
It should initialize running to equal the parameter boolean.
# GETTERS
* Write a getter for diffTime. (Its name must be getDiffTime)
* Write a getter for startTime. (Its name must be getStartTime)
* Write a getter for running. (Its name must be getRunning)
# SETTERS
* Write a setter for diffTime. (Its name must be setDiffTime)
# OTHER METHODS
* Write a method named howManyHours. This method accepts a long parameter and returns a long. The long parameter is considered to be in the unit milliseconds. (1000 ms = 1 s)
howManyHours returns how many hours the parameter represents. Since it returns a long, we just want the integer part and throw away any decimal amount. You need to figure out to do this calculation.
Hint: 10,000,000 milliseconds is just 2 hours. Don't round up.
* Write a method named howManyMinutes. This method accepts a long parameter and returns a long. The long parameter is considered to be in the unit milliseconds. (1000 ms = 1 s)
howManyMinutes returns how many minutes the parameter represents. Since it returns a long, we just want the integer part and throw away any decimal amount. You need to figure out to do this calculation.
Hint: 10,000,000 milliseconds is 166 minutes. Don't round up.
* Write a method named howManySeconds. This method accepts a long parameter and returns a long. The long parameter is considered to be in the unit milliseconds. (1000 ms = 1 s)
howManySeconds returns how many seconds the parameter represents. Since it returns a long, we just want the integer part and throw away any decimal amount.
Hint: 10,000,100 milliseconds is 10000 seconds. Don't round up.
* Write a method named pressButton. This method does not return anything and it accepts no parameters. This method checks to see if this stopwatch is running. If it is, it stops it and also sets stopTime equal to System.currentTimeMillis(). Also calculate diffTime by subtraction startTime from stopTime.
If this stopwatch is not running, then start it. Set startTime equal to System.currentTimeMillis() and set diffTime equal to 0.
* Write a method named pause. This method does not return anything and it accepts no parameters. This method checks to see if this stopwatch is running. If it is, it sets diffTime equal to System.currentTimeMillis() minus the startTime.
If it is not running, then set diffTime equal to 0.
* Write a method named howMuchTime. This method returns a String and accepts no parameters. This method returns a String in the following format to tell you how many hours, minutes, seconds represent diffTime. For example, if diffTime equals 10,000,500 this method returns the String
2:46:40
# Here is a main method you may use to test your code a little. You should modify this main method by adding more tests. This main method doesn't test your code thoroughly. You can put this main method in a class by itself (best) or in the StopWatch class (not as good).
public static void main(String[] args) {
// This main doesn't test pause, the other constructors, getRunning, ...
StopWatch sw = new StopWatch();
sw.pressButton();
/*
This won't compile until you put
import javax.swing.JOptionPane;
at the top of this file--above public class StopWatch
*/
JOptionPane.showConfirmDialog(null, "Click OK to stop this stopwatch", "StopWatch", JOptionPane.PLAIN_MESSAGE);
sw.pressButton();
System.out.println(sw.howMuchTime());
JOptionPane.showConfirmDialog(null, "Click OK to start this stopwatch", "StopWatch", JOptionPane.PLAIN_MESSAGE);
sw.pressButton();
JOptionPane.showConfirmDialog(null, "Click OK to stop this stopwatch", "StopWatch", JOptionPane.PLAIN_MESSAGE);
sw.pressButton();
System.out.println("Hours: "+sw.howManyHours(sw.getDiffTime()));
System.out.println("Minutes: "+sw.howManyMinutes(sw.getDiffTime()));
System.out.println("Seconds: "+sw.howManySeconds(sw.getDiffTime()));
sw.setDiffTime(10000500);
System.out.println(sw.howMuchTime());
}
James Echternach wrote: The outputs are always 0
No holds barred. And no bars holed. Except this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|