My teacher assigned me to do a program that :
Write an application to convert into hours, minutes, seconds, a time originally expressed in seconds. The only operations allowed are addition and subtraction. (3813 seconds become 1 hour, 3 min, 33 sec.).
I just wanted to know if I did it correctly.
Here is my code :
Note : I just put a little part of the frame code
I just wanted to make sure I did as the teacher asked and maybe you guys can give me tips on my code or an easier way to do it.
Thank you everyone !
There are few more minor things I would do:
* I don't think the method check(int) needs to be public. I'd make it private.
* Constants like HOUR are usually created like this: I would also change the name to SECONDS_PER_HOUR.
* Indentation isn't quite right at lines 39-41.
* I don't think the variable counter needs to be a field; a local variable is fine.
Day Vide wrote:I don't have to initialize it in the constructor or in any other method ?
Final fields can be initialized in a constructor, an initialization block, or where they are declared, as Knute showed.
There are different ways to do it because they convey different design choices.
HOUR, SECOND, and MINUTE are what we call symbolic constants and are declared as static final and initialized as Knute showed. Their values are fixed and never change no matter how many instances of the enclosing class are created. Initializing them in your constructor can actually be misleading to other programmers because initializing final fields in a constructor indicates that they are designed to be immutable properties. That is, once you create the object and assign value(s) to the final field(s), you don't want anything to be able to change those values for that instance.
One example from the standard library is the Integer wrapper class. To create an instance of an Integer that represents 100, you would do this: new Integer(100). This Integer object cannot be changed to represent a different integer value, i.e. the object is immutable.
Another example might be for a Person class that had dateOfBirth, name, and height properties. It might be reasonable to design the Person class so that you had to provide the values for dateOfBirth and name when you create instances and they could not be changed. For height, well, that changes over time so you wouldn't want to make it an immutable property, so you wouldn't declare it final, even though you could still require its initial value to be provided to the constructor so that each new instance would be guaranteed to have a known value for height.
One of the first things I look at is semantics. What does client code look like? Does it make sense? Does it tell a coherent story? Here's some test code that uses your ConversionTemps class:
Then I try to explain what this code is doing in plain English and see if it makes sense. It might go something like this:
1. I create a new ConversionTemps object and call it time. I pass the number of seconds to convert to the constructor.
2. I call the secondsCalculate() method to ... calculate how many hours, minutes, and seconds there are in the time object.
3. I call the getSecondes() method to get the number of seconds.
I read that description and the code and try to find things that don't make sense. Here are a few questions that I would ask:
1. Is the name "ConversionTemps" really a good name for this class? Does it reflect what this class is supposed to do? Why "temps"? Don't most people read "temps" and think either "temporary" or "temperature"? But were dealing with time here, so why "temps"?
2. If the constructor takes a number representing seconds, then why would I expect getSecondes() to return a different number? Does it make sense?
3. This class won't work if I don't call the secondsCalculate() method before I call any of the getXXX() methods. Why make the client code call this method first? That's not a very friendly API nor is it intuitive.
Here's another point to consider: Looking more closely at the code, the secondsCalculate() method won't work if the calls to they check() method are not made in that specific order. This is very subtle and while the chances may be remote of it happening, any change to the order of those calls will break the code. That's not really a good thing.