heyimakitty
heyimakitty

Reputation: 1

Having trouble with my math? Very simple java

Hi I'm in my first coding class. This is my first code using math and I'm struggling to see where I went wrong, I have no errors but the calculations are undefined.
I need to find the area of the triangle using 3 points. I was given the euqations:

s = (side1 + side2 + side3)/2    
area = sqrt(s(s-side)(s- side2)(s-side3))   
Side = sqrt(x1-y1)+ (x2-y2)

Please help, here's my code:

double sideOne = Math.sqrt(Math.pow((x1cr - x2cr), 2 + Math.pow((y1cr - y2cr), 2)));    
double sideTwo = Math.sqrt(Math.pow((x2cr - x3cr), 2 + Math.pow((y2cr - y3cr), 2)));    
double sideThree = Math.sqrt(Math.pow((x1cr - x3cr), 2 + Math.pow((y1cr - y3cr), 2)));    
double lSide = (sideOne + sideTwo + sideThree) / 2;    
double areaTri = Math.sqrt((lSide * (lSide - sideOne) * (lSide - sideTwo) * (lSide - sideThree)));    
System.out.println("The area of your triangle is " + areaTri);

Edit: Here's the example my teacher gave:

Here is a sample run:

Enter the coordinates of the first vertex (x1, y1) of the triangle: 1.5 -3.4

Enter the coordinates of the second vertex (x2, y2) of the triangle: 4.6 5

Enter the coordinates of the third vertex (x3, y3) of the triangle: 9.5 -3.4

The area of the triangle is 33.6 sq cms

Upvotes: 1

Views: 78

Answers (2)

c0der
c0der

Reputation: 18792

Please review the code and the comments. Don't hesitate to ask if it is not clear:

class Test {

    //always post MCVE (stackoverflow.com/help/mcve)
    public static void main(String[] args) {

        double  x1cr=1.5, y1cr=-3.4, //side one end points 
                x2cr=4.6, y2cr=5,    //side two end points 
                x3cr=9.5, y3cr=-3.4; //side three end points 

        double sideOne = Math.sqrt(squareIt(x1cr, x2cr)+ squareIt(y1cr, y2cr) );
        double sideTwo = Math.sqrt(squareIt(x2cr, x3cr) + squareIt(y2cr, y3cr));
        double sideThree = Math.sqrt(squareIt(x1cr, x3cr)+ squareIt(y1cr, y3cr));

        System.out.println(sideOne+ " "+ sideTwo+ " "+ sideThree);

        double lSide = (sideOne + sideTwo + sideThree) / 2;
        double areaTri = Math.sqrt((lSide * (lSide - sideOne) * (lSide - sideTwo) * (lSide - sideThree)));
        System.out.println("The area of your triangle is " + areaTri);
    }

    //put the repeating calculation in a function
    //it is easy to check such function by say : 
    //  System.out.println(squareIt(5., 3.));
    static double squareIt(Double x1, double x2) {

        return Math.pow((x1 - x2), 2);
    }
}

Upvotes: 0

Raman Sahasi
Raman Sahasi

Reputation: 31841

The problem is how you're calculating sideOne, sideTwo and sideThree.

I believe you calculate sideOne like this:

formula

Then your code should be:

double sideOne = Math.sqrt(Math.pow((x1cr - x2cr), 2) + Math.pow((y1cr - y2cr), 2)); 
                                                    ^                            ^
                                                    1                            2

Note that the formula is same, but the placement of bracket is different. A bracket from position 2 is shifted to position 1.

Similar changes should be done while calculating sideTwo and sideThree as well.

Upvotes: 3

Related Questions