Home > Back-end >  Design a Triangle Triangle
Design a Triangle Triangle

Time:10-01

Have properties: sideA, sideB sideC, has a constructor, a method: computing area getArea (), modify the three sides modifySides (double a, b double, double c), the number of constructor, the setter and getter methods to design

CodePudding user response:

Makes little sense to setter and getter methods
 
Package CSDN.

Public class Triangle {

Private double sideA;
Private double sideB;
Private double sideC;

//an equilateral triangle
Public Triangle (double side) {
Enclosing sideA=side;
Enclosing sideB=side;
Enclosing sideC=side;
}

//isosceles triangle
Public Triangle (double sideAB, double sideC) {
Enclosing sideA=sideAB;
Enclosing sideB=sideAB;
Enclosing sideC=sideC;
}

//regular triangle
Public Triangle (double sideA, double sideB, double sideC) {
Enclosing modifySides (sideA sideB, sideC);
}

Private Boolean isTriangle () {
//set the trilateral of a triangle as a, b, c, a + b> C, a> C - b, b + c> A, b> A - a + c c> B, c> B - a
Boolean isFlag=false;
If ((enclosing sideA + enclosing sideB & gt; Enclosing sideC) & amp; & (this sideA & gt; (this. SideC - this. SideB))
& & (this. SideB + this. SideC & gt; Enclosing sideA) & amp; & (this sideB & gt; (this. SideA - this. SideC))
& & (this. SideA + this. SideC & gt; Enclosing sideB) & amp; & (this sideC & gt; (this. SideB - this. SideA))) {
IsFlag=true;
}
Return isFlag;
}

Public double getArea () {
Double area=1;
If (isTriangle ()) {
//if it is a triangle, according to Helen formula to solve the area
Double p=(enclosing sideA + enclosing sideB + enclosing sideC)/2;
Area=math.h SQRT (p * (p - this. SideA) * (p - this. SideB) * (p - this. SideC));
}
The return area;
}

Public void modifySides (double sideA, double sideB, double sideC) {
Enclosing sideA=sideA;
Enclosing sideB=sideB;
Enclosing sideC=sideC;
}

Public static void main (String [] args) {
Double a=30;
Double b=40;
Double c=50;
Triangle ta=new Triangle (a, b, c);
//ta.621 modifySides (a * 2 * 2 b, c * 2);//can uncomment look at running effect
Double area=ta.621 getArea ();
If (area!=1)
System. The out. Println (area);
The else {
System. Out.println (" unable to solve the triangle area!" );
}
}
}
  • Related