0

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);

    }

}
3
Contributors
5
Replies
29
Views
4 Weeks
Discussion Span
Last Post by tinstaafl
0

One thing that caught my eye, is line 64. You're calling doublearea but you're not assigning the return value to anything, or trying to display it anywhere.

Assigning the return value to area should work:

@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 = doublearea(1, 2, 3);
    displayArea.setText(area + "");
}

Edited by tinstaafl

0

You're not using valid numbers for your test. Add a check for validity in the doublearea function. Then try with known good numbers(3,4,5).

public static double doublearea(double side1, double side2, double side3)
{
    if (isvalid(side1, side2, side3))
    {
        s = (side1 + side2 + side3) / 2.0;
        sqroot_val = s * (s - 1) * (s - 2) * (s - 3);
        if (sqroot_val <= 0)
        {
            System.out.println("Invalid Triangle");
            return -1.0;
        }
        area = Math.sqrt(sqroot_val);
        return area;
    }
    System.out.println("Invalid Triangle");
    return -1.0;
}

public static boolean isvalid(double side1, double side2, double side3)
{
    return (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1);
}

On a side note, you'll notice I cleaned up the isvalid function for you and added a suggestion to improve doublearea.

Edited by tinstaafl

0

Just bad luck with the first test data. Triangle with sides 1, 2, 3 is valid, but it’s an extreme case... just visualise that... the 1 and 2 sides have to be end to end and run exactly along the 3 side, so the area really is zero

Ps in the area method s needs to be defined as a local variable.

Edited by JamesCherrill

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.