In: Computer Science
Apply inheritance to write a superclass and subclass to compute the triangle area and the surface area of the triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both superclass and subclass so they will return the data of a triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at least two objects with hard-coded data and display the results of the calculations.//Java Program
Working of the program
Triangle class
//Triangle class definition public class Triangle { //declaring instance variables private double a; private double b; private double c; //constructor to initialize variables public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } //getter and setter methods to get and set values public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } //method to calculate area using heron's formula public double area() { double s=(a+b+c)/2; double area=Math.sqrt(s*(s-a)*(s-b)*(s-c)); return Math.round(area*100.0)/100.0; } @Override //overriding toString() method of Object class to print object values directly public String toString() { return "\nSideA: "+a+"\nSideB: "+b+"\nSideC: "+c+"\nArea: "+area(); } }
Pyramid class
//Pyramid class definition public class Pyramid extends Triangle { //constructor to initialize values public Pyramid(double a, double b, double c) { //passing values to super class triangle super(a, b, c); } //method to calculate surface area public double surfaceArea() { //calling area() method of base class and multiply with 4 //to get surface area double sArea=4*area(); return Math.round(sArea*100.0)/100.0; } //overriding toString() method of Object class to print object values directly @Override public String toString() { //calling Triangle class's toString() method to print sides values return super.toString()+"\nSurface Area: "+surfaceArea(); } }
Driver class
public class Main { public static void main(String[] args) { //creating Triangle objects Triangle t1 = new Triangle(5, 4, 7); Triangle t2 = new Triangle(7, 8, 9); //calculating area System.out.println("Area of triangle is: " + t1.area()); //printing t1's values System.out.println(t1); //calculating area System.out.println("\nArea of triangle is: " + t2.area()); //printing t2's values System.out.println(t2); //creating Pyramid objects Pyramid p1 = new Pyramid(6, 8, 10); Pyramid p2 = new Pyramid(10, 12, 15); //calculating surface area System.out.println("\nSurface area of pyramid is: " + p1.surfaceArea()); //printing p1's values System.out.println(p1); //calculating surface area System.out.println("\nSurface area of pyramid is: " + p2.surfaceArea()); //printing p2's values System.out.println(p2); } }
Screen shot of the code
Screen shot of the output