PROJECT KELOMPOK 6 – R5L abstract class Bentuk { protected int panjang; protected int lebar; public String getBentuk() { return "Bentuk Dasar"; } public abstract int hitungLuas(); } -------------------------------------------------------------------------------class SegiTiga extends Bentuk { public SegiTiga(int panjang2, int lebar2) { this.panjang = panjang2; this.lebar = lebar2; } //public String getBentuk() { //return "Bentuk Segi Tiga"; //return ""; //} public int hitungLuas() { return this.panjang*this.lebar/2; } } -------------------------------------------------------------------------------class BujurSangkar extends Bentuk { public BujurSangkar(int panjang1, int lebar1) { this.panjang = panjang1; this.lebar = lebar1; } public String getBentuk() { return "Bentuk Bujur Sangkar"; } public int hitungLuas() { return panjang*lebar; } } -------------------------------------------------------------------------------class Polimorfisme { public static void cetakLuasBentuk(Bentuk btk) { System.out.println(btk.getBentuk() + " dengan luas " + btk.hitungLuas()); } public static void main(String[] args) { BujurSangkar bs = new BujurSangkar(10,20); BujurSangkar bs1 = new BujurSangkar(10,20); Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)
SegiTiga st = new SegiTiga(5,10); SegiTiga st1 = new SegiTiga(50,100); cetakLuasBentuk(bs); cetakLuasBentuk(bs1); cetakLuasBentuk(st); cetakLuasBentuk(st1); } } -------------------------------------------------------------------------------Output:
-------------------------------------------------------------------------------//inheritance implementation class X{ void add(){ int a=200,b=300,c; c=a+b; System.out.println("Jumlahnya adalah"+c); } void mult(){ int a=2,b=4,c; c=a*b; System.out.println("hasilnya adalah"+c); } } -------------------------------------------------------------------------------class Y extends X{ void add(){ int a=200,b=300,c; c=(a+b)/2; System.out.println("hasilnya adalah="+c); } void div(){ int a=4,b=2,c; c=a/b; System.out.println("hasilnya adalah="+c); } }
Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)
-------------------------------------------------------------------------------public class hasil{ public static void main (String [] args){ Y ob=new Y(); ob.mult(); ob.add(); ob.div(); } } -------------------------------------------------------------------------------Output:
-------------------------------------------------------------------------------import java.io.*; class nilai { public static void main(String args[])throws Exception { //Membuat objek baru InputStreamReader masukan = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(masukan); //Deklarasi variabel String nama; String a1, a2, a3; double b1, b2, b3, UTS, UAS, NILTUGAS, NILAKHIR; System.out.print("Masukan Nama Mahasiswa : "); nama = input.readLine(); System.out.print("Masukan Nilai UTS : "); a1 = input.readLine(); System.out.print("Masukan Nilai UAS : "); a2 = input.readLine(); System.out.print("Masukan Nilai Tugas : "); a3 = input.readLine(); b1 = Double.parseDouble(a1); b2 = Double.parseDouble(a2); b3 = Double.parseDouble(a3); UTS = 0.35*b1; UAS = 0.45*b2;
Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)
NILTUGAS = 0.2*b3; System.out.println("\t============================"); System.out.println("\t\tNilai Murni UTS : " + UTS); System.out.println("\t\tNilai Murni UAS : " + UAS); System.out.println("\t\tNilai Tugas : "+ NILTUGAS); NILAKHIR = UTS+UAS+NILTUGAS; System.out.println("\t\tNilai Akhirnya Adalah : " + NILAKHIR); System.out.println("\t==================================="); } } -------------------------------------------------------------------------------Output:
-------------------------------------------------------------------------------public class mahasiswa{ private int nrp; private String nama; public mahasiswa (int nrp, String nama){ this.nrp = nrp; this.nama = nama; } public int getNrp () { return nrp; } public String getNama (){ return nama; } } -------------------------------------------------------------------------------public class coba{ public static void main (String args []){ mahasiswa anak = new mahasiswa (20114350, "Diska Rinanda"); System.out.println("NRP : "+anak.getNrp()+"\nNama : "+anak.getNama());
Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)
} } -------------------------------------------------------------------------------Output:
------------------------------------------------------------------------------class mahasiswa1{ private String Npm, Nama, Kegiatan; public String getNpm(){ return Npm; } public void setNpm(String Npm){ this.Npm=Npm; } public String getNama(){ return Nama; } public void setNama(String Nama){ this.Nama=Nama; } public String getKegiatan(){ return Kegiatan; } public void setKegiatan(String Kegiatan){ this.Kegiatan=Kegiatan; } } -------------------------------------------------------------------------------class Dosen{ private String Mtkul, Nama, Kegiatan; public String getMtkul(){ return Mtkul; } public void setMtkul(String Mtkul){ this.Mtkul=Mtkul; } public String getNama(){ return Nama; } public void setNama(String Nama){
Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)
this.Nama=Nama; } public String getKegiatan(){ return Kegiatan; } public void setKegiatan(String Kegiatan){ this.Kegiatan=Kegiatan; } } -------------------------------------------------------------------------------class if_1{ public static void main (String [] args){ System.out.println ("mahasiswa1"); System.out.println ("========="); mahasiswa1 theo = new mahasiswa1(); theo.setNama("theo"); theo.setNpm("201143501537"); theo.setKegiatan("belajar kecakapan antar personal"); System.out.println("No Npm : " + theo.getNpm()); System.out.println("Nama : " + theo.getNama()); System.out.println("Kegiatan : " + theo.getKegiatan() + "\n"); mahasiswa1 diska = new mahasiswa1(); diska.setNama("diska"); diska.setNpm("201143501537"); diska.setKegiatan("belajar mobile programming"); System.out.println("Npm : " + diska.getNpm()); System.out.println("Nama : " + diska.getNama()); System.out.println("Kegiatan : " + diska.getKegiatan() + "\n"); mahasiswa1 riana = new mahasiswa1(); riana.setNama("diska"); diska.setNpm("201143501573"); diska.setKegiatan("belajar OOP programming"); System.out.println("Npm : " + riana.getNpm()); System.out.println("Nama : " + riana.getNama()); System.out.println("Kegiatan : " + riana.getKegiatan() + "\n"); System.out.println("\nDosen"); System.out.println("====="); Dosen Nahot = new Dosen (); Nahot.setKegiatan("Mengajar Java Programming"); Nahot.setMtkul("OOP Lanjutan"); Nahot.setNama("Nahot"); System.out.println("Nama : " + Nahot.getNama()); System.out.println("Mtkul : " + Nahot.getMtkul()); System.out.println("Kegiatan : " + Nahot.getKegiatan() + "\n"); Dosen Dhani = new Dosen(); Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)
Dhani.setKegiatan ("Praktek Membuat Aplikasi Mobile"); Dhani.setMtkul("Mobile Programming"); Dhani.setNama("Ahmad Dhani"); System.out.println("Nama : " + Dhani.getNama()); System.out.println("Mtkul : " + Dhani.getMtkul()); System.out.println("Kegiatan : " + Dhani.getKegiatan() + "\n"); Dosen Riani = new Dosen(); Riani.setKegiatan ("Presntasi Psikologi soft skill"); Riani.setMtkul("Kecakapan Antar Personal"); Riani.setNama("Riani ardianty"); System.out.println("Nama : " + Riani.getNama()); System.out.println("Mtkul : " + Riani.getMtkul()); System.out.println("Kegiatan : " + Riani.getKegiatan() + "\n"); } } -------------------------------------------------------------------------------Output:
Pemrograman Berorientasi Objek | Lukman Andi W (201143501567)