Laporan 3 -Agregasi dan Komposisi
1. IbuAnak.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ibuanak;
/**
*
* @author User
*/
public class IbuAnak {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Manusia ibu1 = new Manusia ("Budi", 30);
Manusia anak1 = new Manusia ("Ani", 4);
Manusia ibu2 = new Manusia ("Diana", 40 );
Manusia anak2 = new Manusia ("Andi", 5, ibu2);
//Relasi antara Manusia dengan Manusia
System.out.println("=========================\n");
ibu1.cetak();
anak1.cetak();
System.out.println("=========================\n");
ibu1.adopsi(anak1);
ibu1.cetak();
anak1.cetak();
System.out.println("=========================\n");
ibu2.cetak();
anak2.cetak();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ibuanak;
/**
*
* @author User
*/
public class Manusia {
private String nama;
private int umur;
private Manusia ibu;
private Manusia anak;
//Letak asosiasi, kelas anak jadi varabel
public Manusia () { }
public Manusia (String nm, int umr)
{
nama = nm;
umur = umr;
ibu = new Manusia();
anak = new Manusia ();
ibu = null;
anak = null;
}
public Manusia (String nm, int umr, Manusia ibu_angkat)
{
nama= nm;
umur = umr;
ibu = new Manusia();
anak = new Manusia();
ibu = ibu_angkat;
ibu_angkat.anak = this;
}
public void adopsi(Manusia anak_angkat)
{
anak=anak_angkat;
anak_angkat.ibu=this;
}
public void cetak()
{
System.out.println("\n DataPribadi -");
System.out.println("Nama :"+nama);
System.out.println("Umur"+umur);
if(ibu!=null)
System.out.println("Nama ibu :"+ibu.nama);
else if(anak!=null)
System.out.println("Nama anak:"+anak.nama);
}
}
2. Relasi
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package relasi;
/**
*
* @author User
*/
public class Relasi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Perusahaan Per= new Perusahaan("Nusantara Jaya");
Pegawai Peg1,Peg2, Peg3;
Peg1 = new Pegawai("P001","Rudi");
Peg2 = new Pegawai("P002","Toni");
Peg3 = new Pegawai("P003","Ani");
Per.insertPegawai(Peg1);
Per.insertPegawai(Peg2);
Per.insertPegawai(Peg3);
System.out.println();
Per.tampilPer();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package relasi;
/**
*
* @author User
*/
public class Perusahaan {
private String namaPer;
private Pegawai peg[];
private int counter;
public Perusahaan(String namaPer)
{
this.namaPer=namaPer;
counter=0;
peg= new Pegawai[3];
System.out.println("Konstructor Perusahaan dijalankan..");
}
public void insertPegawai(Pegawai p)
{
peg[counter]=p;
counter++;
}
public void tampilPer()
{
System.out.println("Perusahaan "+namaPer+"Memiliki Pegawai :");
for (int i=0;i<counter; i++)
{
peg[i].tampilPeg();
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package relasi;
/**
*
* @author User
*/
public class Pegawai {
private String nama;
private String NIP;
public Pegawai()
{
System.out.println("Konstructor pegawai dijalankan...");
}
public Pegawai(String NIP, String nama)
{
this.NIP=NIP;
this.nama=nama;
System.out.println("Konstructor Pegawai dijalankan.......");
}
public void tampilPeg()
{
System.out.println("NIP:"+NIP+", NAMA : "+nama);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ibuanak;
/**
*
* @author User
*/
public class IbuAnak {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Manusia ibu1 = new Manusia ("Budi", 30);
Manusia anak1 = new Manusia ("Ani", 4);
Manusia ibu2 = new Manusia ("Diana", 40 );
Manusia anak2 = new Manusia ("Andi", 5, ibu2);
//Relasi antara Manusia dengan Manusia
System.out.println("=========================\n");
ibu1.cetak();
anak1.cetak();
System.out.println("=========================\n");
ibu1.adopsi(anak1);
ibu1.cetak();
anak1.cetak();
System.out.println("=========================\n");
ibu2.cetak();
anak2.cetak();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ibuanak;
/**
*
* @author User
*/
public class Manusia {
private String nama;
private int umur;
private Manusia ibu;
private Manusia anak;
//Letak asosiasi, kelas anak jadi varabel
public Manusia () { }
public Manusia (String nm, int umr)
{
nama = nm;
umur = umr;
ibu = new Manusia();
anak = new Manusia ();
ibu = null;
anak = null;
}
public Manusia (String nm, int umr, Manusia ibu_angkat)
{
nama= nm;
umur = umr;
ibu = new Manusia();
anak = new Manusia();
ibu = ibu_angkat;
ibu_angkat.anak = this;
}
public void adopsi(Manusia anak_angkat)
{
anak=anak_angkat;
anak_angkat.ibu=this;
}
public void cetak()
{
System.out.println("\n DataPribadi -");
System.out.println("Nama :"+nama);
System.out.println("Umur"+umur);
if(ibu!=null)
System.out.println("Nama ibu :"+ibu.nama);
else if(anak!=null)
System.out.println("Nama anak:"+anak.nama);
}
}
2. Relasi
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package relasi;
/**
*
* @author User
*/
public class Relasi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Perusahaan Per= new Perusahaan("Nusantara Jaya");
Pegawai Peg1,Peg2, Peg3;
Peg1 = new Pegawai("P001","Rudi");
Peg2 = new Pegawai("P002","Toni");
Peg3 = new Pegawai("P003","Ani");
Per.insertPegawai(Peg1);
Per.insertPegawai(Peg2);
Per.insertPegawai(Peg3);
System.out.println();
Per.tampilPer();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package relasi;
/**
*
* @author User
*/
public class Perusahaan {
private String namaPer;
private Pegawai peg[];
private int counter;
public Perusahaan(String namaPer)
{
this.namaPer=namaPer;
counter=0;
peg= new Pegawai[3];
System.out.println("Konstructor Perusahaan dijalankan..");
}
public void insertPegawai(Pegawai p)
{
peg[counter]=p;
counter++;
}
public void tampilPer()
{
System.out.println("Perusahaan "+namaPer+"Memiliki Pegawai :");
for (int i=0;i<counter; i++)
{
peg[i].tampilPeg();
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package relasi;
/**
*
* @author User
*/
public class Pegawai {
private String nama;
private String NIP;
public Pegawai()
{
System.out.println("Konstructor pegawai dijalankan...");
}
public Pegawai(String NIP, String nama)
{
this.NIP=NIP;
this.nama=nama;
System.out.println("Konstructor Pegawai dijalankan.......");
}
public void tampilPeg()
{
System.out.println("NIP:"+NIP+", NAMA : "+nama);
}
}
Komentar
Posting Komentar