Minggu, 04 Juni 2017

Mesin Pembelian Tiket Bioskop

Dalam Final Project dari tugas PBO,
saya membuat contoh Mesin Pembelian Tiket Bioskop

Source Code:


 
  import java.util.Scanner; 

public class Main 
{ 
    public static void Main (String[] args) 
    { 
        Scanner scan = new Scanner (System.in); 
        int opt=1, initDoraemon = 0, initNaruto = 0, initPikachu = 0; 
         
        while (opt != 0) 
        { 
            System.out.println ("Selamat datang!"); 
            System.out.println ("Silakan pilih film yang akan dipesan: \n"); 
            System.out.println ("1. Doraemon\n2. Naruto\n3. Pikachu\n"); 
            opt = scan.nextInt(); 
             
            Film output = new Film(); 
            Print paper = new Print(); 
             
            switch (opt) 
            { 
                case 1: 
                initDoraemon++; 
                paper.ticket (1); 
                output.println (1, initDoraemon); 
                output.println (2, initNaruto); 
                output.println (3, initPikachu); 
                break; 
                 
                case 2: 
                initNaruto++; 
                paper.ticket (2); 
                output.println (1, initDoraemon); 
                output.println (2, initNaruto); 
                output.println (3, initPikachu); 
                break; 
                 
                case 3: 
                initPikachu++; 
                paper.ticket (2); 
                output.println (1, initDoraemon); 
                output.println (2, initNaruto); 
                output.println (3, initPikachu);                 
                break;                 
            } 
        } 
    } 
}


 
 public class Film 
{ 
    Doraemon a = new Doraemon(); 
    Naruto b = new Naruto(); 
    Pikachu c = new Pikachu(); 
    int x; 
    int saveDoraemon = 0, saveNaruto = 0, savePikachu = 0; 

    public void println (int x, int num) 
    { 
        if (x == 1) 
        { 
            System.out.println ("Pemesan Tiket Doraemon = " + a.DoraemonNumber(num)); 
        } 
        else if (x == 2) 
        { 
            System.out.println ("Pemesan Tiket Naruto = " + b.NarutoNumber(num)); 
            System.out.println (); 
        } 
        else if (x == 3) 
        { 
            System.out.println ("Pemesan Tiket Pikachu = " + c.PikachuNumber(num)); 
            System.out.println (); 
        }         
        else 
        { 
            System.out.println ("Input salah"); 
        } 
    } 
}
 


 
 public class Naruto 
{ 
    private int numNaruto; 
    int snNaruto = 0; 
     
    public int NarutoNumber (int x) 
    { 
        numNaruto = x; 
        snNaruto = numNaruto; 
        return numNaruto; 
    } 
}
 


 
  public class Doraemon 
{ 
    private int numDoraemon; 
    int snDoraemon=0; 
     
    public int DoraemonNumber (int x) 
    { 
        numDoraemon = x; 
        snDoraemon = x; 
        return numDoraemon; 
    } 
} 
 


 
  public class Pikachu 
{ 
    private int numPikachu; 
    int snPikachu=0; 
     
    public int PikachuNumber (int x) 
    { 
        numPikachu = x; 
        snPikachu = x; 
        return numPikachu; 
    } 
} 
 


 
  public class Print 
{ 
    private Main antrian; 
    public void ticket (int x) 
    { 
        if (x == 1) 
        { 
            System.out.println ("========================================"); 
            System.out.println (); 
            System.out.println ("Pemesan Film Doraemon Bertambah"); 
            System.out.println (); 
            System.out.println ("========================================="); 
            System.out.println ("\n"); 
        } 
        else if (x == 2) 
        { 
            System.out.println ("=========================================\n"); 
            System.out.println (); 
            System.out.println ("Pemesan Film Naruto Bertambah"); 
            System.out.println ("========================================="); 
            System.out.println ("\n"); 
        } 
        else if (x == 3) 
        { 
            System.out.println ("=========================================\n"); 
            System.out.println (); 
            System.out.println ("Pemesan Film Pikachu Bertambah"); 
            System.out.println ("========================================="); 
            System.out.println ("\n"); 
        } 
    } 
}
 


