// <applet code="TA" width="400" height="400"></applet>
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class TA extends Applet implements ActionListener {
private Label lblSideA, lblSideB, lblSideC,
lblAngles, lblArea, lblRightAngle;
private TextField txtSideA, txtSideB, txtSideC;
NumberFormat nf;
public void init() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
setLayout(null);
setupGUIComponents();
final int WIDTH = 470, HEIGHT = 220;
setSize(WIDTH, HEIGHT);
}
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
int a = Integer.parseInt(txtSideA.getText().trim());
int b = Integer.parseInt(txtSideB.getText().trim());
int c = Integer.parseInt(txtSideC.getText().trim());
double[] angles = getAngles(a, b, c);
lblAngles.setText("Three angles are " +
nf.format(angles[0]) + " degree, " +
nf.format(angles[1]) + " degree and " +
nf.format(angles[2]) + " degree");
lblArea.setText("Area of the triangle is " +
nf.format(getArea(a, b, c)) + " " + command);
String s = isRightTriangle(angles) ? "" : "not ";
lblRightAngle.setText("It is " + s + "a right triangle");
}
private void setupGUIComponents() {
// initiaze the components
Label lblInstructions =
new Label("Enter the length of the sides of a triangle");
Label lblSideA = new Label("a"); txtSideA = new TextField(5);
Label lblSideB = new Label("b"); txtSideB = new TextField(5);
Label lblSideC = new Label("c"); txtSideC = new TextField(5);
Label lblSelect = new Label("Select unit:");
Button btnCM = new Button("cm");
Button btnM = new Button("m");
Button btnKM = new Button("km");
lblAngles = new Label();
lblArea = new Label();
lblRightAngle = new Label();
// add action-listeners to the buttons
btnCM.addActionListener(this);
btnM.addActionListener(this);
btnKM.addActionListener(this);
// allign the components to the frame
lblInstructions.setBounds(100,10,250,20);
lblSideA.setBounds(100,35,20,20); txtSideA.setBounds(220,35,115,20);
lblSideB.setBounds(100,55,20,20); txtSideB.setBounds(220,55,115,20);
lblSideC.setBounds(100,75,20,20); txtSideC.setBounds(220,75,115,20);
lblSelect.setBounds(130,100,70,20);
btnCM.setBounds(210,100,30,20);
btnM.setBounds(245,100,30,20);
btnKM.setBounds(280,100,30,20);
lblAngles.setBounds(20,140,410,20);
lblArea.setBounds(20,165,410,20);
lblRightAngle.setBounds(20,190,410,20);
// add the components to the frame
add(lblInstructions);
add(lblSideA); add(txtSideA);
add(lblSideB); add(txtSideB);
add(lblSideC); add(txtSideC);
add(lblSelect); add(btnCM); add(btnM); add(btnKM);
add(lblAngles); add(lblArea); add(lblRightAngle);
}
private double[] getAngles(int a, int b, int c) {
// create an array to store the 3 angles
double[] angles = new double[3];
double radians;
// cast all the int values to double
double dA=(double)a, dB=(double)b, dC=(double)c;
// angle A in radians = cos^-1 (b^2 + c^2 - a^2 / 2bc)
// and then convert the radians to degrees
radians = Math.acos((dB*dB + dC*dC - dA*dA) / (2.0*dB*dC));
angles[0] = radians * 180.0 / Math.PI;
// angle B in radians = cos^-1 (a^2 + c^2 - b^2 / 2ac)
// and then convert the radians to degrees
radians = Math.acos((dA*dA + dC*dC - dB*dB) / (2.0*dA*dC));
angles[1] = radians * 180.0 / Math.PI;
// angle C in degrees = 180 - degrees A - degrees B
angles[2] = 180.0 - angles[0] - angles[1];
// return the 3 angles
return angles;
}
private double getArea(int a, int b, int c) {
// Heron's formula
// s = (a + b + c)/2
// area = sqrt( s(s - a)(s - b)(s - c) )
double s = (a + b + c)/2.0;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
private boolean isRightTriangle(double[] angles) {
for(int j = 0; j < angles.length; j++) {
if(Math.abs(angles[j] - 90.0) < 0.05)
return true;
}
return false;
}
}