Contoh Java
Interface.java
package pkginterface; public class Interface { public static void main(String[] args) { segiempat se = new segiempat(20,10); se.luas(); se.keliling(); balok b= new balok (10,5,7); b.luas(); b.keliling(); b.volume(); } }Balok.java
package pkginterface; public class balok implements bangun_ruang,bangun_3d { private int p; private int l; private int t; public balok(int panjang,int lebar,int tinggi){ p=panjang; l=lebar; t=tinggi; } @Override public void keliling() { System.out.println("keliling balok :"+(4*(p+l+t))); } @Override public void luas() { System.out.println("luas balok :"+(2*((p*l)+(p*t)+(p*t)))); } @Override public void volume() { System.out.println("volume balok :"+(p*l*t)); } }bangun_3d.java
package pkginterface; public interface bangun_3d { public void volume(); }bangun_ruang.java
package pkginterface; public interface bangun_ruang { public void keliling(); public void luas(); }segiempat.java
package pkginterface; public class segiempat implements bangun_ruang { private int p; private int l; public segiempat(int panjang,int lebar){ p =panjang; l= lebar; } @Override public void keliling() { System.out.println("kaliling segi empat :"+(2 *(p+l))); } @Override public void luas() { System.out.println("luas segi empat:"+(p*l)); } }Contoh C++
main.cpp
#include#include using namespace std; class interface_bangunruang { void luas(); void keliling(); }; class interface_bangun3d { void volume(); }; class kubus { public: int rusuk; virtual void keliling() { cout << "keliling kubus =" << 12*rusuk << endl; } virtual void luas() { cout << "luas kubus =" << 6 *(rusuk*rusuk) << endl; } virtual void volume() { cout << "volume kubus =" << rusuk*rusuk*rusuk << endl; } }; class balok { public: int panjang; int lebar; int tinggi; virtual void keliling() { cout << "keliling balok =" << 4*(panjang+lebar+tinggi) << endl; } virtual void luas() { cout << "luas balok =" << 2*((panjang*lebar)+(panjang*tinggi)+(lebar*tinggi)) << endl; } virtual void volume() { cout << "volume balok=" << (panjang*lebar*tinggi) << endl; } }; class persegi { public : int sisi; virtual void keliling() { cout << " keliling persegi =" << 4*sisi << endl; } virtual void luas() { cout << "luas persegi =" << sisi*sisi << endl; } }; class persegi_panjang { public: int p; int l; virtual void keliling() { cout << "keliling persegi panjang = " << 2 *(p+l) << endl; } virtual void luas() { cout << "luas persegi panjang =" << p*l << endl; } }; int main() { cout << " INTERFACE " << endl; cout << "" << endl; cout << "" << endl; kubus k; cout << "KUBUS" << endl; cout << " masukkan nilai rusuk ="; cin >> k.rusuk; k.keliling(); k.luas(); k.volume(); balok b; cout<<"" << endl; cout<<" BALOK " << endl; cout<<"masukkan nilai panjang = "; cin>>b.panjang; cout<<"masukkan nilai lebar = "; cin>>b.lebar; cout<<"masukkan nilai tinggi = "; cin>>b.tinggi; b.keliling(); b.luas(); b.volume(); persegi ps; cout << "" << endl; cout << "PERSEGI" << endl; cout << "masukkan nilai sisi ="; cin >> ps.sisi; ps.keliling(); ps.luas(); persegi_panjang pp ; cout << ""<< endl; cout << " PERSEGI PANJANG " << endl; cout << " masukkan nilai panjang ="; cin >> pp.p; cout << "masukkan nilai lebar ="; cin >> pp.l; pp.keliling(); pp.luas(); return 0; }
Contoh Csharp
Program.cs
using System; namespace classinterface { class Program { public static void Main(string[] args) { Console.WriteLine(" interface"); Console.WriteLine(); Console.WriteLine(); kubus k=new kubus(); Console.WriteLine("KUBUS"); Console.Write("masukkan nilai rusuk ="); k.rusuk =Convert.ToInt32(Console.ReadLine()); k.keliling(); k.luas(); k.volume(); balok b = new balok(); Console.WriteLine(); Console.WriteLine(" BALOK "); Console.Write(" masukkan nilai panjang = "); b.panjang=Convert.ToInt32(Console.ReadLine()); Console.Write("masukkan nilai lebar ="); b.lebar=Convert.ToInt32(Console.ReadLine()); Console.Write(" masukkan nilai ="); b.tinggi=Convert.ToInt32(Console.ReadLine()); b.keliling(); b.luas(); b.volume(); persegi pe = new persegi(); Console.WriteLine(); Console.WriteLine("PERSEGI"); Console.Write("masukkan nilai sisi ="); pe.sisi = Convert.ToInt32(Console.ReadLine()); pe.keliling(); pe.luas(); persegipanjang pp = new persegipanjang(); Console.WriteLine(); Console.WriteLine(" PERSEGI PANJANG "); Console.Write("masukkan nilai panjang ="); pp.p = Convert.ToInt32(Console.ReadLine()); Console.Write("masukkan nilai lebar="); pp.l = Convert.ToInt32(Console.ReadLine()); pp.keliling(); pp.luas(); Console.ReadKey(true); } } }persegipanjangbangunruang.cs
using System; namespace classinterface { class persegipanjang : bangunruang { public int p; public int l; public virtual void keliling() { Console.WriteLine(" Keliling persegi panjang = "+( 2*(p+l))); } public virtual void luas() { Console.WriteLine(" luas persegi panjang = "+((p*l))); } } }persegibangunruang.cs
using System; namespace classinterface { class persegi : bangunruang { public int sisi; public virtual void keliling() { Console.WriteLine(" Keliling persegi = "+(4*sisi)); } public virtual void luas() { Console.WriteLine(" Luas persegi = "+(sisi*sisi)); } } }
0 komentar:
Posting Komentar