Selasa, 04 April 2017

Mesin Antrian

Tugas Evaluasi tengah semester saya yaitu rancangan pembuatan program mesin antrian.

Dalam proyek ini saya akan membuat sebuah aplikasi mesin antri. Aplikasi ini masih berupa aplikasi sederhana yang hanya berjalan pada satu komputer saja. Aplikasi ini berfungsi untuk mencetak nomor antrian untuk setiap pelanggan yang memberi input kepada mesin, serta memanggil nomor antrian yang akan di panggil.

 Rancangan adalah sebagai berikut:

di dalam mesin terdapat 2 pilihan yang akan di berikan pada pangambil no.antrian dan yang ke dua akan dipakai oleh pemanggil untuk memanggil nomor yang mengantri.




Ini merupakan inputan yang akan dipilih :





 Untuk output dari yang akan mengantri atau yang akan memanggil pengantri adalah sebagai berikut,


Senin, 06 Maret 2017

Class Date

Class Date mendeklarasikan variabel month, day, dan year untuk merepresentasikan sebuah tanggal.
Didalam class date akan ada dua waktu (waktu sekarang dan waktu  yang lain) yang dimana waktu yang lain dapat di setting sendiri sesuai kebutuhan.

 1. Date Class Declaration

 // Fig. 8.7: Date.java  
 // Date class declaration.  
 public class Date  
 {  
   private int month; // 1-12  
   private int day; // 1-31 based on month  
   private int year; // any year  
   private static final int[] daysPerMonth = // days in each month  
     { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
     // constructor: call checkMonth to confirm proper value for month;  
     // call checkDay to confirm proper value for day  
     public Date( int theMonth, int theDay, int theYear )  
     {  
       month = checkMonth( theMonth ); // validate month  
       year = theYear; // could validate year  
       day = checkDay( theDay ); // validate day  
       System.out.printf(  
       "Date object constructor for date %s\n", this );  
     } // end Date constructor  
     // utility method to confirm proper month value  
     private int checkMonth( int testMonth )  
     {  
       if ( testMonth > 0 && testMonth <= 12 ) // validate month  
         return testMonth;  
       else // month is invalid  
         throw new IllegalArgumentException( "month must be 1-12" );  
         // end method checkMonth  
     }  
     // utility method to confirm proper day value based on month and year  
     private int checkDay( int testDay )  
     {  
       // check if day in range for month  
       if ( testDay > 0 && testDay <= daysPerMonth[ month ] )  
         return testDay;  
       // check for leap year  
       if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||  
         ( year % 4 == 0 && year % 100 != 0 ) ) )  
         return testDay;  
       throw new IllegalArgumentException(  
         "day out-of-range for the specified month and year" );  
     } // end method checkDay  
     // return a String of the form month/day/year  
     public String toString()  
     {  
       return String.format( "%d/%d/%d", month, day, year );  
     } // end method toString  
     } // end class Date  

3. Employee class with references to other objects


 // Fig. 8.8: Employee.java  
 // Employee class with references to other objects.  
 public class Employee  
 {  
   private String firstName;  
   private String lastName;  
   private Date birthDate;  
   private Date hireDate;  
   // constructor to initialize name, birth date and hire date  
   public Employee( String first, String last, Date dateOfBirth,  
     Date dateOfHire )  
     {  
       firstName = first;  
       lastName = last;  
       birthDate = dateOfBirth;  
       hireDate = dateOfHire;  
     } // end Employee constructor  
   // convert Employee to String format  
   public String toString()  
   {  
     return String.format( "%s, %s Hired: %s Birthday: %s",  
     lastName, firstName, hireDate, birthDate );  
   } // end method toString  
   } // end class Employee  

4. Composition demonstration

 

 // Fig. 8.9: EmployeeTest.java  
 // Composition demonstration.  
 public class EmployeeTest  
 {  
   public static void main( String[] args )  
   {  
     Date birth = new Date( 7, 24, 1949 );  
     Date hire = new Date( 3, 12, 1988 );  
     Employee employee = new Employee( "Bob", "Blue", birth, hire );  
     System.out.println( employee );  
   } // end main  
 } // end class EmployeeTest  

