Answer: This is the complete program for this with the highest level of explanation.
This program is written in the Java programming language.
The name of this file is Triangle.java
public class Triangle extends Polygon {
// private fields to store the dimensions of the triangle
private int base;
private int height;
// constructor to initialize the fields and set the number of sides
public Triangle() {
// call the setNumSides method inherited from Polygon to set the number of sides to 3
setNumSides(3);
// initialize base and height to 0
base = 0;
height = 0;
}
// setter method for the base field
public void setBase(int b) {
base = b;
}
// setter method for the height field
public void setHeight(int h) {
height = h;
}
// getter method for the base field
public int getBase() {
return base;
}
// getter method for the height field
public int getHeight() {
return height;
}
// method to calculate and return the area of the triangle
public double getArea() {
// calculate the area using the formula (base * height) / 2.0
return 0.5 * base * height;
}
}
The name of this file is Polygon.java
public class Polygon {
private int numSides;
public Polygon() {
numSides = 0;
}
public void setNumSides(int sides) {
if (sides == 3) {
numSides = sides;
} else {
System.out.println("Error: Invalid number of sides for a triangle");
}
}
public int getNumSides() {
return numSides;
}
}
The name of this file is Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create a new Polygon object and set its number of sides to 3
Polygon polygon = new Polygon();
polygon.setNumSides(3);
// Print out the number of sides of the polygon
System.out.println("Number of sides: " + polygon.getNumSides());
// Obtain input from user for the base and height of a triangle
System.out.print("Enter the base of the triangle: ");
int base = scanner.nextInt();
System.out.print("Enter the height of the triangle: ");
int height = scanner.nextInt();
// Create a new Triangle object and set its base and height
Triangle triangle = new Triangle();
triangle.setBase(base);
triangle.setHeight(height);
// Calculate the area of the triangle and print it out
double area = triangle.getArea();
System.out.println("Area of the triangle: " + area);
}
}
This will be the output generated by this program.
Number of sides: 3
Enter the base of the triangle: 10
Enter the height of the triangle: 15
Area of the triangle: 75.0
Step 2/2
This is the complete explanation for this program.
This Triangle class is a subclass of the Polygon class, which means it inherits all of the methods and variables of the Polygon class.
This Triangle class has two private instance variables: base and height. These variables are used to store the dimensions of the triangle.
This Triangle class has a constructor that initializes the number of sides to 3 and sets the base and height variables to 0.
This Triangle class has four public methods:
a. This set Base method, which sets the value of the base variable to the given value.
b. This set Height method, which sets the value of the height variable to the given value.
c. This get Base method, which returns the value of the base variable.
d. The get Height method, which returns the value of the height variable.
Step-by-step explanation:
This triangle class has a public get Area method that calculates the area of the triangle using the formula (base * height) / 2.0 and returns the result as a double.
Final answer
This is the image of the output generated by this code.
This is the image of the final output.
This is the complete summary of the entire program.
This program defines two classes: Polygon and Triangle. The Polygon class has one private instance variable, num Sides, and three public methods: Polygon(), set Num Sides(int sides), and get Num Sides (). The Triangle class is a subclass of the Polygon class and features two private instance variables, base and height, as well as six public methods: Triangle (), set Base(int base), set Height(int height), get Base (), get Height (), and get Area ().
The Main class is used to build a Triangle class object and to gather user input for the triangle's base and height. The set Base and set Height methods are used to set the values of the base and height instance variables, respectively.