Question

In: Computer Science

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 least two objects with hard-coded data and display the results of the calculations.

Use of meaningful names for variables, classes, and methods and meaningful comments.

Solutions

Expert Solution

JAVA CODE:

class Triangle
{
private double length;

public Triangle()
{
this.length = 0.00;
}

public Triangle(double length)
{
this.length = length;
}

// Setter and getter methods
public double get_the_Length()
{
return length;
}
public void set_the_Length(double length)
{
this.length = length;
}

// Create a to_string() method
@Override
public String toString()
{
return "Triangle [length=" + length + "]";
}

// Create a method named Surface_Area()
public double Surface_Area()
{
return Math.sqrt(3) / 2 * Math.pow(this.get_the_Length(), 2);
}
}


class Tri_Vertebral extends Triangle
{

private double height;


public Tri_Vertebral()
{
super(0);
this.height = 0.00;
}

// Create a parameter constructor.
public Tri_Vertebral(double length, double height)
{
super(length);
this.height = height;
}

// Getter and setter methods
public double get_the_Height()
{
return height;
}

public void set_the_Height(double height)
{
this.height = height;
}

public double totalFaceArea()
{
return 3 / 2 * this.get_the_Length() * this.get_the_Height();
}

// Create a method named totalSurface_Area
public double totalSurface_Area()
{
return this.Surface_Area() + 3 * this.totalFaceArea();
}

// the toString method is as follows:
@Override
public String toString()
{
return "Tri_Vertebral [height=" + height + "," + super.toString() + "]";
}
}

//Driver code
public class Triangle_Test
{
// Create a main method.
public static void main(String[] args)
{
// Create an object for the class named Triangle
Triangle t1 = new Triangle(15);
System.out.println("Area of traingle:");
System.out.println(t1.Surface_Area());

System.out.println("Traingle using tostring method:");
System.out.println(t1.toString());

// Create an object for the class named Tri_Vertebral
Tri_Vertebral tp = new Tri_Vertebral(15, 20);
System.out.println("Surface area of triangle pyramid");
System.out.println(tp.totalSurface_Area());
System.out.println("Tri_Vertebral using tostring method:");
System.out.println(tp.toString());
}
}

OUTPUT SCREENSHOT:

//Hope you understood the code and have a good day..Don't forget to give UPVOTE s it means a lot..THANKS:)


Related Solutions

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...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
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
(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.)
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of using "super" and "this" both as constructors and as variables.
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of using "super" and "this" both as constructors and as variables.**
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in. *NOTE*- I...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT