Search code examples
javamathformula

How would you write 2𝑠^2(1 + square root of 2) in java?


I am trying to attempt to create a code block which calculates the area of a floor space with user input (in this case my scanner input variable is s), but the area isn't calculating properly. What would be the correct way to write out the formula? Below is what I currently have.

double sForArea = s * 2;
double floorArea = (Math.pow(sForArea, 2)*(1 + Math.sqrt(2)));

Solution

  • I don't think you've translated that formula correctly.

    I'd guess it should look like this:

    // assume s is some characteristic length
    double floorArea = 2.0*s*s*(1.0 + Math.sqrt(2.0)));
    

    Note: There's no need for Math.pow() here. Squaring a number just means multiplying it by itself.

    Others have noted that you have some ambiguity in your written formula. It's not clear what the term in parentheses is supposed to be multiplying. I wrote it in the way that makes most sense. It keeps the units as length squared, as it must be for area.