hi
this is one of my assignment that I have to do. it is a triangle calculator. everything well with it but it just the calculation. when I run it, it gives a zero output. I think it may be my calculation. can someone take a look at it and tell me what is wrong and point me in the right direciton. thanks in advance.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TriangleCalculator extends JFrame implements ActionListener {
JTextField side1, side2, side3, displayArea, displayArea2, exit;
public TriangleCalculator() {
this.setTitle("Triangle Calculator");
this.setSize(400, 400);
JLabel lblA = new JLabel("1", SwingConstants.RIGHT);
side1 = new JTextField(5);
JLabel lblB = new JLabel("2", SwingConstants.RIGHT);
side2 = new JTextField(5);
JLabel lblC = new JLabel("3", SwingConstants.RIGHT);
side3 = new JTextField(5);
JLabel lblArea = new JLabel("Area", SwingConstants.RIGHT);
displayArea = new JTextField(5);
displayArea.setEditable(false);
JLabel lblArea2 = new JLabel("", SwingConstants.RIGHT);
displayArea2 = new JTextField(5);
displayArea2.setEditable(false);
JButton compute = new JButton("compute");
compute.addActionListener(this);
JPanel jp = new JPanel();
jp.setBackground(Color.CYAN);
jp.setLayout(new GridLayout(6, 4, 6, 6));
jp.add(lblA);
jp.add(side1);
jp.add(lblB);
jp.add(side2);
jp.add(lblC);
jp.add(side3);
jp.add(lblArea);
jp.add(displayArea);
jp.add(lblArea2);
jp.add(displayArea2);
this.add(jp);
Container cont = getContentPane();
cont.add(jp, BorderLayout.CENTER);
cont.add(compute, BorderLayout.SOUTH);
pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
double A = Double.parseDouble(side1.getText());
double B = Double.parseDouble(side2.getText());
double C = Double.parseDouble(side3.getText());
double area = 0;
doublearea(1, 2, 3);
displayArea.setText(area + "");
}
double sqroot_val;
double area;
double s=0;
public double doublearea(double side1, double side2, double side3){
s = (side1 + side2 + side3)/2.0;
area = Math.sqrt( s * (s - side1) * (s - side2) * (s - side3));
sqroot_val = s * (s - 1) * (s - 2) * (s - 3);
if(sqroot_val <= 0){
;
System.out.println("Invalid Triangle");
}
area = Math.sqrt(sqroot_val);
return area;
}
public static boolean isvalid(double side1, double side2, double side3) {
boolean isvalid = false;
double side = 0;
if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
return true;
}
else{
return false;
}
}
public static void main(String[] args) {
TriangleCalculator trical = new TriangleCalculator();
trical.setBounds(300, 300, 350, 250);
trical.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}