Buat implementasi dari method sebagai berikut :
1. Menyediakan accessor dan mutator method terhadap seluruh atribut
2. Constructor
Langkah Pertama buat file dengan nama : StudentRecord.java, Kemudian ketikkan coding sebagai berikut:
public class StudentRecord { private String[] name=new String [100]; private String[] alamate=new String [100]; private String[] telpone=new String [100]; private String[] emaile=new String [100]; private static int studentCount; private static int indek; public StudentRecord(){ studentCount=0; } public String getName(int indek){ return name[indek]; } public String getAlamat(int indek){ return alamate[indek]; } public String getTelpon(int indek){ return telpone[indek]; } public String getEmail(int indek){ return emaile[indek]; } public void setName( String nama, int indek,String alamat,String telpon,String email){ name[indek] = nama; alamate[indek] = alamat; telpone[indek] = telpon; emaile[indek] = email; studentCount++; } public static int getStudentCount(){ return studentCount; } }Langkah Kedua buat file dengan nama : StudentRecordExample.java, Kemudian ketikkan coding sebagai berikut:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class StudentRecordExample { public static void main( String[] args ){ BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in)); String [] nama =new String[100]; String [] alamat =new String[100]; String [] telp =new String[100]; String [] email =new String[100]; //membuat 5 object StudentRecord StudentRecord namaRecord = new StudentRecord(); for(int i=0; i<2; i++ ){ System.out.print("Input Nama: "); try{ nama[i]=dataIn.readLine(); }catch(IOException e){ System.out.println("Erorr!"); } System.out.print("Input Alamat: "); try{ alamat[i]=dataIn.readLine(); }catch(IOException e){ System.out.println("Erorr!"); } System.out.print("Input telpon: "); try{ telp[i]=dataIn.readLine(); }catch(IOException e){ System.out.println("Erorr!"); } System.out.print("Input Email: "); try{ email[i]=dataIn.readLine(); }catch(IOException e){ System.out.println("Erorr!"); } } for(int i=0; i<2; i++ ){ namaRecord.setName(nama[i],i,alamat[i],telp[i],email[i]); System.out.println("==========================================="); System.out.println("Nama :"+namaRecord.getName(i) ); System.out.println("Alamat :"+namaRecord.getAlamat(i) ); System.out.println("Telepon :"+namaRecord.getTelpon(i) ); System.out.println("Email :"+namaRecord.getEmail(i) ); System.out.println("==========================================="); } System.out.println("\nTotal Jumlah Siswa ="+StudentRecord.getStudentCount()); } }
makasi bgt nih, postnya ngebantu bgt :D
ReplyDelete