[POO] aula07 shapes update

This commit is contained in:
TiagoRG 2023-03-28 23:34:18 +01:00
parent 4098cf1156
commit 60944a9228
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
4 changed files with 43 additions and 18 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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();

View File

@ -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;
}