Out put:
 Date object constructor for date 7/24/1949  
 Date object constructor for date 3/12/1988  
 Blue, Bob Hired: 3/12/1988 Birthday: 7/24/1949  

5. Declaring an enum type with constructor and explicit in

 // Fig. 8.10: Book.java  
 // Declaring an enum type with constructor and explicit instance fields  
 // and accessors for these fields  
 public enum Book  
 {  
   // declare constants of enum type  
   JHTP( "Java How to Program", "2012" ),  
   CHTP( "C How to Program", "2007" ),  
   IW3HTP( "Internet & World Wide Web How to Program", "2008" ),  
   CPPHTP( "C++ How to Program", "2012" ),  
   VBHTP( "Visual Basic 2010 How to Program", "2011" ),  
   CSHARPHTP( "Visual C# 2010 How to Program", "2011" );  
   // instance fields  
   private final String title; // book title  
   private final String copyrightYear; // copyright year  
   // enum constructor  
   Book( String bookTitle, String year )  
   {  
     title = bookTitle;  
     copyrightYear = year;  
   } // end enum Book constructor  
   // accessor for field title  
   public String getTitle()  
   {  
     return title;  
   } // end method getTitle  
   // accessor for field copyrightYear  
   public String getCopyrightYear()  
   {  
     return copyrightYear;  
   } // end method getCopyrightYear  
 } // end enum Book  

6. Testing enum type Book

 // Fig. 8.11: EnumTest.java  
 // Testing enum type Book.  
 import java.util.EnumSet;  
 public class EnumTest  
 {  
   public static void main( String[] args )  
   {  
     System.out.println( "All books:\n" );  
     // print all books in enum Book  
     for( Book book : Book.values())  
     System.out.printf( "%-10s%-45s%s\n", book,  
     book.getTitle(), book.getCopyrightYear() );  
     System.out.println( "\nDisplay a range of enum constants:\n" );  
     // print first four books  
     for ( Book book : EnumSet.range( Book.JHTP, Book.CPPHTP ) )  
       System.out.printf( "%-10s%-45s%s\n", book,  
       book.getTitle(), book.getCopyrightYear() );  
     } // end main  
  } // end class EnumTest  

Output :
 All books:  
 JHTP Java How to Program 2012  
 CHTP C How to Program 2007  
 IW3HTP Internet & World Wide Web How to Program 2008  
 CPPHTP C++ How to Program 2012  
 VBHTP Visual Basic 2010 How to Program 2011  
 CSHARPHTP Visual C# 2010 How to Program 2011  
 Display a range of enum constants:  
 JHTP Java How to Program 2012  
 CHTP C How to Program 2007  
 IW3HTP Internet & World Wide Web How to Program 2008  
 CPPHTP C++ How to Program 2012  

7. Static variable used to maintain a count of the number of Employee objects in memory

 

 // Fig. 8.12: Employee.java  
 // Static variable used to maintain a count of the number of  
 // Employee objects in memory.  
 public class Employee  
 {  
   private String firstName;  
   private String lastName;  
   private static int count = 0; // number of Employees created  
   // initialize Employee, add 1 to static count and  
   // output String indicating that constructor was called  
   public Employee( String first, String last )  
   {  
     firstName = first;  
     lastName = last;  
     ++count; // increment static count of employees  
     System.out.printf( "Employee constructor: %s %s; count = %d\n",  
       firstName, lastName, count );  
   } // end Employee constructor  
   // get first name  
   public String getFirstName()  
   {  
     return firstName;  
   } // end method getFirstName  
   // get last name  
   public String getLastName()  
   {  
     return lastName;  
   } // end method getLastName  
   // static method to get static count value  
   public static int getCount()  
   {  
     return count;  
   } // end method getCount  
   } // end class Employee  

