• Post Reply Bookmark Topic Watch Topic
  • New Topic

Time Class w/ constructors  RSS feed

 
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on some problems to test my meager understanding of classes in Java. The problem I'm currently working on goes:

Write a class called Time that can be used to store a time in hours, minutes and seconds. It should have the following public methods:
Time() A constructor that takes no parameters and that constructs an object representing 0 time.

Time(int, int, int) A constructor that takes three integers (hours, minutes, seconds) and that constructs a Time object storing that time.

void add(int, int, int) A method which takes a time as three integers (hours, minutes, seconds) and that adds the time to the current time.

double getTime() A method which returns the time as the real-valued number of hours with a decimal (as in 9.53).

String toString() toString which returns a string representation of the time (hh:mm:ss) When converted to a string, the seconds and minutes should always be reported as being in the range of 0 to 59. That means that you may have to "carry" a full hour or minute to the next column.

I think I've figured everything out except the void add method- I've tried numerous ways and can't seem to get anywhere. Here's what I have so far, and any pointers or assistance would be much appreciated!

 
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a quick glance in your code I'd say the first thing that is flawed is the AM/PM. You only have time upto 1200 hours after that the user would get a IllegalArgumentException.

As per your request to add time, I'd say java.util.Calendar is good place to start searching. With the calendar functionality you can get the current time and make manipulations, like adding 10 minutes or decreasing 10 minutes from the current time.
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:I think I've figured everything out except the void add method- I've tried numerous ways and can't seem to get anywhere. Here's what I have so far, and any pointers or assistance would be much appreciated!


In your add method, I would rather first figure out what delta needs to be modified for hour, minutes, seconds. E.g. if the user passed (0,1,61) what needs to be added is 2 minutes and 1 second. Hint: Are you aware of the modulus (%) operator in Java?

And to throw a spanner in the works, have you considered the scenario where the user can pass something like (0,1,-61) to your method?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vineeth Menon wrote:
As per your request to add time, I'd say java.util.Calendar is good place to start searching.

True but you probably missed what the OP said regarding this being an academic and learning exercise
Laura Peterson wrote:I am working on some problems to test my meager understanding of classes in Java.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol, my bad.
 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've both been quite helpful... I'll get to work on your hints right away... thanks so much!

- It works now, thanks again!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!