Change the Triangle class code1 so the constructor and the setSides() method throw an IllegalArgumentException when the parameters are invalid. Change main from code2 so instantiating the new Triangle objects is in one try block (and has its own catch clause which catches an IllegalArgumentException in which you display a message and assign a default Triangle with 1,1,1 to the “invalid” Triangle variable), and the setSides calls are in another try block (and has its own catch clause which catches an IllegalArgumentException in which you display what the toString of the exception).
Code1
public class Triangle {
private double sideA=1., sideB=1., sideC=1.;
private double perimeter=3.;
private double area=0.;
public Triangle(double a, double b, double c){
setSides(a, b, c);
calcAreaAndPerimeter();// call in case default
} // end constructor
private void calcAreaAndPerimeter(){
perimeter = sideA + sideB + sideC;
double p = perimeter/2.;
area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
} // end calcAreaAndPerimeter
public double getSideA(){ return sideA; }
public double getSideB(){ return sideB; }
public double getSideC(){ return sideC; }
public double getPerimeter(){ return perimeter; }
public double getArea(){ return area; }
public boolean setSides(double a, double b, double c){
if( a > 0 && b > 0 && c > 0 &&
(a+b)>c && (a+c)>b && (b+c)>a){
sideA=a;
sideB=b;
sideC=c;
calcAreaAndPerimeter();
return true;
}// end if
// DON’T print an error message
return false;
} // end setSides
} // end class Triangle
code2
// USING THE Triangle class written for code1 (main is below)
import java.util.*; // for the Scanner class
public class Triangle {
private double sideA=1., sideB=1., sideC=1.;
private double perimeter=3.;
private double area=0.;
public Triangle(double a, double b, double c){
setSides(a, b, c);
calcAreaAndPerimeter();// call in case default
} // end constructor
private void calcAreaAndPerimeter(){
perimeter = sideA + sideB + sideC;
double p = perimeter/2.;
area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
} // end calcAreaAndPerimeter
public double getSideA(){ return sideA; }
public double getSideB(){ return sideB; }
public double getSideC(){ return sideC; }
public double getPerimeter(){ return perimeter; }
public double getArea(){ return area; }
public boolean setSides(double a, double b, double c){
if( a > 0 && b > 0 && c > 0 &&
(a+b)>c && (a+c)>b && (b+c)>a){
sideA=a;
sideB=b;
sideC=c;
calcAreaAndPerimeter();
return true;
}// end if
// DON’T print an error message
return false;
} // end setSides
} // end class Triangle
class TryTriangle {
static Scanner scanner= new Scanner(System.in);
public static void main( String [] args ){
Triangle tri1, tri2;
tri1 = new Triangle(6., 10., 8.);
tri2 = new Triangle(-2., 3., 4.);
double t1Area= tri1.getArea();
double t2Perim = tri2.getPerimeter();
System.out.println(“Tri#1 area = ” + t1Area);
System.out.println(“Tri#2 perimeter = ”
+ t2Perim);
if( tri2.setSides(3., 4., 5.) )
System.out.println(“Tri2 sides been changed to “+ tri2.getSideA()+”, “+tri2.getSideB()+”, “+tri2.getSideC());
else
System.out.println(“Error in setLength”);
if( !tri1.setSides(7., 10., 1.) )
System.out.println(“setSides didn’t work as expected”);
else
System.out.println(“setSides didn’t fail (should have)”);
//EXAMPLE OF WRITING A METHOD TO RETURN AN OBJECT:
Triangle tri3;
//DON’T DO the following (won’t assign to tri4)
// makeTriangle(tri4);// won’t compile
//do instead:
tri3 = makeTriangle();
// call tri3 methods, etc.
// etc.
} // end main
public static Triangle makeTriangle(){
double a, b, c;
System.out.print(“Enter a Triangle side A: “);
a = scanner.nextDouble();
System.out.print(“Enter a Triangle side B: “);
b = scanner.nextDouble();
System.out.print(“Enter a Triangle side C: “);
c = scanner.nextDouble();
return new Triangle(a, b, c);
} // end makeTriangle
} // end class TryTriangle
Expert Answer
Code 1
public class Triangle {
private double sideA=1., sideB=1., sideC=1.;
private double perimeter=3.;
private double area=0.;
public Triangle(double a, double b, double c){
if(a > 0 && b > 0 && c > 0 &&
(a+b)>c && (a+c)>b && (b+c)>a){
setSides(a, b, c);
calcAreaAndPerimeter();// call in case default
}else{
throw new IllegalArgumentException(“sides of triagle are not valid”);
}
} // end constructor
private void calcAreaAndPerimeter(){
perimeter = sideA + sideB + sideC;
double p = perimeter/2.;
area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
} // end calcAreaAndPerimeter
public double getSideA(){ return sideA; }
public double getSideB(){ return sideB; }
public double getSideC(){ return sideC; }
public double getPerimeter(){ return perimeter; }
public double getArea(){ return area; }
public boolean setSides(double a, double b, double c){
if( a > 0 && b > 0 && c > 0 &&
(a+b)>c && (a+c)>b && (b+c)>a){
sideA=a;
sideB=b;
sideC=c;
calcAreaAndPerimeter();
return true;
}else{
throw new IllegalArgumentException(“sides of triagle are not valid”);
}
} // end setSides
} // end class Triangle
code 2
class TryTriangle {
static Scanner scanner= new Scanner(System.in);
public static void main( String [] args ){
Triangle tri1, tri2;
try{
tri1 = new Triangle(6., 10., 8.);
}catch(IllegalArgumentException e){
tri1 = new Triangle(1., 1., 1.);
System.out.println(e.getMessage());
}
try{
tri2 = new Triangle(-2., 3., 4.);
}catch(IllegalArgumentException e){
tri2 = new Triangle(1., 1., 1.);
System.out.println(e.getMessage());
}
double t1Area= tri1.getArea();
double t2Perim = tri2.getPerimeter();
System.out.println(“Tri#1 area = ” + t1Area);
System.out.println(“Tri#2 perimeter = ”
+ t2Perim);
try{
tri2.setSides(3., 4., 5.);
System.out.println(“Tri2 sides been changed to “+ tri2.getSideA()+”, “+tri2.getSideB()+”, “+tri2.getSideC());
}catch(IllegalArgumentException e){
System.out.println(e.toString());
}
try{
tri1.setSides(7., 10., 1.);
System.out.println(“setSides didn’t fail (should have)”);
}catch(IllegalArgumentException e){
System.out.println(e.toString());
}
//EXAMPLE OF WRITING A METHOD TO RETURN AN OBJECT:
Triangle tri3;
//DON’T DO the following (won’t assign to tri4)
// makeTriangle(tri4);// won’t compile
//do instead:
tri3 = makeTriangle();
// call tri3 methods, etc.
// etc.
} // end main
public static Triangle makeTriangle(){
double a, b, c;
System.out.print(“Enter a Triangle side A: “);
a = scanner.nextDouble();
System.out.print(“Enter a Triangle side B: “);
b = scanner.nextDouble();
System.out.print(“Enter a Triangle side C: “);
c = scanner.nextDouble();
return new Triangle(a, b, c);
} // end makeTriangle
} // end class TryTriangle
OUTPUT: