Langsung ke konten utama
Composition & Enumerations

A. COMPOSITION
    Composition adalah dimana hubungan suatu object bergantung dengan objek lainnya.
   Berikut ini program class Date, class Employee dan class EmployeeTest.

Source Code : Class Date
// Fig. 8.7: Date.java  
 // Date class declaration.  
  public class Date   
  {   
   private int month;   
   private int day;   
   private int year;   
   private static final int[] daysPerMonth = //days in each month    
   {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    
   //contruktor : 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);    
   year = theYear;    
   day = checkDay(theDay);    
   System.out.printf ("Date object constructor for date %s\n", this);    
   }    
   //utility method to confirm proper month value    
   private int checkMonth(int testMonth)    
   {    
   if (testMonth > 0 && testMonth <= 12)    
   return testMonth;    
   else    
   throw new IllegalArgumentException("Month must be 1 - 12");    
   }    
   //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 && 100 != 0)))    
   return testDay;    
   throw new IllegalArgumentException("day out-of-range for the specified month and year");    
   }    
   //return a String of the form month/day/year    
   public String toString()    
   {    
   return String.format("%d/%d/%d", month, day, year);    
   }    
  }   
Souce Code : Class Employee
// 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;    
   //contsructor 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;    
   }    
   //convert Employee to String format    
   public String toString()    
   {    
   return String.format ("%s, %s Hired: %s Birthday: %s", lastName, firstName, hireDate, birthDate);    
   }    
  }   
Source Code : Class EmployeeTest
 // Fig. 8.9: EmployeeTest.java  
 // Composition demonstration.
public class EmployeeTest  
 {  
   public static void main (String[] agrs)    
   {    
   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);    
   }    
 } 
Output :
B.ENUMERATIONS
  Enumeration adalah kumpulan nama-nama konstanta 
   yang didefinisikan sebagai tipe data baru.
Source code : program tipe enum Book
// 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;    
   }    
   //accessor for field title    
   public String getTitle()    
   {    
   return title;    
   }//end method getTitle    
   //accessor for field copyrightYear    
   public String getCopyrightYear()    
   {    
   return copyrightYear;    
   }    
  }   
Source Code : EnumTest
// Fig. 8.11: EnumTest.java  
 // Testing enum type Book.  
 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());    
   }    
  }   
Output : 
Static Class Members Apabila sebuah variabel didefinisikan static didalam sebuah class, maka untuk memanggil variabel tersebut kita tidak perlu membuat objek dari class tersebut, namun langsung bisa memanggil variabel tersebut dari nama class dimana dia dideklarasikan.
Source Code : Class Employee1
//// Fig. 8.12: Employee.java  
 // Static variable used to maintain a count of the number of  
 // Employee objects in memory.  
 public class Employee1   
  {   
   private String firstName;   
   private String lastName;   
   private static int count = 0;   
   public Employee1(String first, String last)   
   {   
    firstName=first;   
    lastName=last;   
    ++count;   
    System.out.printf( "Employee constructor: %s %s; count = %d\n",    
     firstName, lastName, count);   
   }   
   public String getFirstName()   
   {   
    return firstName;   
   }   
   public String getLastName()   
   {   
    return lastName;   
   }   
   public static int getCount()   
   {   
    return count;   
   }   
  }   
Source Code : Emloyee Test
// Fig. 8.13: EmployeeTest.java  
 // static member demonstration.  
 public class Employee1Test   
  {   
   public static void main(String[] args)   
   {   
    System.out.printf("Employees before instantiation: %d\n", Employee1.getCount() );   
    Employee1 e1 = new Employee1("Susan", "Baker");   
    Employee1 e2 = new Employee1("Bob", "Blue");   
    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 Employee1.getCount(): %d\n", Employee1.getCount());   
    System.out.printf("\nEmployee 1: %s %s\nEmployee 2: %s %s\n",    
     e1.getFirstName(), e1.getLastName(),   
     e2.getFirstName(), e2.getLastName());   
    e1 = null;   
    e2 = null;   
   }   
  }   
Output : 

Komentar

Postingan populer dari blog ini

Konsep Objek

1. Objek Objek merupakan sesuatu yang memiliki identitas (nama), pada umumnya juga memiliki data tentang dirinya maupun object lain dan mempunyai kemampuan untuk melakukan sesuatu dan bisa bekerja sama dengan objek lainnya. Contoh object : Rumah, mobil, sepeda motor, meja, dan komputer merupakan contoh-contoh object yang ada di dunia nyata. 2. Property Property (atau disebut juga dengan atribut) adalah data yang terdapat dalam sebuah class. Melanjutkan analogi tentang laptop. Contoh property dari laptop bisa berupa merk, warna, jenis processor, ukuran layar, dan lain-lain. 3. Method 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.Method adalah tindakan yang bisa dilakukan didalam class. Jika menggunakan analogi class laptop kita. Contoh method adalah : menghidupkan laptop, mematikan laptop, mengganti cove

Rangkuman Buku SYSTEM ANALYSIS EDITION 5TH

Chapter 3: Requirements Determination  Fase analisis  Fase analisis menentukan garis besar tujuan bisnis untuk sistem, menentukan ruang lingkup proyek, menilai kelayakan proyek, dan menyediakan rencana kerja awal. Pada fase ini, seorang system analyst bekerja secara ekstensif dengan klien untuk mengetahui kebutuhan apa saja yang diperlukan untuk sistem yang baru. Proses dasar fase analisis memiliki tiga langkah:  Memahami situasi yang ada (as-is system) Identifikasi perbaikan Menentukan kebutuhan untuk sistem yang baru Penentuan Kebutuhan Penentuan kebutuhan dilakukan untuk mengubah penjelasan tingkat tinggi mengenai kebutuhan bisnis yang tercantum dalam permintaan sistem ke dalam daftar kebutuhan yang lebih tepat. Daftar kebutuhan ini didukung, dikonfirmasi, dan diklarifikasi oleh kegiatan lain dalam fase analisis: membuat use case, membangun proses model, dan membangun data model. Kebutuhan bisnis menggambarkan sistem “apa” dan kebutuhan sistem menggambarkan “bagaimana” sis
// 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 (   "Empployees 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 coount is 2 after creating two Employees          System . out . println (   " \n Employees after instantiation: " ) ;          System . out . printf (   "via e1.getCount(): %d \n " , e1. getCount ( )   ) ;          System . out . printf (   "via e2.getCount(): %d \n " , e2. getCount ( )   ) ;