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

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)
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...
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).
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...
Multivariable Calculus: Surface Area and Change of Variables Find the surface area of the surface given...
Multivariable Calculus: Surface Area and Change of Variables Find the surface area of the surface given by z =18−2x−3y over the triangle with vertices: (0,0), (2,3), (4,1). Since this is not a type I or type II region, you will either need to divide the region into two regions or use a change of variables. x=(1/5)(u-2v) and y=(1/10)(3u-v) is a possible change of variables. I will upvote answers!
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle,...
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { int geoCalc; //number selection of user Scanner get = new Scanner(System.in); //display section. System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a Cirlce."); System.out.println("2. Calculate the Area of a Rectangle."); System.out.println("3. Calculate the Area of a Triangle."); System.out.println("4. QUIT"); System.out.println("Please make a selection:...
Must be done with the Program Raptor Program #2 - Area of Shapes Design a superclass...
Must be done with the Program Raptor Program #2 - Area of Shapes Design a superclass called Shape that contains two functions—getVolume() and getInput(). The getVolume and getInput functions in the Shape class will simply return 0, you will derive from them in your subclasses mentioned below. Define 3 subclasses of the Shape class—Sphere, Cube, and Cone. The sphere class will need a radius field, the cube class will need a length field, and the cone class will need radius...
java create a class for triangle and also driver(area and perimeter) also write its getters and...
java create a class for triangle and also driver(area and perimeter) also write its getters and setters for the right triangle
1) Write a functional program in Java that can calculate the volume and surface area of...
1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube 2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube 3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube
Write a JavaScript function to calculate the surface area of a sphere. Input the radius using...
Write a JavaScript function to calculate the surface area of a sphere. Input the radius using a NumericUpDown. Have the function calculate the surface area and display it on your page. Label the inputs and the output on your page. Change the title to "Oh, Boy! JavaScript!". Put your name at the top in camelCase. Put your major at the bottom in camelCase.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT