Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Time1 here.
- * Time1 class declaration maintains the time in 24-hour format
- * @author Ahmad Lamaul Farid
- * @version 08 Oktober 2020
- */
- public class Time1
- {
- // instance variables - replace the example below with your own
- private int hour;
- private int minute;
- private int second;
- /**
- * Constructor for objects of class Time1
- */
- public void setTime( int h, int m, int s)
- {
- // initialise instance variables
- if( ( h >= 0 && h < 24 ) && ( m >= 0 && m <60 ) && ( s >= 0 && s < 60 ) )
- {
- hour = h;
- minute = m;
- second = s;
- }
- else
- throw new IllegalArgumentException(
- "hour, minute and/or second was out of range" );
- }
- public String toUniversalString()
- {
- return String.format( "%02d:%02d:%02d", hour, minute, second );
- }
- public String toString()
- {
- return String.format( "%d:%02d:%02d %s",
- ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
- minute, second, ( hour < 12 ? "AM" : "PM" ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement