Guest User

Untitled

a guest
Oct 14th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. //Fig. 8.4 : ThisTest.java
  2. //digunakan secara implisit dan eksplisit untuk merujuk ke object's members
  3.  
  4. public class ThisTest
  5. {
  6. public static void main(String[] args)
  7. {
  8. SimpleTime time = new SimpleTime(15, 30, 19);
  9. System.out.println(time.buildString() );
  10. } //akhir main
  11. }//akhir class ThisTest
  12.  
  13. //class SimpleTime mendemonstrasikan the "this" reference
  14. class SimpleTime
  15. {
  16. private int jam; //0-23
  17. private int menit; //0-59
  18. private int detik; //0-59
  19.  
  20. // if the constructor uses parameter names identical to
  21. // instance variable names the "this" reference is
  22. // required to distinguish between the names
  23. public SimpleTime( int jam, int menit, int detik)
  24. {
  25. this.jam = jam; // set "this" object's jam
  26. this.menit = menit; // set "this" object's menit
  27. this.detik = detik; // set "this" object's detik
  28. }//akhir SimpleTime constructor
  29.  
  30. //use explicit and implicit "this" to call toUniversalString
  31. public String buildString()
  32. {
  33. return String.format("%24s: %s\n%24s: %s",
  34. "this.toUniversalString()", this.toUniversalString(),
  35. "toUniversalString()", toUniversalString() );
  36. }//akhir method buildString
  37.  
  38. //mengubah ke String pada format waktu Universal (JJ:MM:DD)
  39. public String toUniversalString()
  40. {
  41. // "this" is not required here to access instance variables,
  42. // because method does not have local variables with same
  43. // names as instance variables
  44. return String.format("%02d : %02d : %02d",
  45. this.jam, this.menit, this.detik);
  46. } // akhiir method toUniversalString
  47. }//akhir program
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment