Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a file named Triangle.java, write complete code to create a \"Triangle\" clas

ID: 3623417 • 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":
* btsvec -- a Vector used to store every value assigned to
its triangle's c property.


Instance methods (accessor and mutator):
* setC -- sets a value for c.
Therefore, setC should also store the value into btsvec, too.
setC should also do data validation, prohibiting c from
becoming 0 or negative.
(Caution: remember that Vectors can't store the
8 primitive data types, so you should store c as a
String object or a Double object.)
* 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 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: as discussed in CS107,
"==" 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 could 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

Dear, Giving you just outline of class class Triangle { public double a; public double b; private double c; public boolean isRightTriangle; Vector btsvec = new Vector(); private static int numMade = 0; public Triangle() { this.a = 3; this.b = 4; this.c = 5; String s; s = (String) btsvec.addElement(1); } public Triangle(double a, double b, double c) { this.a = a; this.b = b; if (c < 0) this.c = 0; else this.c = c; btsvec.addElement(c); numMade = ++numMade; } public void setC(double n) { if (n< 0) System.out.println("Illegal Value, Ignoring"); else this.c = n; btsvec.addElement(n); } public double getC() { return this.c; } public void showAllCs() { System.out.println(btsvec.toString()); } public boolean isRightTriangle() { if (a*a + b*b == c*c) this.isRightTriangle = true; else this.isRightTriangle = false; } public static double getNumMade() { return numMade; } } // end file
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote