106k views
0 votes
Triangle area comparison (classes) Given class Triangle, complete the program to read and set the base and height of triangle1 and triangle2, determine which triangle's area is larger, and output the larger triangle's info, making use of Triangle's relevant methods. Ex: If the input is: 3.0 4.0 4.0 5.0 where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is: Triangle with larger area: Base: 4.00 Height: 5.00 Area: 10.00

User Bruso
by
5.0k points

2 Answers

4 votes

Final answer:

To compare the areas of two triangles, you can use the formula for triangle area: 1/2 × base × height. By reading and setting the base and height of each triangle, you can calculate the areas using their respective Triangle class methods. Comparing the two areas will allow you to determine the larger triangle and output its information.

Step-by-step explanation:

In this programming task, you need to compare the areas of two triangles using the formula for triangle area: 1/2 × base × height. First, you will read and set the base and height of triangle1 and triangle2. Then, you will calculate the areas of both triangles using the formula. Finally, you will compare the two areas and output the larger triangle's information.

To complete this task, you can use the following code in C++:

#include <iostream>

using namespace std;

class Triangle {
private:
float base;
float height;
public:
void setBase(float b) { base = b; }
void setHeight(float h) { height = h; }
float calculateArea() { return 0.5 * base * height; }
};

int main() {
float base1, height1, base2, height2;
cin >> base1 >> height1 >> base2 >> height2;

Triangle triangle1, triangle2;

triangle1.setBase(base1);
triangle1.setHeight(height1);

triangle2.setBase(base2);
triangle2.setHeight(height2);

float area1 = triangle1.calculateArea();
float area2 = triangle2.calculateArea();

if (area1 > area2) {
cout << "Triangle with larger area:\\";
cout << "Base: " << base1 << " Height: " << height1 << " Area: " << area1 << endl;
} else {
cout << "Triangle with larger area:\\";
cout << "Base: " << base2 << " Height: " << height2 << " Area: " << area2 << endl;
}

return 0;
}

You can also write the code in Python:

class Triangle:
def __init__(self, base, height):
self.base = base
self.height = height

def calculate_area(self):
return 0.5 * self.base * self.height

base1, height1, base2, height2 = map(float, input().split())

triangle1 = Triangle(base1, height1)
triangle2 = Triangle(base2, height2)

area1 = triangle1.calculate_area()
area2 = triangle2.calculate_area()

if area1 > area2:
print(f"Triangle with larger area:\\Base: {base1} Height: {height1} Area: {area1}")
else:
print(f"Triangle with larger area:\\Base: {base2} Height: {height2} Area: {area2}")

User Rick Glimmer
by
5.1k points
5 votes

Answer:

// Triangle.java

public class Triangle {

private double base;

private double height;

public void setBase(double base) {

this.base = base;

}

public void setHeight(double height) {

this.height = height;

}

public double getArea() {

return 0.5 * base * height;

}

public void printInfo() {

System.out.printf("Base: %.2f\\", base);

System.out.printf("Height: %.2f\\", height);

System.out.printf("Area: %.2f\\", getArea());

}

}

// TriangleArea.java

import java.util.Scanner;

public class TriangleArea {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

double base1 = in.nextDouble();

double height1 = in.nextDouble();

double base2 = in.nextDouble();

double height2 = in.nextDouble();

Triangle triangle1 = new Triangle();

triangle1.setBase(base1);

triangle1.setHeight(height1);

Triangle triangle2 = new Triangle();

triangle2.setBase(base2);

triangle2.setHeight(height2);

System.out.println("Triangle with larger area:");

if (triangle1.getArea() > triangle2.getArea()) {

triangle1.printInfo();

} else {

triangle2.printInfo();

}

}

}

Step-by-step explanation:

  • Inside the getArea method, calculate the area by applying the following formula.

Area = 0.5 * base * height;

  • Get the input from user for both triangles.
  • Compare the area of both triangle and print the information of the larger triangle.
User NickKampe
by
5.3k points