From f1f961dd14225c87e8dd10ce03da1e8e99bd04f3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Hypki Date: Tue, 28 Mar 2023 16:24:07 +0200 Subject: [PATCH] Implemented Figures classes with interface Area; --- src/net/hypki/testjava/figures/Figure.java | 6 ++---- src/net/hypki/testjava/main/Main.java | 10 +++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/net/hypki/testjava/figures/Figure.java b/src/net/hypki/testjava/figures/Figure.java index e945eee..0a6da9a 100644 --- a/src/net/hypki/testjava/figures/Figure.java +++ b/src/net/hypki/testjava/figures/Figure.java @@ -1,13 +1,11 @@ package net.hypki.testjava.figures; -public abstract class Figure { +public abstract class Figure implements Area { private int id = -1; private String name; - - public abstract double computeArea(); - + public Figure() { setName("Figure"); } diff --git a/src/net/hypki/testjava/main/Main.java b/src/net/hypki/testjava/main/Main.java index 8e55ed1..788de97 100644 --- a/src/net/hypki/testjava/main/Main.java +++ b/src/net/hypki/testjava/main/Main.java @@ -30,11 +30,15 @@ public class Main { Triangle t = new Triangle(1, 2); System.out.println(t.getColor()); - Area a = new Triangle(4, 10); + Area a = new Triangle(4, 10); + a = new Rectangle(4, 10); + a = new Circle(10.0); System.out.println(a); - - System.out.println(((Triangle) a).getColor()); + if (a instanceof Triangle) + System.out.println(((Triangle) a).getColor()); + else if (a instanceof Circle) + System.out.println(((Circle) a).getRadius()); } }