Java Programming Inheritance Geometri with extends class Circle, Rectangel, Triangel
//class Geometri
package tugasapbo;
public class Geometri {
protected double alas;
protected double phi;
protected double r;
protected double tinggi;
protected int sisi;
public Geometri() {
}
public Geometri(double alas, double phi, double r, int sisi, double tinggi) {
this.alas = alas;
this.phi = phi;
this.jarijari = r;
this.tinggi = tinggi;
this.sisi = sisi;
}
public double getAlas() {
return alas;
}
public void setAlas(double alas) {
this.alas = alas;
}
public double getPhi() {
return phi;
}
public void setPhi(double phi) {
this.phi = phi;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getTinggi() {
return tinggi;
}
public void setTinggi(double tinggi) {
this.tinggi = tinggi;
}
public int getSisi() {
return sisi;
}
public void setSisi(int sisi) {
this.sisi = sisi;
}
}
//class Circle
package tugasapbo;
public class Circle extends Geometri{
private double LuasC;
private double KelilingC;
public Circle() {
}
public Circle(double LuasC) {
this.LuasC = LuasC;
super.phi = phi;
super.r = r;
}
public double getLuasC() {
return LuasC=phi*r*r;
}
public double getKelilingC() {
return KelilingC= 2*phi*r;
}
}
//class Rectangel
package tugasapbo;
public class Rectangel extends Geometri{
private int luas;
private int kelilingR;
public Rectangel() {}
public Rectangel(int luas, int kelilingR) {
this.luas = luas;
this.kelilingR = kelilingR;
super.sisi = sisi;
}
public int getLuas() {
return luas = sisi*sisi;
}
public int getKelilingR() {
return kelilingR = 4*sisi;
}
}
//class Triangel
package tugasapbo;
public class Triangel extends Geometri {
private double luasT;
public Triangel() {
}
public Triangel(double luasT) {
this.luasT = luasT;
super.alas = alas;
super.tinggi = tinggi;
}
public double getLuasT() {
return luasT= 1.5*alas*tinggi;
}
}
// main function
package tugasapbo;
import java.util.Scanner;
public class TugasApbo {
public static void main(String[] args) {
Rectangel A= new Rectangel();
Scanner input= new Scanner(System.in);
System.out.println(" TUGAS KULIAH APBO ");
System.out.println("Masukkan Sisi :");
A.setSisi(input.nextInt());
System.out.println("Keliling Rectangel = "+A.getKelilingR());
System.out.println("Luas Rectangel = "+A.getLuas());
System.out.println("<<<===============================>>>");
Triangel B= new Triangel();
Scanner input2= new Scanner(System.in);
System.out.println("Masukkan Alasnya :");
B.setAlas(input2.nextDouble());
System.out.println("Masukkan Tingginya :");
B.setTinggi(input2.nextDouble());
System.out.println("Luas Triangel = "+B.getLuasT());
System.out.println("<<<===============================>>>");
Circle C= new Circle();
Scanner input3= new Scanner(System.in);
C.setPhi(3.14);
System.out.println("Masukkan Jari-jarinya :");
C.setJarijari(input3.nextDouble());
System.out.println("Luas Lingkarannya :"+C.getLuasC());
System.out.println("Keliling Lingkarannya :"+C.getKelilingC());
System.out.println("<<<===============================>>>");
}
}
Output :
package tugasapbo;
public class Geometri {
protected double alas;
protected double phi;
protected double r;
protected double tinggi;
protected int sisi;
public Geometri() {
}
public Geometri(double alas, double phi, double r, int sisi, double tinggi) {
this.alas = alas;
this.phi = phi;
this.jarijari = r;
this.tinggi = tinggi;
this.sisi = sisi;
}
public double getAlas() {
return alas;
}
public void setAlas(double alas) {
this.alas = alas;
}
public double getPhi() {
return phi;
}
public void setPhi(double phi) {
this.phi = phi;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getTinggi() {
return tinggi;
}
public void setTinggi(double tinggi) {
this.tinggi = tinggi;
}
public int getSisi() {
return sisi;
}
public void setSisi(int sisi) {
this.sisi = sisi;
}
}
//class Circle
package tugasapbo;
public class Circle extends Geometri{
private double LuasC;
private double KelilingC;
public Circle() {
}
public Circle(double LuasC) {
this.LuasC = LuasC;
super.phi = phi;
super.r = r;
}
public double getLuasC() {
return LuasC=phi*r*r;
}
public double getKelilingC() {
return KelilingC= 2*phi*r;
}
}
//class Rectangel
package tugasapbo;
public class Rectangel extends Geometri{
private int luas;
private int kelilingR;
public Rectangel() {}
public Rectangel(int luas, int kelilingR) {
this.luas = luas;
this.kelilingR = kelilingR;
super.sisi = sisi;
}
public int getLuas() {
return luas = sisi*sisi;
}
public int getKelilingR() {
return kelilingR = 4*sisi;
}
}
//class Triangel
package tugasapbo;
public class Triangel extends Geometri {
private double luasT;
public Triangel() {
}
public Triangel(double luasT) {
this.luasT = luasT;
super.alas = alas;
super.tinggi = tinggi;
}
public double getLuasT() {
return luasT= 1.5*alas*tinggi;
}
}
// main function
package tugasapbo;
import java.util.Scanner;
public class TugasApbo {
public static void main(String[] args) {
Rectangel A= new Rectangel();
Scanner input= new Scanner(System.in);
System.out.println(" TUGAS KULIAH APBO ");
System.out.println("Masukkan Sisi :");
A.setSisi(input.nextInt());
System.out.println("Keliling Rectangel = "+A.getKelilingR());
System.out.println("Luas Rectangel = "+A.getLuas());
System.out.println("<<<===============================>>>");
Triangel B= new Triangel();
Scanner input2= new Scanner(System.in);
System.out.println("Masukkan Alasnya :");
B.setAlas(input2.nextDouble());
System.out.println("Masukkan Tingginya :");
B.setTinggi(input2.nextDouble());
System.out.println("Luas Triangel = "+B.getLuasT());
System.out.println("<<<===============================>>>");
Circle C= new Circle();
Scanner input3= new Scanner(System.in);
C.setPhi(3.14);
System.out.println("Masukkan Jari-jarinya :");
C.setJarijari(input3.nextDouble());
System.out.println("Luas Lingkarannya :"+C.getLuasC());
System.out.println("Keliling Lingkarannya :"+C.getKelilingC());
System.out.println("<<<===============================>>>");
}
}
Output :
Comments
Post a Comment