Rabu, 21 Oktober 2009

SuperClass.java

class SuperClass {
public void Handphone() {
System.out.println("Contoh Macam-macam Handphone :");
System.out.println("- Handphone Nokia");
System.out.println("- Handphone Sony Ericsson");
System.out.println("- Handphone BlackBerry");
}
public static void main(String[] args) {
SuperClass sc = new SuperClass();
sc.Handphone();
}
}
Output SuperClass.java:

C:\Documents and Settings\INTEL>d:
D:\>javac SuperClass.java
D:\>java SuperClass
Contoh Macam-macam Handphone :
- Handphone Nokia
- Handphone Sony Ericsson
- Handphone BlackBerry

D:\>


SubClass.java

class SubClass extends SuperClass {
public void Panggil() {
super.Handphone();
System.out.println("Sumber Handphone :");
}

public void HandphoneNokia() {
System.out.println("- Handphone Nokia : E71, E86, E63, dll");
}
public void HandphoneSonyEricsson() {
System.out.println("- Handphone Sony Ericsson : J230i, W380, W350 dll");
}
public void HandphoneBlackBerry() {
System.out.println("- Handphone BlackBerry : Bold 9900, Strom 8900, dll");
}

public static void main(String[]args) {
SubClass scs = new SubClass();
scs.Panggil();
scs.HandphoneNokia();
scs.HandphoneSonyEricsson();
scs.HandphoneBlackBerry();
}
}

Output SubClass.java:

C:\Documents and Settings\INTEL>d:
D:\>javac SubClass.java
D:\>java SubClass
Contoh Macam-macam Handphone :
- Handphone Nokia
- Handphone Sony Ericsson
- Handphone BlackBerry
Sumber Handphone :
- Handphone Nokia : E71, E86, E63, dll
- Handphone Sony Ericsson : J230i, W380, W350 dll
- Handphone BlackBerry : Bold 9900, Strom 8900, dll

D:\>

Segitiga.java

public class Segitiga{
private double alas;
private double tinggi;
public Segitiga() {
alas = 0;
tinggi = 0;
}
private double luas(double a, double t) { // di hide
return (a*t)/2;
}
public void setAlas(double alas) {
this.alas = alas;
}
public void setTinggi(double tinggi){
this.tinggi = tinggi;
}
public double getAlas() {
return alas;
}
public double getTinggi(){
return tinggi;
}
public double getLuas() {
return luas(alas, tinggi);
}
}


MainClass.java

public class MainSegitiga extends Segitiga{
public static void main(String[] srgs) {
Segitiga bk = new Segitiga();
bk.setAlas(40);
bk.setTinggi(20);
System.out.println("Alas : "+ bk.getAlas());
System.out.println("Tinggi : "+ bk.getTinggi());
System.out.println("Luas : "+ bk.getLuas());
}
}

Output:

C:\Documents and Settings\INTEL>d:
D:\>javac Segitiga.java
D:\>java Segitiga
Exception in thread "main" java.lang.NoSuchMethodError: main
D:\>javac MainSegitiga.java
D:\>java MainSegitiga
Alas : 40.0
Tinggi : 20.0
Luas : 400.0

D:\>

Tidak ada komentar:

Posting Komentar