package pl.am.object2.lesson38;
public class Abstrakcyjna {
public static void main(String[] args) {
Figura figura = new Trojkat(6, 5);
System.out.println(figura);
}
}
package pl.am.object2.lesson38;
public abstract class Figura {
public abstract double obliczPole();
}
package pl.am.object2.lesson38;
public class Kwadrat extends Prostokat {
public Kwadrat(double bok) {
super(bok, bok);
}
public double getBok() {
return a;
}
@Override
public String toString() {
return "Kwadrat{" +
"bok=" + a +
", pole=" + obliczPole() +
", obwód=" + obliczObwod() +
'}';
}
}
package pl.am.object2.lesson38;
import java.util.Objects;
public class Prostokat extends Figura {
protected final double a;
protected final double b;
public Prostokat(double a, double b) {
this.a = a;
this.b = b;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
@Override
public double obliczPole() {
return a * b;
}
public double obliczObwod() {
return 2*a + 2*b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Prostokat prostokat = (Prostokat) o;
return Double.compare(prostokat.a, a) == 0 &&
Double.compare(prostokat.b, b) == 0;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "Prostokat{" +
"a=" + a +
", b=" + b +
", pole=" + obliczPole() +
", obwód=" + obliczObwod() +
'}';
}
}
package pl.am.object2.lesson38;
public class Trojkat extends Figura {
private final double podstawa;
private final double wysokosc;
public Trojkat(double podstawa, double wysokosc) {
this.podstawa = podstawa;
this.wysokosc = wysokosc;
}
@Override
public double obliczPole() {
return 0.5 * podstawa * wysokosc;
}
@Override
public String toString() {
return "Trojkat{" +
"podstawa=" + podstawa +
", wysokosc=" + wysokosc +
", pole=" + obliczPole() +
'}';
}
}