Tugas PBO 3 Kelas B
Berikut adalah simulasi mesin tiket kereta api sederhana yang terinspirasi dari negara Jepang. Pada kasus isi terdapat dua kelas yaitu Time1 dan Time1Test dimana kelas Time1 menunjukkan waktu dalam sehari sedangkan kelas Time1Test merupakan kelas aplikasi dimana metode utama menciptakan satu objek dari kelas Time1 dan memanggil metodenya.
1. Kelas Waktu
Source Code: Time1 Class /** * Fig. 8.1: Time1.java Time1 class declaration maintains the time in 24-hour format. */ public class Time1 extends Time1Test { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 //set a new time value using universal time; //throw an exception if the hour, minute, or second is invalid public void setTime( int h, int m, int s) { 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"); }//end method setTime //convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String.format ("%02d:%02d:%02d", hour, minute, second); }//end method toUniversalString //convert to String in standart-time format (H:MM:SS AM or PM) public String toString () { return String.format ("%d:%02d:%02d %s", ( ( hour == 0 || hour == 12) ? 12 : hour % 12), minute, second, (hour < 12 ? "AM" : "PM") ); }//end method toString }// end class Time1
Output
2. Mengontrol Akses ke Member-Member
Source Code: MemberAcccessTest Class /** * Program Kelas MemberAccessTest * Anggota kelas private Time1 tidak dapat diakses * Nama file : MemberAccessTest.java */ public class MemberAccessTest { public static void main (String[] args) { Time1 time = new Time1(); //create and initialize Time1 object time.hour = 7; //error: hour has private access in Time1 time.minute = 15; //error : minute has private access in Time1 time.second = 30; //error : second has private access in Time1 } }
3. Mengacu Anggota Objek Saat ini dengan this reference
Source Code: ThisTest Class /** * Program Kelas ThisTest * Program ini digunakan secara implisit dan eksplisit untuk merujuk kepada anggota dari sebuah objek * Nama file : ThisTest.java */ public class ThisTest { public static void main (String[] args) { SimpleTime time = new SimpleTime( 15, 30, 19 ); System.out.println (time.buildString()); } } class SimpleTime { private int hour; private int minute; private int second; //if the contructor uses parameter names identical to instance variable names //the "this" reference is required to distinguish between the names public SimpleTime( int hour, int minute, int second ) { this.hour = hour; // set "this" object's hour this.minute = minute; //set "this" object's minute this.second = second; //set "this" object's second } //use explicit and implicit "this" to call toUniversalString public String buildString() { return String.format ("%24s: %s\n%24s: %s", "this.toUnivesalString()", this.toUniversalString(), "toUniversalString()", toUniversalString() ); } //convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { //"this" is not required here to access instance variables, //because method does not have local variables with same names as instance variables return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second); } }
Output
4. Konstruktor Berelebihan Beban
Source Code: Tim2 Class /** * Program Kelas Time2 * Deklarasi kelas Time2 dengan konstuktor overload * Nama file : Time2.java */ public class Time2 extends Time2Test { private int hour; private int minute; private int second; //Time2 no-argument constructor: //initializes each instance variable to zero public Time2() { this( 0, 0, 0); //invoke Time2 constructor with three arguments } //Time2 constructor: hour supplied, minute and second defaulted to 0 public Time2 (int h) { this (h, 0, 0); } //Time2 constructor: hour and minute supplied, second defaulted to 0 public Time2 (int h, int m) { this (h, m, 0); } //Time2 constructor: hour, minute and second supplied public Time2 (int h, int m, int s) { setTime (h, m, s); //invoke setTime to validate time } //Time2 constructor: another Time2 object supplied public Time2(Time2 time) { this (time.getHour(), time.getMinute(), time.getSecond()); } //Set methods //Set a new time value using universal time; //validate the data public void setTime (int h, int m, int s) { setHour(h); setMinute(m); setSecond(s); } //validate and set hour public void setHour(int h) { if (h >= 0 && h< 24) hour = h; else throw new IllegalArgumentException ("hour must b 0-23"); } //validate and set minute public void setMinute (int m) { if (m >= 0 && m < 60) minute = m; else throw new IllegalArgumentException ("minute must be 0-59"); } //validate and set second public void setSecond (int s) { if (s >= 0 && s <60) second = ((s >= 0 && s< 60) ? s : 0); else throw new IllegalArgumentException ("second must be 0-59"); } //gets methods //get hour value public int getHour() { return hour; } //get minute value public int getMinute() { return minute; } //get second value public int getSecond() { return second; } //convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond()); } //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")); } }
Source Code: Time2Test /** * Program Kelas Time2Test * Konstruktor overload digunakan untuk menginisialisasi objek Time2 * Nama file : Time2Test.java */ public class Time2Test { public static void main (String[] args) { Time2 t1 = new Time2(); //00:00:00 Time2 t2 = new Time2(2); //02:00:00 Time2 t3 = new Time2(21, 34); //21:34:00 Time2 t4 = new Time2(12, 25, 42); //12:25:42 Time2 t5 = new Time2(t4); //12:25:42 System.out.println ("Constructured with:"); System.out.println ("t1: all arguments defaulted"); System.out.printf(" %s\n", t1.toUniversalString()); System.out.printf(" %s\n", t1.toString()); System.out.println("t2: hour specified; minute and second defaulted"); System.out.printf(" %s\n", t2.toUniversalString()); System.out.printf(" %s\n", t2.toString()); System.out.println("t3: hour and minuter specified; second defaulted"); System.out.printf(" %s\n", t3.toUniversalString()); System.out.printf(" %s\n", t3.toString()); System.out.println("t4: hour, minute and second specified"); System.out.printf(" %s\n", t4.toUniversalString()); System.out.printf(" %s\n", t4.toString()); System.out.println("t5: Time2 object t4 specified"); System.out.printf(" %s\n", t5.toUniversalString()); System.out.printf(" %s\n", t5.toString()); //attempt to initialize t6 with invalid values try { Time2 t6 = new Time2 (27, 74, 99); } catch(IllegalArgumentException e) { System.out.printf("\nException while initializing t6: %s\n", e.getMessage()); } } }
Output
Comments
Post a Comment