[POO] aula07 shapes update

This commit is contained in:
TiagoRG 2023-03-28 23:34:18 +01:00
parent c756cc6b0b
commit 6cd8ced8f4
4 changed files with 43 additions and 18 deletions

View File

@ -1,12 +1,13 @@
package aula07.ex1; package aula07.ex1;
import java.util.Objects;
public class Circle extends Shape { public class Circle extends Shape {
private double radius; private double radius;
public Circle(double radius) { public Circle(String color, double radius) {
if (radius <= 0) this.setColor(color);
throw new IllegalArgumentException("Sizes must be positive."); this.setRadius(radius);
this.radius = radius;
} }
public double getRadius() { public double getRadius() {
@ -19,6 +20,14 @@ public class Circle extends Shape {
this.radius = radius; this.radius = radius;
} }
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
@Override @Override
public double getArea() { public double getArea() {
return Math.PI * Math.pow(this.radius, 2); return Math.PI * Math.pow(this.radius, 2);
@ -32,7 +41,7 @@ public class Circle extends Shape {
@Override @Override
public boolean equals(Shape s2) { public boolean equals(Shape s2) {
if (s2 instanceof Circle s2Circ) if (s2 instanceof Circle s2Circ)
return this.radius == s2Circ.radius; return Objects.equals(this.color, s2Circ.color) && this.radius == s2Circ.radius;
return false; return false;
} }

View File

@ -1,14 +1,14 @@
package aula07.ex1; package aula07.ex1;
import java.util.Objects;
public class Rectangle extends Shape { public class Rectangle extends Shape {
public double side1; public double side1;
public double side2; public double side2;
public Rectangle(double side1, double side2) { public Rectangle(String color, double side1, double side2) {
if (!(side1 > 0 && side2 > 0)) this.setColor(color);
throw new IllegalArgumentException("Sizes must be positive."); this.setSides(side1, side2);
this.side1 = side1;
this.side2 = side2;
} }
public double[] getSides() { public double[] getSides() {
@ -22,10 +22,18 @@ public class Rectangle extends Shape {
this.side2 = side2; this.side2 = side2;
} }
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
@Override @Override
public boolean equals(Shape s2) { public boolean equals(Shape s2) {
if (s2 instanceof Rectangle s2Rect) 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; return false;
} }

View File

@ -1,6 +1,7 @@
package aula07.ex1; package aula07.ex1;
public abstract class Shape { public abstract class Shape {
protected String color;
public abstract boolean equals(Shape c2); public abstract boolean equals(Shape c2);
public abstract String toString(); public abstract String toString();
public abstract double getArea(); public abstract double getArea();

View File

@ -1,16 +1,15 @@
package aula07.ex1; package aula07.ex1;
import java.util.Objects;
public class Triangle extends Shape { public class Triangle extends Shape {
private double side1; private double side1;
private double side2; private double side2;
private double side3; private double side3;
public Triangle(double side1, double side2, double side3) { public Triangle(String color, double side1, double side2, double side3) {
if (!(side1 > 0 && side2 > 0 && side3 > 0)) this.setColor(color);
throw new IllegalArgumentException("Sizes must be positive."); this.setSides(side1, side2, side3);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
} }
public double[] getSides() { public double[] getSides() {
@ -25,10 +24,18 @@ public class Triangle extends Shape {
this.side3 = side3; this.side3 = side3;
} }
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
@Override @Override
public boolean equals(Shape s2) { public boolean equals(Shape s2) {
if (s2 instanceof Triangle s2Triang) 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; return false;
} }