Question

In: Computer Science

Apply inheritance to write a superclass and subclass to compute the triangle area and the surface...

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

Solutions

Expert Solution

Working of the program

  • In triangle class, instance vriables a,b,c for three sides are declared
  • Constructor is defined to initialize variables
  • Getter and Setter methods are defined to get and set values
  • area() method is defined to find area of triangle
  • area is calculated using Heron's formula
  • Math.round() function is used to round result upto 2 decimal point.
  • toString() method of Object class is overridden to print object values directly
  • In pyramid class,constructor pass the values to base class constructor
  • surfaceArea() method is defined to calculate surface area
  • surface area of pyramid will be 4 times of area of triangle
  • area() method of Triangle class is called to get area of triangle
  • toString() method of Object class is overridden to print object values directly
  • toString() method of Triangle class is called to get information about triangle
  • Inside Main class, objects of Triangle and Pyramid classes are created and their methods are tested

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


Related Solutions

Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of 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 of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
Q. Explain how to invoke a superclass method from a subclass method for the case in...
Q. Explain how to invoke a superclass method from a subclass method for the case in which the subclass method overrides a superclass method and the case in which the subclass method does not override a superclass method. [1 Mark] this is a correct answer but i want answer it  in different way   : In the case where the subclass method overrides the superclass method, it is necessary to explicitly use the keyword super, to invoke the method of the superclass as...
Using a scalar surface integral, compute the surface area of the portion of the unit sphere...
Using a scalar surface integral, compute the surface area of the portion of the unit sphere that is above the cone z = sqrt(x^2+y^2)
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
Could you expound in database modeling, how subclass and superclass are used? Maybe you can use...
Could you expound in database modeling, how subclass and superclass are used? Maybe you can use these terms(cardinality constraint , total participation constraint, weak entity type, partial key, IS-A relationship, specialization and generalization, specific (or local) attribute, specific relationship, aggregation)
Find the maximum area of a rectangle inscribed in a triangle of area A.(NOTE: the triangle...
Find the maximum area of a rectangle inscribed in a triangle of area A.(NOTE: the triangle need not necessarily be a right angled triangle).
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
You have to write a program that computes the area of a triangle. Input consists of...
You have to write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points are represented as Cartesian units i.e. X,Y coordinates as in (3,5). Output must be the three points, the three distances between vertices, and the area of the triangle formed by these points. The program must read the coordinates of each point, compute the distances between each pair of them and print these values....
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT