From ad76b6be5560d480ec264390d0bc67777794a725 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sat, 26 Sep 2020 06:16:00 +0300 Subject: [PATCH] lab-02: - Added remaining classes. - Added abstract class shape. - _Area Interface. - Lab test program. --- lab-02/.idea/uiDesigner.xml | 124 ++++++++++++++++++++++++++++++++++++ lab-02/src/Circle.java | 22 +++++++ lab-02/src/Triangle.java | 31 +++++++++ lab-02/src/_Area.java | 3 + lab-02/src/_Rectangle.java | 6 +- lab-02/src/_Shape.java | 9 ++- lab-02/src/testLab2.java | 30 +++++++++ 7 files changed, 221 insertions(+), 4 deletions(-) create mode 100644 lab-02/.idea/uiDesigner.xml create mode 100644 lab-02/src/Circle.java create mode 100644 lab-02/src/Triangle.java create mode 100644 lab-02/src/_Area.java create mode 100644 lab-02/src/testLab2.java diff --git a/lab-02/.idea/uiDesigner.xml b/lab-02/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/lab-02/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lab-02/src/Circle.java b/lab-02/src/Circle.java new file mode 100644 index 0000000..709ef91 --- /dev/null +++ b/lab-02/src/Circle.java @@ -0,0 +1,22 @@ +public class Circle extends _Shape { + + private double radius; + + public Circle(double radius){ + super("Circle"); + setRadius(radius); + } + + public void setRadius(double radius){ + if (radius >= 0) this.radius = radius; + else this.radius = 1; + } + + public double getRadius(){ return radius; } + + public double getArea() { return Math.PI*Math.pow(radius, 2); } + + public String toString(){ + return String.format("%s%.2f\n", super.toString(), this.getArea()); + } +} diff --git a/lab-02/src/Triangle.java b/lab-02/src/Triangle.java new file mode 100644 index 0000000..1f84239 --- /dev/null +++ b/lab-02/src/Triangle.java @@ -0,0 +1,31 @@ +public class Triangle extends _Shape { + + private double base; + private double height; + + public Triangle(double base, double height){ + super("Triangle"); + setBase(base); + setHeight(height); + } + + public void setBase(double base){ + if (base != 0) this.base = base; + else this.base = 1; + } + + public void setHeight(double height){ + if (height != 0) this.height = height; + else this.height = 1; + } + + public double getBase() { return this.base; } + + public double getHeight() {return this.height; } + + public double getArea() { return (this.base*this.height)/2; } + + public String toString() { + return String.format("%s%.2f\n", super.toString(), this.getArea()); + } +} diff --git a/lab-02/src/_Area.java b/lab-02/src/_Area.java new file mode 100644 index 0000000..bfe50ff --- /dev/null +++ b/lab-02/src/_Area.java @@ -0,0 +1,3 @@ +public interface _Area { + public double getArea(); +} diff --git a/lab-02/src/_Rectangle.java b/lab-02/src/_Rectangle.java index 3dc14a6..01a115d 100644 --- a/lab-02/src/_Rectangle.java +++ b/lab-02/src/_Rectangle.java @@ -4,12 +4,12 @@ public class _Rectangle extends _Shape { private double height; public _Rectangle(double w){ + this(w, w); //Since only width or height is given, assume square. - setWidth(w); - setHeight(w); } public _Rectangle(double w, double h){ + super("Rectangle"); setWidth(w); setHeight(h); } @@ -32,6 +32,6 @@ public class _Rectangle extends _Shape { public double getHeight(){ return this.height; } public String toString() { - return " ";//TODO: _Rectangle toString output format + return String.format("%s%.2f\n", super.toString(), this.getArea()); } } diff --git a/lab-02/src/_Shape.java b/lab-02/src/_Shape.java index 79c0037..76df287 100644 --- a/lab-02/src/_Shape.java +++ b/lab-02/src/_Shape.java @@ -1,9 +1,16 @@ -public abstract class _Shape { +public abstract class _Shape implements _Area { private String name; + public _Shape(String name){ this.name = name; } + public void setName(String name) { this.name = name; } public String getName(){ return name; } + public String toString(){ + return String.format("Class name is %s\n" + + "Area = ", this.name); + } + } diff --git a/lab-02/src/testLab2.java b/lab-02/src/testLab2.java new file mode 100644 index 0000000..b0bf9d7 --- /dev/null +++ b/lab-02/src/testLab2.java @@ -0,0 +1,30 @@ + +public class testLab2 { + public static void main(String args[]){ + _Shape[] shapes = new _Shape[3]; + shapes[0] = new Circle(2.2); + shapes[1] = new Triangle(4.9, 7.43); + shapes[2] = new _Rectangle(4.5); + + + for (_Shape shape : shapes) { + System.out.println(shape.toString()); + } + + Circle c1 = new Circle(3.9); + ((_Area)c1).getArea(); + //_Shape s = new _Rectangle(4.1); + //_Area area = new _Area(); + _Area[] areas = new _Area[2]; + areas[0] = new _Rectangle(6.5); + areas[1] = new Circle(5.5); + + _Shape shape2 = new Triangle(4.1, 7.3); + shape2.getName(); + shape2.getArea(); + //shape2.base; + + _Shape r2 = new _Rectangle(8.94); + if (r2 instanceof _Rectangle) ((_Rectangle) r2).setHeight(5); + } +}