This is not my piece of code, but I would be interested to know something.
how to create, for example, an object of the triangle class?There, an array of the lengths of the sides of the triangle is passed as a parameter. But I do not know how to do it.Even if the array is registered in the main. Please tell me how to do it, I will be very grateful.
(I'll figure out how to do this with keyboard input later on my own.so far, only this question.)
public class Polygon {
int sideLengths[];
int numSides =0;
public Polygon(int [] lengths){
sideLengths = lengths;
numSides = lengths.length;
}
public int perimeter() {
//compute perimeter
int sum = 0;
for (int i = 0 ; i < sideLengths.length ;i++) {
sum += sideLengths[i];
}
return sum;
}
//No area method.
}
public class Triangle extends Polygon {
public Triangle(int [] lengths) {
super(lengths); //duper
numSides = 3;
}
public Triangle () {
this(new int[3]);
}
//No perimeter needed
public double area (){
double s = perimeter()/2;
//Heron's formula
return Math.sqrt(s*(s- sideLengths[0])*(s- sideLengths[1])*(s - sideLengths[2]));
}
}
Upd:I figured out how to call the perimeter method for an object of the polygon class.But how to do this for a triangle?