Online Class - Bengkel Mobile
Salam.., Dear All, pada sesi ini,akan dibuat contoh implementasi program di Java menggunakan konsep OOP untuk Class Relationship menggunakan konsep Agregasi. Ikuti langka-langkah berikut ini :
package agre_driver;
public class Car {
private String name;
private Engine engine;
private Transmision transmision;
public Car(String name){
this.name = name;
}
public void addEngine(Engine e){
engine = e;
}
public void addTransmision(Transmision t){
transmision = t;
}
}
package agre_driver;
public class Engine {
private String name;
private int horsePower;
public Engine(String name){
this.name = name;
}
public int getHorsePower(){
return horsePower;
}
public void setHorsePower(int hp){
this.horsePower = hp;
}
}
Selanjutnya Klik Kanan pada Package ager_Driver | New | Java Class | namakan dengan Transmision.java, tulikan kode program berikut :
- Pada Netbean, Klik File New Project | namakan file dengan agre_driver
- Klik Kanan pada Package agre_driver | New | Java Class | namakan dengan Car.Java, dan tuliskan kode program berikut ini :
package agre_driver;
public class Car {
private String name;
private Engine engine;
private Transmision transmision;
public Car(String name){
this.name = name;
}
public void addEngine(Engine e){
engine = e;
}
public void addTransmision(Transmision t){
transmision = t;
}
}
- Selanjutnya Klik Kanan pada Package ager_Driver | New | Java Class | namakan dengan Engine.Java, tulikan kode program berikut :
package agre_driver;
public class Engine {
private String name;
private int horsePower;
public Engine(String name){
this.name = name;
}
public int getHorsePower(){
return horsePower;
}
public void setHorsePower(int hp){
this.horsePower = hp;
}
}
package agre_driver;
public class Transmision {
private String type;
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
}
- Selanjutnya tulis kode program berikut pada Main CLass atau class agre_driver.java sebagai berikut :
package agre_driver;
public class Agre_Driver {
public static void main(String[] args) {
// TODO code application logic here
Car c = new Car("Honda");
Engine v1000 = new Engine("v1000");
Transmision auto = new Transmision();
auto.setType("Automatic");
c.addEngine(v1000);
c.addTransmision(auto);
//add for display; coba doang :)
System.out.println(auto.getType());
System.out.println(v1000.getHorsePower());
}
}
kode program persis dengan yang diatas, mudahkan..kirim via email ke ader.akatel@gmail.com, subjek : NamaMhs-Kls-BengMob, dikumpul paling lambat Rabu depan, trims :)