Hello, first time poster here. So if I come off as asking something so hastily and quickly my bad, but currently I'm creating a clock class on DrJava. This is what I have so far.

clock.java
import java.util.*;
import java.text.*;
public class Clock
{private int hours;
  private int minutes;
    private int seconds;
    int minutesleft, secondsleft;
    //Constructor
   //item a
    Clock()
    {
      hours=0;
      minutes=0;
      seconds=0;
    }
    //item b
    Clock(int h, int m, int s)
    {hours=h;
      minutes=m;
      seconds=s;
    }
     //Accessors
     //item c
    public void setTime(int h, int m, int s)
    {
     hours=h;
     minutes=m;
     seconds=s;
    }
   //item d
    public int getHours()
    {return hours;
    }
   //item e
    public int getMinutes()
    {return minutes;
    }
  //item f
    public int getSeconds()
    {return seconds;
    }
  
    //item g
       public void setHours(int h)
    {hours=h;
    }
    //Modifiers
    //item h
    public void setMinutes(int m)
    {minutes=m;
     
    }
    //item i
   public void setSeconds(int s)
    {seconds=s;
    }
   //item j
   public void printTime()
   { System.out.println(hours+ "H:" + minutes + "M:" + seconds + "S");
                       }
   //item k
     public void inputTime()
   {Scanner input=new Scanner(System.in);
       System.out.println("Enter hours: ");
     hours=input.nextInt();
     System.out.println("Enter minutes: ");
     minutes=input.nextInt();
     System.out.println("Enter seconds: ");
     seconds=input.nextInt();
   }
   //item l
   public String toString()
   {String s= hours+ "H:" + minutes + "M:" + seconds + "S";
     return s;
   }
   //item m
   public void incrementSeconds(int seconds)
   {this.seconds=this.seconds+seconds;
   }
   //item n
   public void incrementMinutes(int minutes)
   {this.minutes=this.minutes+ minutes;
   }
   //item o
   public void incrementHours(int hours)
   {this.hours=this.hours+hours;
   }
}
// this is the end of class Clock, put all new methods before this
                   
  
 MAIN METHOD:
import java.util.*;
import java.text.*;
public class lab80
{public static void main(String[] args)
  {
  //item a
  Clock t1=new Clock();
  //item b
  Clock t2=new Clock();
  //item c
  Clock t3=new Clock(10, 30, 15);
  //item d
  t1.inputTime();
 
  //item e
  t2.setTime(20,12, 45);
  //item f
  System.out.println("t1 hours:"+  t1.getHours());
 //item g
  System.out.println("t2 minutes:" + t2.getMinutes());
  //item h
  System.out.println("t3 seconds:" + t3.getSeconds());
  //item i
  t1.setHours(5);
  //item j
  t2.setMinutes(13);
  //item k
  t3.setSeconds(55);
  //item l
  t1.printTime();
  //item m
  System.out.println(t2);
  //item n
  t1.incrementHours(5);
  //item o
  t1.incrementMinutes(10);
  //item p
  t1.incrementSeconds(2);
  //item q
  t1.printTime();

 
 
 
 
}//end main
}//end class

I have fullfilled most of the requirements that my profressor has asked except for a few.

1-A way to increment the time, so that if seconds/minutes goes >= 60 they become minute/hour.

2-Things displayed in the main Method:
A:Create an array of objects called timeArray of type Time with 3 elements.
B: Store the Time objects t1, t2, t3 in the array timeArray
C: Display the time of all the objects in the array timeArray.

Just incase of anymore questions, I have attached the file of the requirements of the clock class. So any tips/help would be helpful. Hope it dosen't sound like alot.

Attachments
1. Design and implement the class Clock to implement the time of day in a program. Suppose that the time is represented as set of three integer data members: one to represent the hours, one to represent the minutes, and one to represent the seconds. The program must perform the following operations:

   1. default constructor - initialize  hours, minutes and seconds to zero.
   2. three-argument constructor
   3. setTime to set the time to the time specified by the user.
      public void setTime(int hours, int minutes, int seconds)
   4. getHours returns hours.
      public int getHours()
   5. getMinutes returns minutes
      public int getMinutes()
   6. getSeconds returns seconds
      public int getSeconds()
   7. setHours sets the hours to a new hour value
      void setHours(int h)
   8. setMinutes sets the minutes to a new minute value
      void setMinutes(int m)
   9. setSeconds sets the seconds to a new value
  10. printTime prints the time in the form hh:mm:ss.
      public void printTime()
  11. inputTime inputs a hours, minutes and seconds for a Clock object.
      public void inputTime()
  12. toString() returns a string in the form "hh:mm:ss"
      public String toString()
  13. incrementSeconds increments the time by seconds.
      public void incrementSeconds(int seconds)
  14. incrementMinutes increments the time by  minutes.
      public void incrementMinutes(int minutes)
  15. incrementHours increments the time by hours
      public void incrementHours(in hours)

 Write the main method to test all the object methods in the class Time.

   1. Create a Time object called  t1 with hours, minutes and seconds initialized to zero.
   2. Create a Time object called t2 with hours. minutes and seconds initialized to zero.
   3. Create a Time object called t3 with hours, minutes and seconds initialized to 10, 30, and 15 respectively.
   4. Call the method inputTime to input hours, minutes and seconds for the Time object t1.
      Input hours, minutes, seconds to  13, 40 and 20 respectively.
   5. Set the hours, minutes, and seconds for the time object t2 to 20, 12 and 45 respectively using the setTime method.
   6. Display the hours of object t1
   7. Display the minutes of object t2
   8. Display the seconds of object t3
   9. Set the hours of object t1 to 5
  10. Set the minutes of object t2 to 13
  11. Set the seconds of objects t3 to 55
  12. Display the time of object t1 using the printTime method
  13. Display the time of  object t2 using the toString method
  14. Increment the hours of object t1 to 5
  15. Increment the minute of object t1 to 10
  16. Increment the seconds of object t1 to 2
  17. Display the time of object t1 by using the method toString
  18. Create an array of objects called timeArray of type Time with 3 elements.
  19. Store the Time objects t1, t2, t3 in the array timeArray
  20. Display the time of all the objects in the array timeArray.

You should probably have your printTime () function call the toString () function. It's the same code. No sense writing it twice.

Regarding going from 0 to 59 and 0 to 23 and not beyond, in your increment functions, use the / and % operators. Add the new amount to the old amount as you do, but then divide by 60 and take the mod of 60. For hours, it would be 24.

public void incrementSeconds(int seconds)
   {
       this.seconds=this.seconds+seconds;
       int minutestoIncrement = this.seconds / 60;
       if (minutesToIncrement != 0)
            incrementMinutes (minutesToIncrement);

       this.seconds = this.seconds % 60;
   }

Do you have to worry about negative numbers at all? Not sure what the question is regarding the array. Do you not know how to declare an array? Do you not know how to write and read from it?

This article has been dead for over six months. Start a new discussion instead.