Tuff Luff

Greenhorn
+ Follow
since Apr 11, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tuff Luff

Nice point, sir. I'm trying to keep my code as readable and straightforward as possible.
14 years ago
Ah thanks again. This is starting to make more sense.

Right now I have this as my code:


My only problem seems to be the howMuchTime() method. I'm not sure if I'm doing it quite right, but it was the only way I could think of getting that method running. However, I don't think I should have to declare the sw object in order to run the getDiff method. Anyway, the outputs are always the same, 0, so I must be doing something not quite right still.

14 years ago
Thanks very much, Ernest!

Now the program runs, but it doesn't do what it's supposed to do. I just need to work on my methods howManyHours, howManyMinutes, and howManySeconds. Do you know what I'm doing wrong? The outputs are always 0, so I'm pretty sure I'm doing something very wrong.
14 years ago
Hi I'm new to the forums and I'm looking forward to learning Java inside and out,

I'm in a programming class and we're on our second assignment. I thought I was doing alright, but for some reason my code won't compile. I get an error that says I have no main method (or something like that), but I do!

I'm also not completely sure if my methods are all correct. The three where I have to convert from milliseconds to hours, minutes, and seconds I'm pretty sure aren't working correctly. Any help would be much appreciated.

Here are my instructions:

# 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());
}





and here is my code:
14 years ago