Q:

Calculating the side length of a hexagon

I have to make a method called getSideLength() for my AP Computer Science class. It calculates the formula of a hexagon using the following formula:
length = √(x2-x1)2+(y2-y1)2
This is the method that I was able to come up with: 
public double getSideLength() {
/* To be implemented in part (a) */
Math.sqrt(x2-x1)Math.pow(2) + (y2-y1)Math.pow(2));
}

I am not really sure how to write that formula in java. Thanks

A:

Your problem is the lack of parenthesis is causing issues with your order of operations. I would reccomend breaking it up into several steps to make the code more readable:
public double getSideLength(){
    double sideLength;
    double deltaX = x2 - x1;
    double deltaY = y2 - y1;
    double squaredSum = Math.pow(deltaX, 2) + Math.pow(deltaY, 2);

    sideLength = Math.sqrt(squaredSum);

    return sideLength;

}

Note: I am not sure where you have defined x1 x2 y1 y2, but this answer assumes they are defined within the scope of this function. If you currently have them as global variables I would recommend changing them to local variables or parameters to the getSideLength() function.

