In a file named Triangle.java, write complete code to create a \"Triangle\" clas
ID: 671407 • Letter: I
Question
In a file named Triangle.java, write complete code to create a "Triangle" class as follows: Instance variables, assume all are "double": * a -- length of side a, assume "public" * b -- length of side b, assume "public" * c -- length of side c, assume "private". Instance variable, assume "private": * btsarr -- an ArrayList used to store every value assigned to its triangle's c property. (The "bts" is short for "behind the scenes", to help you recall that a Prog1 will not know about this "private" helper variable, only we will know about it.) Instance methods (accessor and mutator): * setC -- mutator for the c property, sets a value for c. Therefore, setC should also store the value into btsarr, too. setC should also do data validation, prohibiting c from becoming 0 or negative. * Note: ArrayLists can't store the 8 primitive data types (but, newer Java compilers will automatically cast them to objects and allow them). * Tip: To easily convert from double to String: x+"" * getC -- accessor method for the c property. More instance methods: * showAllCs -- outputs (to the screen) all values of c that were ever assigned to that particular triangle. * isRightTriangle -- returns true or false, based on whether or not that triangle is a right triangle. Hint: a*a + b*b == c*c indicates a right triangle. For ease, assume c always stores the hypotenuse when a triangle is a right-triangle. (Technical note: "==" shouldn't be used with real numbers, because computers usually store real numbers with slight errors. However, for ease, assume that it'll always work correctly here.) Constructor methods: * default constructor -- sets a, b, and c, to the values 3, 4, and 5. * customized constructor -- receives a value for each of the 3 sides. (For ease, you can assume the user will always use positive lengths, i.e. you don't have to do data validation on the parameters.) Class variables: * numMade -- starts at 0, but increases by 1 every time a new triangle object is created by some Prog1 main routine. This keeps track of how many triangles Prog1 has created. This variable should be int and private so that its value can't be accidentally messed up by Prog1. Class methods: * getNumMade -- accessor method, which returns the value of numMade, so that some Prog1 program can discover numMade's current value.
Explanation / Answer
// Triangle.java
import java.util.ArrayList;
public class Triangle{
public double a;
public double b;
private double c;
private ArrayList btsarr = new ArrayList();
public void setC(double c){
if(c > 0){
this.c = c;
btsarr.add(c);
}
else{
System.out.println("Value cannot be negative or 0");
}
}
public double getC(){
return c;
}
public void showAllCs(){
System.out.println(btsarr);
}
public boolean isRightTriangle(){
return (a*a + b*b == c*c);
}
Triangle(){
a = 3;
b = 4;
c = 5;
numMade++;
}
Triangle(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
numMade++;
}
static int numMade = 0;
public static int getNumMade(){
return numMade;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.