mnaufaldillah

ThisTest Tugas1

Oct 11th, 2020
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class ThisTest here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class ThisTest
  9. {
  10.     public static void main( String[] args )
  11.     {
  12.         SimpleTime time = new SimpleTime( 15, 30, 19 );
  13.         System.out.println( time.buildString() );
  14.     } // end main
  15. } // end class ThisTest
  16.  
  17. // class SimpleTime demonstrates the "this" reference
  18. class SimpleTime
  19. {
  20.     private int hour; // 0 - 23
  21.     private int minute; // 0 - 59
  22.     private int second; // 0 - 59
  23.    
  24.     // if the constructor uses parameter names identical to
  25.     // instance variable names the "this" reference is
  26.     // required to distinguish between the names
  27.     public SimpleTime( int hour, int minute, int second )
  28.     {
  29.         this.hour = hour; // set "this" object's hour
  30.         this.minute = minute; // set "this" object's minute
  31.         this.second = second; // set "this" object's second
  32.     } // end SimpleTime constructor
  33.    
  34.     // use explicit and implicit "this" to call toUniversalString
  35.     public String buildString()
  36.     {
  37.         return String.format( "%24s: %s\n%24s: %s",
  38.             "this.toUniversalString()", this.toUniversalString(),
  39.             "toUniversalString()", toUniversalString() );
  40.     } // end method buildString
  41.    
  42.     // convert to String in universal-time format (HH:MM:SS)
  43.     public String toUniversalString()
  44.     {
  45.         // "this" is not required here to access instance variables,
  46.         // because method does not have local variables with same
  47.         // names as instance variables
  48.         return String.format( "%02d:%02d:%02d",
  49.             this.hour, this.minute, this.second );
  50.     } // end method toUniversalString
  51. } // end class SimpleTime
Advertisement
Add Comment
Please, Sign In to add comment