1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
JulsSmile [24]
4 years ago
12

Triangle area comparison (classes) Given class Triangle, complete the program to read and set the base and height of triangle1 a

nd 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
Computers and Technology
1 answer:
Alenkinab [10]4 years ago
7 0

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\n", base);

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

       System.out.printf("Area: %.2f\n", 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();

       }

   }

}

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.
You might be interested in
Write a demo test to verify the above Rectangle object accessible from demo test main program and can execute display() method t
Sati [7]

Answer:

Explanation:

The Rectangle class was not provided in this question but after a quick online search I was able to find the class code. Due to this I was able to create a test code in the main Method of my program to create a Rectangle object and call it's display() method. The Test code and the output can be seen in the attached image below. While the code below is simply the Main Test Program as requested.

class Brainly {  

   public static void main(String[] args) {

       Rectangle rectangle = new Rectangle(20, 8);

       rectangle.display();

   }

}

4 0
3 years ago
Given a class called Measure that has the methods and data as specified, choose the correct code to satisfy the requirements of
Nina [5.8K]

Answer:

D

Explanation:

7 0
3 years ago
The best way to share criticism is to ______. a. write it in the break room b. talk it about with all your coworkers c. talk abo
ICE Princess25 [194]

Answer:

C.

Explanation:

6 0
4 years ago
Read 2 more answers
For those that play pc games how do you go outside in The Sims 2?
yanalaym [24]

Answer:

My sister plays a lot of the sims 4 but I do recall she also played the Sims 2 so when she would go outside she would command them to do that.

Explanation:

If this does not work I would suggest you get a video that can explain it better than I possibly would? Considering the Sims 2 was made a while back you would maybe want somebody with a little more experience.

5 0
3 years ago
Read 2 more answers
Determine the "answer" to the function according to the spreadsheet below: =SUM(A1:A3)/2
nataly862011 [7]
If this is the full question " <span>Which of the following formulas is equivalent to =SUM(A1:A3)? A. =A1+A3 B. =A1+A2+A3 B. =A2 D. =A3-A1"


The answer is </span>
<span>B. =A1+A2+A3 formulas is equivalent to =SUM(A1:A3)</span>
4 0
4 years ago
Other questions:
  • Create a dictionary named letter_counts that contains each letter and the number of times it occurs in string1. Challenge: Lette
    14·1 answer
  • File Explorer contains ribbon tabs that can be used for various functions. Which ribbon tab provides options to open a new File
    11·1 answer
  • 3. What are the first steps that you should take if you are unable to get onto the Internet? (1 point)
    15·1 answer
  • Which term refers to a type of an attack in which an attacker makes his data look like it is coming from a different source addr
    10·1 answer
  • Which type of computer is the best? laptop, tablet, or desktop
    15·2 answers
  • shapes polymorphism Create a set of classes derived from an abstract class Shape, which provides a common interface, for managin
    7·1 answer
  • Given the statement: int list[25]; The index can go from 0 to 25 inclusive withoutgoing beyond the end of the array.true or fals
    14·1 answer
  • Escribe 10 ejemplos de lo que consideras un byte
    5·1 answer
  • Nadeen runs a website on vegan lifestyle. She wants to spread the word and make more people visit her site to read her recipes a
    5·1 answer
  • A _____ is a character or group of characters that has a specific meaning. a. field b. record c. file d. database
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!