88 943
Assignments Done
97.1%
Successfully Done
In July 2020

# Answer to Question #64354 in Java | JSP | JSF for Parth Desai

Question #64354
Problem 2: Triangle Recognition (If, If … Else)

Create Triangle Recognition on the right side of the app. It should ask the user to enter three points. You will need six text fields – three pairs of two. Each pair will represent a point (x, y). The coordinates of the points should be double variables. Using these points, the app should communicate to the user if the points form a triangle. Once it is confirmed that the points form a triangle the app needs to identify the type of triangle that was formed: Equilateral, Scalene, Isosceles, Right-Angled, Obtuse, or any combination. It has to communicate this to the user. Then it has to print the length of the sides of the triangle using the distance formula.
1
2017-01-04T04:19:04-0500
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class TriangleRecognition extends JFrame{

private JButton type = new JButton("Type Triangle");
private JTextField input1 = new JTextField();
private JTextField input2 = new JTextField();
private JTextField input3 = new JTextField();
private JTextField input4 = new JTextField();
private JTextField input5 = new JTextField();
private JTextField input6 = new JTextField();

private JLabel label1 = new JLabel("x1:");
private JLabel label2 = new JLabel("y1:");
private JLabel label3 = new JLabel("x2:");
private JLabel label4 = new JLabel("y2:");
private JLabel label5 = new JLabel("x3:");
private JLabel label6 = new JLabel("y3:");

public TriangleRecognition() {
this.setBounds(200, 200, 500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container container = this.getContentPane();
container.setLayout(new GridLayout(4, 4, 7, 10));

}

class ButtonEventListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double x1,y1,x2,y2,x3,y3;
double length1,length2,length3;
x1 = Double.parseDouble(input1.getText());
y1 = Double.parseDouble(input2.getText());
x2 = Double.parseDouble(input3.getText());
y2 = Double.parseDouble(input4.getText());
x3 = Double.parseDouble(input5.getText());
y3 = Double.parseDouble(input6.getText());

length1 = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2));
length2 = Math.sqrt(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2));
length3 = Math.sqrt(Math.pow(x1-x3, 2) + Math.pow(y1-y3, 2));

String message = "";

message += "Length of the sides 1: " + length1 + "\n";
message += "Length of the sides 2: " + length2 + "\n";
message += "Length of the sides 3: " + length3 + "\n";
message += "Type triangle: " + typeTriangle(length1, length2, length3);
JOptionPane.showMessageDialog(null, message, "Triangle", JOptionPane.PLAIN_MESSAGE);
}
}

static public String typeTriangle(double length1,double length2,double length3){
if((length1 + length2 < length3 && length2 + length3 < length1 && length1 + length3 < length2) ||
(length1 == 0 && length2 == 0 && length3 == 0)) {
return "Not triangle";
}else
if(length1 == length2 && length3 == length1) {//if a = b = c ,triangle is Equilateral
return "Equilateral";
}

if((length1 == Math.sqrt(length2 * length2 + length3 * length3))
|| (length2 == Math.sqrt(length1 * length1 + length3 * length3))
||(length3 > Math.sqrt(length2 * length2 + length1 * length1))) { //Pythagor theorem,if a^2 = b^2 + c^2 triangle is Rectangular
return "Rectangular";
}

if((length1 == length2) || (length1 == length3) || (length2 == length3)) {// if triangle is isosceles

if((length1 > Math.sqrt(length2 * length2 + length3 * length3))
||(length2 > Math.sqrt(length1 * length1 + length3 * length3))
||(length3 > Math.sqrt(length2 * length2 + length1 * length1))) {// if a^2 > b^2 + c^2 triangle is Obtuse Isosceles
return "Obtuse Isosceles";
}
else //else Acute Isosceles
return "Acute Isosceles";
}
return "Scalene";
}

public static void main(String[] args) {
TriangleRecognition app = new TriangleRecognition();
app.setVisible(true);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!