• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Designing Classes CONFUSES Me

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!

I'm in my first year of coding, and I try to read everything, but I retain nothing when it comes to creating code which has been a very annoying problem.



Here are the items the class is supposed to contain and where I think I created them:
The data fields hour, minute, and second that represent a time.
Lines 16 - 20. I added milliseconds so I could convert the System.currentTimeMillis() to hours, seconds and minutes later
A no-arg constructor that creates a Time object for the current time. (The values of the data fields will represent the current time)
Lines 22-23
A constructor that constructs a Time object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time)
Lines 25-27
A constructor that constructs a Time object with the specified hour, minute, and second.
Lines 29-33
Three getter methods for the data fields hour, minute, and second, respectively.
Lines 35-43
A method named setTime(long elapseTime) that sets a new time for the object using the elapsed time. For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10
Lines 45-55

If all of these are correct, then I believe my next step is to debug my setTime method so it will actually return the correct elapseTime and allow the code to execute correctly. Any clarification, tips or tricks are greatly appreciated! This is usually what takes me hours to figure out.
 
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Please provide the code as text rather than an image.

Thanks.
 
Vel Williams
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed Ron, thank you! Just learning how cool this forum is.
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first constructor does nothing, which is not correct.
Your second constructor does not use the argument passed in.
The setTime() method calculates the time parts but never sets them. It should not be  'static'.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hours, minutes, and seconds should probably be ints.
Millis is probably used in the internals of some methods but should not be a member variable.
A toString() method may not be required but is useful for debugging.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use one constructor to help build another.
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is your instance variable milliseconds a plural noun whereas the other three are singular nouns? That makes it even more difficult for me to decide whether your Time class is supposed to represent an instant in time or an interval of time.

Although the blue text in your original post is already unhelpful because it seems to be both, or neither. A Java class should have a description of what its instances represent, and it should preferably include comments which explain that. That blue text is from somewhere else, then? Did you quote it exactly or have you interpreted the requirements in this way?
 
Rancher
Posts: 5035
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: A Java class should have a description of what its instances represent, and it should preferably include comments which explain that.


That is the first time I have seen anyone on this forum say anything positive about commenting a program.
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

Paul Clapham wrote: A Java class should have a description of what its instances represent, and it should preferably include comments which explain that.


That is the first time I have seen anyone on this forum say anything positive about commenting a program.


Some of us believe that JavaDoc isn't just for Oracle-supplied code.

Also, some of us like to do what Donald Knuth labelled "literate programming". Where you write the comments first. then implement the code accordingly.

Yes, I'll confess that I was largely influenced by the pre-OOP programming languages where it was harder to tell what was going on just by looking at a chunk of code. Especially a large chunk of spaghetti. In assembly language. But I still find the practices  I developed useful.

If you read the docs on Javadocs, you'll see that not only do they describe the different annotations that result in the various documentation elements, but also set guidelines on how to write things. For example, the first sentence of a class or method comment should be a short description, one shouldn't have to be a classically-educated polyglot to be able to comprehend, and so forth.

Note also that it is common for IDEs to encourage JavaDocs by having a high-level menu that facilitate running the JavaDoc processor on a project.

My own rules include "tell what the class/method is FOR and NOT how it does things", as Java code is sufficiently self-descriptive for that. Also, from Calculus, what the "domain" and "range" of a resource is. Meaning, what's legal input, what's expected output AND what will happen if illegal input is given.

If you have ever noticed, when I post code here on the Ranch, unless it's a simple snippet, it will have JavaDoc comments on it.
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . Millis is probably used in the internals of some methods but should not be a member variable. . . .

Unfortunately, it says to record the milliseconds since 1st January 1970. That means the class has two mutually incompatible purposes, one being to record hours minutes seconds and the other to record milliseconds alone. I see Paul has already said that. We cannot blame OP for that, but whoever set the exercise. In order to record milliseconds, you would need a milliseconds field
Another thing: I would remove that static method. It looks good, but it cannot do anything to your obkects, and I think it should be moved into a different class. It doesn't constitute any part of Time, but it does seem to constitute a way to test a Time object, which is different.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:. . . Millis is probably used in the internals of some methods but should not be a member variable. . . .

Unfortunately, it says to record the milliseconds since 1st January 1970. That means the class has two mutually incompatible purposes, one being to record hours minutes seconds and the other to record milliseconds alone. I see Paul has already said that. We cannot blame OP for that, but whoever set the exercise. In order to record milliseconds, you would need a milliseconds field


Paul was saying something different, I believe, about how it's unclear whether the class represents a specific time or an interval.   The fact that it can be constructed from milliseconds since midnight GMT 1970-01-01 suggest to me it's a specific time.

Note that the instructions repeat the list of fields several times, omitting milliseconds.  Milliseconds are only mentioned in one constructor, and one setTime() method.  This suggest to me that you can create a time from milliseconds, but you don't need to be able to report them later.  That means you don't need to store milliseconds as a field.  There is an open question as to how to handle milliseconds that are not a multiple of 1000.  They could be truncated or rounded.  Or, seconds could be represented as a double.  These are design decisions, if they're not specified in the requirements.

Campbell Ritchie wrote:Another thing: I would remove that static method. It looks good, but it cannot do anything to your obkects, and I think it should be moved into a different class. It doesn't constitute any part of Time, but it does seem to constitute a way to test a Time object, which is different.


I think this misses the point.  The method, as written, doesn't do anything really, just manipulate local variables which are then forgotten.  Extracting it to another class will continue to not do anything useful.  Carey's comments are more useful: make the method non-static.  And the method should then use its non-static nature to allow it to set fields, not local variables.  Then, it would be doing something useful.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A high-level note:

This class is designed to hold a time of no definite reference. That is, it does not take into account whether this is the current time of day, UTC time of day, or some other time odf day (which can be a real issue for webapps with users in multiple timezones). Although in certain usages, it would be UTC-relative.

Also, fatally, the milliseconds returned grom System.getMilliseconds isn't simply milliseconds, it's the number of total milliseconds since the epoch time: midnight, January 1, 1970 UTC.

So the class properties hours, minutes and seconds are apples, but milliseconds is an orange. To be totally consistent, the system.getMilliseconds() results need to be extracted from the system time milliseconds.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . This class is designed to hold a time of no definite reference. . . .

What's wrong with that? It would make it too complicated to include time zones, and what we have here is a very simplified version of LocalTime. Only, as you will have seen from the Java™ Tutorials link, LocalTime is immutable and doesn't have any setXXX() methods.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Holloway wrote:. . . This class is designed to hold a time of no definite reference. . . .

What's wrong with that? It would make it too complicated to include time zones, and what we have here is a very simplified version of LocalTime. Only, as you will have seen from the Java™ Tutorials link, LocalTime is immutable and doesn't have any setXXX() methods.


I didn't say it needed to. Just pointed out that it has specific characteristics which should be kept in mind when using it.

And that it wasn't consistent, since part and only part of it was not only timezone-specific (UTC), but carried more information than expected.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . part . . . carried more information than expected.

Yes, that is what Paul meant about apples and oranges.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Holloway wrote:. . . part . . . carried more information than expected.

Yes, that is what Paul meant about apples and oranges.


Actually, that waas me. Paul's comment was on inconsistent pluralisation.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . Actually, that waas me. . . .

Sorry for my mistake. No wonder MS told me off earlier.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic