Create a public interface Triangle, add the following method signatures:
Create a public abstract class Isosceles that implements Triangle and implements following method:
Create a public interface Right that extends Triangle, add one more method signature:
Create a public class RightIsosceles that extends Isosceles and implements interface Right:
Create the object of class RightIsosceles and print area, perimeter and angles of RightIsosceles triangle.
interface Triangle {
// double area(): calculates and returns the area of a triangle.
double area();
// double perimeter(): calculates and returns the perimeter of a triangle.
double perimeter();
}
interface Right extends Triangle {
// double[] angles(): returns an array of measures of three interior angles.
public double[] angles();
}
abstract class Isosceles implements Triangle {
// fields as private double longside, equalSide. Use getter() and setter()
// methods to access them.
private double longside;
private double getEqualSide;
public Isosceles() {
}
public Isosceles(double longside, double equalSide) {
this.longside = longside;
this.getEqualSide = equalSide;
}
// double perimeter(): calculates and returns the perimeter of a triangle
public double perimeter() {
return longside + 2 * getEqualSide;
}
/**
* @return the longside
*/
public double getLongside() {
return longside;
}
/**
* @param longside the longside to set
*/
public void setLongside(double longside) {
this.longside = longside;
}
/**
* @return the equalSide
*/
public double getEqualSide() {
return getEqualSide;
}
/**
* @param equalSide the equalSide to set
*/
public void setEqualSide(double equalSide) {
this.getEqualSide = equalSide;
}
}
class RightIsosceles extends Isosceles implements Right {
public RightIsosceles(double longside, double equalSide) {
super(longside, equalSide);
}
@Override
public double area() {
double s = (this.getEqualSide() + this.getEqualSide() + this.getLongside()) / 2;
return Math.sqrt(s * (s - this.getEqualSide()) * (s - this.getEqualSide()) * (s - this.getLongside()));
}
@Override
public double[] angles() {
double _angles[] = { 90, 45, 45 };
return _angles;
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
RightIsosceles _RightIsosceles = new RightIsosceles(Math.sqrt(2) * 10, 10);
System.out.println("Area: " + _RightIsosceles.area());
System.out.println("Perimeter: " + _RightIsosceles.perimeter());
System.out.println("Angles: " + _RightIsosceles.angles()[0] + ", " + _RightIsosceles.angles()[1] + ", "
+ _RightIsosceles.angles()[0]);
}
}
Comments
Leave a comment