8. Static member demonstration

 DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL  
 // Fig. 8.13: EmployeeTest.java  
 // static member demonstration.  
 public class EmployeeTest  
 {  
   public static void main( String[] args )  
   {  
     // show that count is 0 before creating Employees  
     System.out.printf( "Employees before instantiation: %d\n",  
     Employee.getCount() );  
     // create two Employees; count should be 2  
     Employee e1 = new Employee( "Susan", "Baker" );  
     Employee e2 = new Employee( "Bob", "Blue" );  
     // show that count is 2 after creating two Employees  
     System.out.println( "\nEmployees after instantiation: " );  
     System.out.printf( "via e1.getCount(): %d\n",e1.getCount() );  
     System.out.printf( "via e2.getCount(): %d\n",e2.getCount() );  
     System.out.printf( "via Employee.getCount(): %d\n",  
       Employee.getCount());  
     // get names of Employees  
     System.out.printf( "\nEmployee 1: %s %s\nEmployee 2: %s %s\n",  
       e1.getFirstName(), e1.getLastName(),  
       e2.getFirstName(), e2.getLastName() );  
       // in this example, there is only one reference to each Employee,  
       // so the following two statements indicate that these objects  
       // are eligible for garbage collection  
       e1 = null;  
       e2 = null;  
   }// end main  
   } // end class EmployeeTest  

Output:

 Employees before instantiation: 0  
 Employee constructor: Susan Baker; count = 1  
 Employee constructor: Bob Blue; count = 2  
 Employees after instantiation:  
 via e1.getCount(): 2  
 via e2.getCount(): 2  
 via Employee.getCount(): 2  
 Employee 1: Susan Baker  
 Employee 2: Bob Blue  

Object studi kasus Ticket Machine

Ticket Machine
adalah sebuah mesin sebagai penjualan tiket kereta api yang mengatur proses penjualan dari satu kota ke kota yag lain, di dalam tiket machine ada program dimana progam itu yang mengatur proses harga tiket.

Berikut program (Source code) dari Ticket Machine:

 /**  
  * Write a description of class TicketMachine here.  
  *   
  * Ari Setyaji  
  * Ticket Machine  
  */  
 public class TicketMachine  
 {  
   //The price of a ticket ftr=rom this machin  
   private int price;  
   private int balance;  
   private int total;  
   public TicketMachine(int ticketCost)  
   {  
     price = ticketCost;  
     balance = 0;  
     total = 0;  
   }  
   public int getPrice()  
   {  
     return price ;  
   }  
   public int getBalance()  
   {  
     return balance;  
   }  
   public void insertMoney(int amount)  
   {  
     balance=balance+amount;  
   }  
   public void printTicket()  
   {  
     System.out.println("##################");  
     System.out.println("# The BlueJ Line");  
     System.out.println("# Ticket");  
     System.out.println("# "+price+" cents.");  
     System.out.println("##################");  
     System.out.println();  
     total=total+balance;  
     balance=0;  
   }    
 }  



Ini adalah Program IntMain:

 /**  
  * Write a description of class IntMain here.  
  *   
  * Ari setyaji  
  * Int main  
  */  
 import java.util.Scanner;  
 public class IntMain  
 {  
   public static void main(String args[])  
   {  
     Scanner scan= new Scanner(System.in);  
     int cost,menu, a, n;  
     cost = 50;  
     System.out.println("Masukkan harga tiket \n");  
     cost=scan.nextInt();  
     TicketMachine ticket=new TicketMachine(cost);  
     while (true)  
     {  
     System.out.println("1.Get Price");  
     System.out.println("2. Get Balance");  
     System.out.println("3. Insert Money");  
     System.out.println("4. Print Ticket");  
     menu=scan.nextInt();  
       switch(menu)  
       {  
       case 1:  
       cost=ticket.getPrice();  
       System.out.println(cost);  
       break;  
       case 2:  
       ticket.getBalance();  
       break;  
       case 3:  
       int money=scan.nextInt();  
       ticket.insertMoney(money);   
       break;  
       case 4:  
       ticket.printTicket();   
       break;   
     }  
   }  
 }  
 }  


Senin, 27 Februari 2017

Java Time Class

