From 60944a922806272205a0d6e949b31c1da21147f5 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 28 Mar 2023 23:34:18 +0100 Subject: [PATCH] [POO] aula07 shapes update --- 1ano/2semestre/poo/src/aula07/ex1/Circle.java | 19 ++++++++++++----- .../poo/src/aula07/ex1/Rectangle.java | 20 ++++++++++++------ 1ano/2semestre/poo/src/aula07/ex1/Shape.java | 1 + .../poo/src/aula07/ex1/Triangle.java | 21 ++++++++++++------- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/1ano/2semestre/poo/src/aula07/ex1/Circle.java b/1ano/2semestre/poo/src/aula07/ex1/Circle.java index d83306b..2904f9a 100644 --- a/1ano/2semestre/poo/src/aula07/ex1/Circle.java +++ b/1ano/2semestre/poo/src/aula07/ex1/Circle.java @@ -1,12 +1,13 @@ package aula07.ex1; +import java.util.Objects; + public class Circle extends Shape { private double radius; - public Circle(double radius) { - if (radius <= 0) - throw new IllegalArgumentException("Sizes must be positive."); - this.radius = radius; + public Circle(String color, double radius) { + this.setColor(color); + this.setRadius(radius); } public double getRadius() { @@ -19,6 +20,14 @@ public class Circle extends Shape { this.radius = radius; } + public String getColor() { + return this.color; + } + + public void setColor(String color) { + this.color = color; + } + @Override public double getArea() { return Math.PI * Math.pow(this.radius, 2); @@ -32,7 +41,7 @@ public class Circle extends Shape { @Override public boolean equals(Shape s2) { if (s2 instanceof Circle s2Circ) - return this.radius == s2Circ.radius; + return Objects.equals(this.color, s2Circ.color) && this.radius == s2Circ.radius; return false; } diff --git a/1ano/2semestre/poo/src/aula07/ex1/Rectangle.java b/1ano/2semestre/poo/src/aula07/ex1/Rectangle.java index ca50d04..868ac62 100644 --- a/1ano/2semestre/poo/src/aula07/ex1/Rectangle.java +++ b/1ano/2semestre/poo/src/aula07/ex1/Rectangle.java @@ -1,14 +1,14 @@ package aula07.ex1; +import java.util.Objects; + public class Rectangle extends Shape { public double side1; public double side2; - public Rectangle(double side1, double side2) { - if (!(side1 > 0 && side2 > 0)) - throw new IllegalArgumentException("Sizes must be positive."); - this.side1 = side1; - this.side2 = side2; + public Rectangle(String color, double side1, double side2) { + this.setColor(color); + this.setSides(side1, side2); } public double[] getSides() { @@ -22,10 +22,18 @@ public class Rectangle extends Shape { this.side2 = side2; } + public String getColor() { + return this.color; + } + + public void setColor(String color) { + this.color = color; + } + @Override public boolean equals(Shape s2) { if (s2 instanceof Rectangle s2Rect) - return this.side1 == s2Rect.side1 && this.side2 == s2Rect.side2; + return Objects.equals(this.color, s2Rect.color) && this.side1 == s2Rect.side1 && this.side2 == s2Rect.side2; return false; } diff --git a/1ano/2semestre/poo/src/aula07/ex1/Shape.java b/1ano/2semestre/poo/src/aula07/ex1/Shape.java index ed19ada..6031cb2 100644 --- a/1ano/2semestre/poo/src/aula07/ex1/Shape.java +++ b/1ano/2semestre/poo/src/aula07/ex1/Shape.java @@ -1,6 +1,7 @@ package aula07.ex1; public abstract class Shape { + protected String color; public abstract boolean equals(Shape c2); public abstract String toString(); public abstract double getArea(); diff --git a/1ano/2semestre/poo/src/aula07/ex1/Triangle.java b/1ano/2semestre/poo/src/aula07/ex1/Triangle.java index c41280f..0c14e68 100644 --- a/1ano/2semestre/poo/src/aula07/ex1/Triangle.java +++ b/1ano/2semestre/poo/src/aula07/ex1/Triangle.java @@ -1,16 +1,15 @@ package aula07.ex1; +import java.util.Objects; + public class Triangle extends Shape { private double side1; private double side2; private double side3; - public Triangle(double side1, double side2, double side3) { - if (!(side1 > 0 && side2 > 0 && side3 > 0)) - throw new IllegalArgumentException("Sizes must be positive."); - this.side1 = side1; - this.side2 = side2; - this.side3 = side3; + public Triangle(String color, double side1, double side2, double side3) { + this.setColor(color); + this.setSides(side1, side2, side3); } public double[] getSides() { @@ -25,10 +24,18 @@ public class Triangle extends Shape { this.side3 = side3; } + public String getColor() { + return this.color; + } + + public void setColor(String color) { + this.color = color; + } + @Override public boolean equals(Shape s2) { if (s2 instanceof Triangle s2Triang) - return this.side1 == s2Triang.side1 && this.side2 == s2Triang.side2 && this.side3 == s2Triang.side3; + return Objects.equals(this.color, s2Triang.color) && this.side1 == s2Triang.side1 && this.side2 == s2Triang.side2 && this.side3 == s2Triang.side3; return false; }