Contoh Abstract Class :
abstract_class_java.java
package abstract_class_java; public class Abstract_class_java { public static void main(String[] args) { SegiEmpat se = new SegiEmpat(20, 10); se.Luas(); se.Keliling(); BujurSangkar bj = new BujurSangkar(8); bj.Luas(); bj.Keliling(); SegiTiga st = new SegiTiga(3,10,0.5,5); st.Luas(); st.Keliling(); } }
BujurSangkar.java
package abstract_class_java; public class BujurSangkar extends bangun_ruang { private int s; public BujurSangkar(int sisi){ s = sisi; } @Override public void Luas() { System.out.println("Luas Bujur Sangkar : "+(s * s)); } @Override public void Keliling() { System.out.println("Keliling Bujur Sangkar : "+(4 * s)); } }
SegiEmpat.java
package abstract_class_java; public class SegiEmpat extends bangun_ruang{ private int p; private int l; public SegiEmpat(int panjang, int lebar){ p = panjang; l = lebar; } @Override public void Luas() { System.out.println("Luas Segi Empat : "+(p * l)); } @Override public void Keliling() { System.out.println("Keliling Segi Empat : "+ (2 * (p + l)) ); } }bangunruang.java
package abstract_class_java; abstract class bangun_ruang { abstract public void Luas(); abstract public void Keliling(); }SegiTiga.java
package abstract_class_java; public class SegiTiga extends bangun_ruang { private int a; private int t; private double s; private int sm; public SegiTiga(int alas, int tinggi, double set, int sisimiring){ a = alas; t = tinggi; s = set; sm = sisimiring; } @Override public void Luas() { System.out.println("Luas Segi Tiga : "+((a * t)* s)); } @Override public void Keliling() { System.out.println("Keliling Segi Tmpat : "+(a + t + sm)); } }
Contoh C++.
main.cpp
#includeContoh Csharp#include using namespace std; class abstrak { public : void Luas(); void Keliling(); }; class bujursangkar { public : int x; virtual void Luas() { cout <<" Luas bujursangkar = " << (x*x) << endl; } virtual void Keliling() { cout <<" Keliling bujursangkar = " << (x+x) << endl; } }; class segiempat { public: int p; int l; virtual void Luas() { cout <<" Luas segiempat = " << (p*l) << endl; } virtual void Keliling() { cout <<" Keliling segiempat = "<<(2*(p+l)) << endl; } }; class segitiga { public : int s; int a; int t; virtual void Luas() { cout <<" Luas segitiga = "<<((a*t)/2) << endl; } virtual void Keliling() { cout <<" Keliling segitiga = "<<(s*s*s) << endl; } }; int main() { cout <<" ABSTRACT CLASS" << endl; cout <<" MENGHITUNG LUAS & KELILING BANGUN RUANG " << endl; cout <<""<< endl; cout <<" 1. Bujursangkar " << endl; bujursangkar b; cout << " Masukkan nilai sisi = "; cin >> b.x; b.Luas(); b.Keliling(); cout <<"" << endl; cout <<" 2. Segiempat" << endl; segiempat se; cout << " Masukkan nilai panjang = "; cin >> se.p; cout << " Masukkan nilai lebar = "; cin >> se.l; se.Luas(); se.Keliling(); cout <<"" << endl; cout <<" 3. Segitiga" << endl; segitiga st; cout <<" Masukkan nilai alas = "; cin >> st.a; cout <<" Masukkan nilai tinggi = "; cin >> st.t; cout <<" Masukkan nilai sisi = "; cin >> st.s; st.Luas(); st.Keliling(); return 0; }
program.cs
using System; namespace Abstrakclass { class Program { public static void Main(string[] args) { Console.WriteLine(" LUAS DAN KELILING "); Console.WriteLine(" BANGUN RUANG "); Console.WriteLine(); Console.WriteLine(" 1. Bujur Sangkar "); bujursangkar bj = new bujursangkar(); Console.Write(" Masukkan nilai sisi = "); bj.x = Convert.ToInt32(Console.ReadLine()); bj.Luas(); bj.Keliling(); Console.WriteLine(); Console.WriteLine(" 2. Segi Empat"); segiempat sg = new segiempat(); Console.Write(" Masukkan nilai panjang = "); sg.p = Convert.ToInt32(Console.ReadLine()); Console.Write(" Masukkan nilai lebar = "); sg.l = Convert.ToInt32(Console.ReadLine()); sg.Luas(); sg.Keliling(); Console.WriteLine(); Console.WriteLine(" 3. Segitiga"); segitiga st = new segitiga(); Console.Write(" Masukkan nilai alas = "); st.a = Convert.ToInt32(Console.ReadLine()); Console.Write(" Masukkan nilai tinggi = "); st.t = Convert.ToInt32(Console.ReadLine()); Console.Write(" Masukkan nilai sisi = "); st.s = Convert.ToInt32(Console.ReadLine()); st.Luas(); st.Keliling(); Console.ReadKey(true); } } }bangunruang.cs
using System; namespace Abstrakclass { abstract class bangunruang { public abstract void Luas(); public abstract void Keliling(); } }bujursangkar.cs
using System; namespace Abstrakclass { class bujursangkar : bangunruang { public int x; public override void Luas() { Console.WriteLine(" Luas Bujursangkar = "+(x*x)); } public override void Keliling() { Console.WriteLine(" Keliling Bujursangkar = "+(x+x)); } } }segiempat.cs
using System; namespace Abstrakclass { class segiempat : bangunruang { public int p; public int l; public override void Luas() { Console.WriteLine(" Luas segiempat = "+(p*l)); } public override void Keliling() { Console.WriteLine(" Keliling segiempat = "+(2*(p+l))); } } }segitiga.cs
using System; namespace Abstrakclass { class segitiga : bangunruang { public int s=0; public int a; public int t; public override void Luas() { Console.WriteLine(" Luas segitiga = "+((a*t)/2)); } public override void Keliling() { Console.WriteLine(" Keliling Segitiga = "+(s*s*s)); } } }
0 komentar:
Posting Komentar