ini adalah hasilnnya :

 **  
  * Time1.java  
  * Time1 class declaration maintains the time in 24-hour format.  
  *  
  * @Ari Setyaji  
  * @28 February 2017  
  */  
 public class Time1  
 {  
   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)  
   {  
     // validate hour, minute and second  
     if ((h >= 0 && h < 24) && ( m >= 0 && m < 60) && (s >= 0 && s < 60))  
     {  
       hour = h;  
       minute = m;  
       second = s;  
     } // end if  
     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 standard-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  




 /**  
  * Time1Test.java  
  *  
  * Ari setyaji  
  * 28 February 2017  
  */  
 public class Time1Test  
 {  
   public static void main (String[] args)  
   {  
     // create and initialize a Time1 object  
     Time1 time = new Time1(); // invokes Time1 constructor  
     // output string representations of the time  
     System.out.print ("The initial universal time is: ");  
     System.out.println (time.toUniversalString());  
     System.out.print ("The initial standard time is: ");  
     System.out.println (time.toString());  
     System.out.println(); // output a blank line  
     // change time and output updated time  
     time.setTime (13, 27, 6);  
     System.out.print ("Universal time after setTime is: ");  
     System.out.println (time.toUniversalString());  
     System.out.print ("Standard time after setTime is: ");  
     System.out.println (time.toString());  
     System.out.println(); // output a blank line  
     // attempt to set time with invalid values  
     try  
     {  
       time.setTime (99, 99, 99); // all values out of range  
     } // end try  
     catch (IllegalArgumentException e)  
     {  
       System.out.printf ("Exception: %s\n\n", e.getMessage());  
     } // end catch  
     // display time after attempt to set invalid values  
     System.out.println ("After attempting invalid settings:" );  
     System.out.print ("Universal time: ");  
     System.out.println (time.toUniversalString());  
     System.out.print ("Standard time: ");  
     System.out.println (time.toString());  
   } // end main  
 } // end class Time1Test  
Hasil output adalah :

 The initial universal time is: 00:00:00  
 The initial standard time is: 12:00:00 AM  
 Universal time after setTime is: 13:27:06  
 Standard time after setTime is: 1:27:06 PM  
 Exception: hour, minute and/or second was out of range  
 After attempting invalid settings:  
 Universal time: 13:27:06  
 Standard time: 1:27:06 PM  



 /**  
  * ThisTest.java  
  * this used implicitly and explicitly to refer to members of an object.  
  *  
  * Ari Setyaji  
  * 28 February 2017  
  */  
 public class ThisTest  
 {  
   public static void main (String[] args)  
   {  
    SimpleTime time = new SimpleTime (15, 30, 19);  
    System.out.println (time.buildString());  
   } // end main  
 } // end class ThisTest  
 // class SimpleTime demonstrates the "this" reference  
 class SimpleTime  
 {  
   private int hour; // 0 - 23  
   private int minute; // 0 - 59  
   private int second; // 0 - 59  
   // if the constructor 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  
   } // end SimpleTime constructor  
   // use explicit and implicit "this" to call toUniversalString  
   public String buildString()  
   {  
     return String.format ("%24s: %s\n%24s: %s",  
       "this.toUniversalString()", this.toUniversalString(),  
       "toUniversalString()", toUniversalString());  
   } // end method buildString  
   // 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);  
   } // end method toUniversalString  
 } // end class SimpleTime  
Hasil output adalah :

 this.toUniversalString(): 15:30:19  
 toUniversalString(): 15:30:19  


 /**  
  * Time2.java  
  * Time2 class declaration with overloaded constructors.  
  *  
  * Ari setyaji  
  * 28 February 2017  
  */  
 public class Time2  
 {  
   private int hour; // 0 - 23  
   private int minute; // 0 - 59  
   private int second; // 0 - 59  
   // Time2 no-argument constructor:  
   // initializes each instance variable to zer  
   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;  
   // validate the data  
   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)  
   {  
     if (h >= 0 && h < 24)  
       hour = h;  
     else  
       throw new IllegalArgumentException ("hour must be 0 - 23");  
   } // end method setHour  
   // 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");  
   } // end method setMinute  
   // 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");  
   } // 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  
   // convert to String in universal-time format (HH:MM:SS)  
   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  


 /**  
  * Time2Test.java  
  * Overloaded constructors used to initialize Time2 objects.  
  *  
  * Ari Setyaji  
  * 28 February 2017  
  */  
 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 ("Constructed 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 minute 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); // invalid value  
     } // end try  
     catch (IllegalArgumentException e)  
     {  
       System.out.printf ("\nException while initializing t6: %s\n", e.getMessage());  
     } // end catch  
   } // end main  
 } // end class Time2Test  
