Hey all - yes, this is another thread asking for some homework help, but I've got a solid start, just looking for some fresh ideas. Anyways, the task is to create a java clock class and access/use it from a driver. I saw something similar on here, but not quite the same. On top of that, there are other stipulations:
5.)There should be constructors that allow the user to set the time as desired and a default constructor that sets the time at 00:00:00 (high midnight)
6. The user should be able to set a valid time using the 24-hour clock
7. The user should be able to print the time by referring to its identifier, i.e. System.out.println(myClock);
8. There should be methods to increment hours, minutes and seconds
9. There should be a method to test if two times are equal and one to test one time relative to another
10. There should be a method to make a copy of a Clock and a method to store a copy of a Clock in another Clock

So here's what I got:

public class Clock
{
 private int hr; //store hours
 private int min;  //store minutes
 private int sec; //store seconds

 public Clock ()
 {
  setTime (0, 0, 0);
 }

 public Clock (int hours, int minutes, int seconds)
 {
  setTime (hours, minutes, seconds);
 }

 public void setTime (int hours, int minutes, int seconds)
 {
  if (0 <= hours && hours < 24)
       hr = hours;
  else
       hr = 0;
  
  if (0 <= minutes && minutes < 60)
       min = minutes;
  else
       min = 0;
  
  if (0 <= seconds && seconds < 60)
       sec = seconds;
  else
       sec = 0;
 }
 
  //Method to return the hours
 public int getHours ( )
 {
  return hr;
 }

  //Method to return the minutes
 public int getMinutes ( )
 {
  return min;
 }

  //Method to return the seconds
 public int getSeconds ( )
 {
  return sec;
 }

 public void printTime ( )
 {
  if (hr < 10)
       System.out.print ("0");
  System.out.print (hr + ":");

  if (min < 10)
       System.out.print ("0");
  System.out.print (min + ":");

  if (sec < 10)
       System.out.print ("0");
  System.out.print (sec);
}
  
  //The time is incremented by one second
  //If the before-increment time is 23:59:59, the time
  //is reset to 00:00:00
 public void incrementSeconds ( )
 {
  sec++;
  
  if (sec > 59)
  {
   sec = 0;
   incrementMinutes ( );  //increment minutes
  }
 }

  ///The time is incremented by one minute
  //If the before-increment time is 23:59:53, the time
  //is reset to 00:00:53
 public void incrementMinutes ( )
 {
  min++;
  
if (min > 59)
  {
   min = 0;
   incrementHours ( );  //increment hours
  }
 }

 public void incrementHours ( )
 {
  hr++;
  
  if (hr > 23)
      hr = 0;
 }

 public boolean equals (Clock otherClock)
 {
  return (hr == otherClock.hr
   && min == otherClock.min
    && sec == otherClock.sec);
 }

}

And the Driver so far is weak:

public class ClockDriver
{

   public static void main(String[] args) 
   {
               Clock test = new Clock();
               
               test.setTime(x, x, x);
               
               System.out.printf("The time is: ", test.getHours(), test.getMinutes(), test.getSeconds());

   }
}

Any help or input is greatly appreciated.

For number 7, add a toString() method.

In the equals method you should probably check that the parameter is not null. You may also think about properly overriding Object's equals method and just use that.

The other part of number 9 is to implement Comparable.

For number ten override the clone() method and for the other part of add a Clock instance variable and a getter/setter method for that.

For clone and equals (and hashcode since you're doing equals) see http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html

For some info on getters and setter see http://en.wikipedia.org/wiki/JavaBean (although that is also covering something else).

For more info on Comparable see http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.