Merge pull request #37 from TiagoRG/dev-tiagorg

[POO] Changed from asserts to IllegalArgumentExceptions
This commit is contained in:
Tiago Garcia 2023-03-07 23:33:30 +00:00 committed by GitHub
commit 5f187c433f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 12 deletions

View File

@ -8,7 +8,8 @@ class Circle {
public double radius; public double radius;
public Circle(double radius) { public Circle(double radius) {
assert radius > 0; if (!(radius > 0))
throw new IllegalArgumentException("Sizes must be positive.");
this.radius = radius; this.radius = radius;
} }
@ -17,7 +18,8 @@ class Circle {
} }
public void setRadius(double radius) { public void setRadius(double radius) {
assert radius > 0; if (!(radius > 0))
throw new IllegalArgumentException("Sizes must be positive.");
this.radius = radius; this.radius = radius;
} }
@ -44,10 +46,9 @@ class Triangle {
public double side3; public double side3;
public Triangle(double side1, double side2, double side3) { public Triangle(double side1, double side2, double side3) {
assert side1 > 0 && side2 > 0 && side3 > 0; if (!(side1 > 0 && side2 > 0 && side3 > 0))
if(Math.abs(side2 - side3) < side1 || side1 < side2 + side3 throw new IllegalArgumentException("Sizes must be positive.");
|| Math.abs(side1 - side3) < side2 || side2 < side1 + side3 if(!(side1 < side2 + side3 && side2 < side1 + side3 && side3 < side1 + side2))
|| Math.abs(side1 - side2) < side3 || side3 < side1 + side2)
throw new IllegalArgumentException("Triangle cannot be created with those sides."); throw new IllegalArgumentException("Triangle cannot be created with those sides.");
this.side1 = side1; this.side1 = side1;
this.side2 = side2; this.side2 = side2;
@ -59,10 +60,9 @@ class Triangle {
} }
public void setSides(double side1, double side2, double side3) { public void setSides(double side1, double side2, double side3) {
assert side1 > 0 && side2 > 0 && side3 > 0; if (!(side1 > 0 && side2 > 0 && side3 > 0))
if(Math.abs(side2 - side3) < side1 || side1 < side2 + side3 throw new IllegalArgumentException("Sizes must be positive.");
|| Math.abs(side1 - side3) < side2 || side2 < side1 + side3 if(!(side1 < side2 + side3 && side2 < side1 + side3 && side3 < side1 + side2))
|| Math.abs(side1 - side2) < side3 || side3 < side1 + side2)
throw new IllegalArgumentException("Triangle cannot be created with those sides."); throw new IllegalArgumentException("Triangle cannot be created with those sides.");
this.side1 = side1; this.side1 = side1;
this.side2 = side2; this.side2 = side2;
@ -92,7 +92,8 @@ class Rectangle {
public double side2; public double side2;
public Rectangle(double side1, double side2) { public Rectangle(double side1, double side2) {
assert side1 > 0 && side2 > 0; if (!(side1 > 0 && side2 > 0))
throw new IllegalArgumentException("Sizes must be positive.");
this.side1 = side1; this.side1 = side1;
this.side2 = side2; this.side2 = side2;
} }
@ -102,7 +103,8 @@ class Rectangle {
} }
public void setSides(double side1, double side2) { public void setSides(double side1, double side2) {
assert side1 > 0 && side2 > 0; if (!(side1 > 0 && side2 > 0))
throw new IllegalArgumentException("Sizes must be positive.");
this.side1 = side1; this.side1 = side1;
this.side2 = side2; this.side2 = side2;
} }