I didn't think I'd need help again, but I'm pretty sure its not as bad as last time. I'm trying to increment the time in Time2.class, and that compiled. The problem is I can't make a class to use it as per the assignment, and that's because I used void on the methods. I don't know how to make a return type. I would love some guidance on this. I can't wait until I get done with this Java class, no offense to those of you who love it.

Basically my program has to increment seconds, minutes, and hours.

I also have to be sure that it increments to the next day if it goes from 11 - 12. (AM - PM and vice versa).

public class Time2
{
   public int hour;   // 0 - 23
   public int minute; // 0 - 59
   public int second; // 0 - 59

   // Time2 no-argument constructor: initializes each instance variable 
   // to zero; ensures that Time2 objects start in a consistent state
   public Time2()
   {
      this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
   } // end Time2 no-argument constructor

   // Time2 constructor: hour supplied, minute and second defaulted to 0
   public Time2( int h ) 
   { 
      this( h, 0, 0 ); // invoke Time2 constructor with three arguments
   } // end Time2 one-argument constructor

   // Time2 constructor: hour and minute supplied, second defaulted to 0
   public Time2( int h, int m ) 
   { 
      this( h, m, 0 ); // invoke Time2 constructor with three arguments
   } // end Time2 two-argument constructor 

   // Time2 constructor: hour, minute and second supplied
   public Time2( int h, int m, int s ) 
   { 
      setTime( h, m, s ); // invoke setTime to validate time
   } // end Time2 three-argument constructor 

   // Time2 constructor: another Time2 object supplied
   public Time2( Time2 time )
   {
      // invoke Time2 three-argument constructor
      this( time.getHour(), time.getMinute(), time.getSecond() );
   } // end Time2 constructor with a Time2 object argument

   // Set Methods
   // set a new time value using universal time; ensure that 
   // the data remains consistent by setting invalid values to zero
   public void setTime( int h, int m, int s )
   {
      setHour( h );   // set the hour
      setMinute( m ); // set the minute
      setSecond( s ); // set the second
   } // end method setTime

   // validate and set hour 
   public void setHour( int h ) 
   { 
      hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); 
   } // end method setHour

   // validate and set minute 
   public void setMinute( int m ) 
   { 
      minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); 
   } // end method setMinute

   // validate and set second 
   public void setSecond( int s ) 
   { 
      second = ( ( s >= 0 && s < 60 ) ? s : 0 ); 
   } // end method setSecond

   // Get Methods
   // get hour value
   public int getHour() 
   { 
      return hour; 
   } // end method getHour

   // get minute value
   public int getMinute() 
   { 
      return minute; 
   } // end method getMinute

   // get second value
   public int getSecond() 
   { 
      return second; 
   } // end method getSecond
	
   public void tick()
   {
      setSecond( second + 1 );

      if ( second == 0 )
         incrementMinute();
   }
	
	public void incrementMinute()
   {
      setMinute( minute + 1 );

      if ( minute == 0 )
         incrementHour();
	}
	
	public void incrementHour()
   {
      setHour( hour + 1 );
   }

   public String toUniversalString()
   {
      return String.format( 
         "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
   } // end method toUniversalString

   // convert to String in standard-time format (H:MM:SS AM or PM)
   public String toString()
   {
      return String.format( "%d:%02d:%02d %s", 
         ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
         getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
   } // end method toString

} // end class Time2
public class IncrementTest
{
	public static void main( String args[] )
	{
		Time2 t1 = new Time2( 5, 18, 41);
		Time2 t2 = new Time2( 7, 42, 24);
		Time2 t3 = new Time2( 11, 59, 59);
		
		System.out.println( "Time before tick method" );
		System.out.printf( "   %s\n", t3.toString() );
		System.out.println( "Time after tick method" );
		System.out.printf( "   %s\n", t3.tick() );
		
		System.out.println( "Time before incrementMinute method" );
		System.out.printf( "   %s\n", t2.toString() );
		System.out.println( "Time after incrementMinute method" );
		System.out.printf( "   %s\n", t2.incrementMinute() );
		
		System.out.println( "Time before incrementHour method" );
		System.out.printf( "   %s\n", t1.toString() );
		System.out.println( "Time after incrementHour method" );
		System.out.printf( "   %s\n", t1.incrementHour() );
		
	}//end method main
}//end class

The incrementing methods don't necessarily need a return value. Just call the increment method and then show the new time toString().

Your set method may also need to throw an IllegalArgumentException when an our of bounds parameter is passed, like 61 minutes. Also, 60 minutes is actually 0 minutes and hour++. :)

Sorry, I don't understand what you're saying.

Sorry, I don't understand what you're saying.

I'm saying that your incrementing methods are changing the state of your Time instance. All you need to do after you change that state is to display the new Time

System.out.println( "Time before incrementMinute method" );
        System.out.printf( "   %s\n", t2.toString() );
        t2.incrementMinute()
        System.out.println( "Time after incrementMinute method" );
        System.out.printf( "   %s\n", t2.toString() );

The logic to manage the state of your Time instance has nothing to do with displaying it. You already have a method for the display, so you just need to call it.

You could have made the increment methods return a formatted string that was ready to display, but then you would be mixing what really should be separate responsibilities: managing state and viewing it. Keeping those two separate, as they are now, keeps your design more flexible.

I understand what you are saying now. I just needed it explained differently, I'm a bit of an idiot.

I understand what you are saying now. I just needed it explained differently, I'm a bit of an idiot.

Bah. My first comment was pretty brief, so asking for clarification is far from being an idiot. Glad you got it :)

Nonetheless, thank you for the help.

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.