wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Programming Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Programming Class" Watch "Programming Class" New topic
Author

Programming Class

Tuff Luff
Greenhorn

Joined: Apr 11, 2010
Posts: 4
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:
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to the Ranch!

"main()" has to be static -- "public static void main(String[] args)". So you're very, very close!


[Jess in Action][AskingGoodQuestions]
Tuff Luff
Greenhorn

Joined: Apr 11, 2010
Posts: 4
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

James Echternach wrote: The outputs are always 0


Integer division always gives an integer answer, so (for example) 2 / 3 is 0. If one of the arguments is floating-point, though, then that's floating-point division, which gives floating-point results; i.e., 2.0 / 3.0 is 0.666666666...7 . I think that's generally what you're seeing, although I confess I didn't examine the code too closely.

I see the instructions say something about keeping the integer part, and throwing away the decimal part. This suggests that you do the math as floating point, and then cast the result to an int or long.
Tuff Luff
Greenhorn

Joined: Apr 11, 2010
Posts: 4
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.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi James,

You're right to suspect that this is wrong. You want to call those methods not on a new StopWatch, but on "this" stopwatch -- the one that howMuchTime() was called on. When you're writing an instance method in a class (howMuchTime() is an instance method) you can call any other instance method in that same class, on "this" object, simply by calling the method, without qualification: i.e.,



Since we're here, I'll point out that it would be considered good style to just call getDiffTime() once, save the result in a variable, and then pass that variable as the argument to the three other methods.
Tuff Luff
Greenhorn

Joined: Apr 11, 2010
Posts: 4
Nice point, sir. I'm trying to keep my code as readable and straightforward as possible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programming Class