Hasil output adalah :
 Constructed with:  
 t1: all arguments defaulted  
 00:00:00  
 12:00:00 AM  
 t2: hour specified; minute and second defaulted  
 02:00:00  
 2:00:00 AM  
 t3: hour and minute specified; second defaulted  
 21:34:00  
 9:34:00 PM  
 t4: hour, minute and second specified  
 12:25:42  
 12:25:42 PM  
 t5: Time2 object t4 specified  
 12:25:42  
 12:25:42 PM  
 Exception while initializing t6: hour must be 0-23  

Senin, 20 Februari 2017

 Object, Property, Methode, Class, State, Instantiation, dan Instance

-Object
Yang dimaksud dengan objek adalah sekumpulan software yang terdiri dari variable dan method-method yang terkait. Objek juga merupakan benda nyata yang di buat berdasarkan rancangan yang di definisikan di dalam class. Object adalah instance dari class.
Jika class secara umum mepresentasikan (template) sebuah object, sebuah instance adalah representasi nyata dari class itu sendiri.
Contoh : Dari class Fruit kita dapat membuat object Mangga, Pisang, Apel, dan lain-lain.

-Property
disebut juga dengan atribut adalah data yang terdapat dalam sebuah class.
contoh property dari laptop bisa berupa merk, warna, jenis processor, ukuran layar, dan lain-lain.
Jika anda sudah terbiasa dengan program PHP, property ini sebenarnya hanyalah variabel yang terletak di dalam class.
Seluruh aturan dan tipe data yang biasa diinput kedalam variabel, bisa juga diinput kedalam property.
Aturan tata cara penamaan property sama dengan aturan penamaan variabel.

-Methode
Method adalah tindakan yang bisa dilakukan didalam class, Jika menggunakan analogi class laptop kita. Method pada dasarnya adalah function yang berada di dalam class.
Seluruh fungsi dan sifat function bisa diterapkan kedalam method, seperti argumen/parameter, mengembalikan nilai (dengan keyword return), dan lain-lain.
contoh method adalah: menghidupkan laptop, mematikan laptop, mengganti cover laptop, dan berbagai tindakan lain.

-Class
Instantiation adalah proses pembuatan obyek spesifik yang merupakan anggota atau instan dari kelas.
Instance adalah sebuah object yang dibuat oleh sebuah class Instantiation adalah pembuatan instance

-State
State (instance variables) –> variable2 yang dideklarasikan di dalam class.
contoh di atas nama dan umur disebut dengan state.

-Instantiation
Proses pembuatan obyek spesifik yang merupakan anggota atau instan dari kelas.
Instance adalah sebuah object yang dibuat oleh sebuah class Instantiation adalah pembuatan instance

-Instance
disebut juga variable tidak statis. Dalam bahasa pemrograman berorientasi objek, objek menyimpan variabel yang tidak dideklarasikan dengan kata kunci static dalam kategori non-statis, atau dapat berubah-ubah. Suatu kelas dapat dijelmakan ke dalam beberapa objek.
Nilai yang terkandung dalam variabel tak-statis ini berbeda untuk setiap objeknya. adalah atribut untuk tiap obyek dari class yang sama. Instance variable adalah variabel yang memiliki nilai yang berbeda di setiap objek Instance Variable, adalah atribut untuk tiap objek dari class yang sama.
Jadi, objek-objek di dalam sebuah class kemungkinan mempunyai nilai (value) yang berbeda karena atribut diberikan kepada masing-masing objek.