82.3k views
2 votes
Look at the following Polygon class:

public class Polygon
{
private int numSides;

public Polygon()
{
numSides = 0;
}

public void setNumSides(int sides)
{
numSides = sides;
}

public int getNumSides()
{
return numSides;
}
}
Write a public class named Triangle that is a subclass of the Polygon class. The Triangle class should have the following members:
a private int field named base
a private int field named height
a constructor that assigns 3 to the numSides field and assigns 0 to the base and height fields
a public void method named setBase that accepts an int argument. The argument's value should be assigned to the base field
a public void method named setHeight that accepts an int argument. The argument's value should be assigned to the height field
a public method named getBase that returns the value of the base field
a public method named getHeight that returns the value of the height field
a public method named getArea that returns the area of the triangle as a double.
Use the following formula to calculate the area: Area = (height * base) / 2.0

User Genspec
by
8.2k points

2 Answers

2 votes

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.

User Macedo
by
8.8k points
5 votes

Answer:

public class Triangle extends Polygon {

private int base;

private int height;

public Triangle() {

setNumSides(3);

base = 0;

height = 0;

}

public void setBase(int baseValue) {

base = baseValue;

}

public void setHeight(int heightValue) {

height = heightValue;

}

public int getBase() {

return base;

}

public int getHeight() {

return height;

}

public double getArea() {

double area = (height * base) / 2.0;

return area;

}

}

Hope This Helps

User Richard Corfield
by
8.5k points