63.3k views
0 votes
The area of a square is stored in a double variable named area. write an expression whose value is length of the diagonal of the square java

User FreakyAli
by
5.5k points

1 Answer

1 vote

First of all, a bit of theory: since the area of a square is given by


A = s^2

where s is the length of the square. So, if we invert this function we have


s = √(A).

Moreover, the diagonal of a square cuts the square in two isosceles right triangles, whose legs are the sides, so the diagonal is the hypothenuse and it can be found by


d = √(s^2+s^2) = √(2s^2) = s√(2)

So, the diagonal is the side length, multiplied by the square root of 2.

With that being said, your function could be something like this:

double diagonalFromArea(double area) {

double side = Math.sqrt(area);

double diagonal = side * Math.sqrt(2);

return diagonal;

}

User Siegfred
by
5.6k points