When shopping for TVs consumers may notice that a TV's size is given by its diagonal in inches. The aspect ratio of a screen is the ratio of its width to its height. Given the aspect ratio and the diagonal size in inches of a TV screen, you must compute its width and height in inches (see the notes and the first example for more information). If the width or the height is not an integer, round down to the nearest integer; for example, 1.7 would become 1.
You are tasked with writing a method which accepts 3 arguments: an int diagonal specifying the TV's diagonal in inches, an int height specifying the aspect ratio height, and an int width specifying the aspect ratio width. Your program should compute the actual height and width of the TV in inches and return them in a int[] where the first element is the TV's height and the second element is the TV's width.
Definition
Class:
TVSize
Method:
calcSize
Parameters:
int, int, int
Returns:
int[]
Method signature:
int[] calcSize(int diagonal, int height, int width)
(be sure your method is public)
Notes
-
Let W denote the width, H denote the height and D denote the diagonal of the screen. Then W*W + H*H = D*D and W * height = H * width.
Constraints
-
diagonal will be between 5 and 1000, inclusive.
-
height will be between 1 and 99, inclusive.
-
width will be between 2 and 100, inclusive.
-
width will be greater than height.
Examples
0)
52
9
16
Returns: {25, 45 }
W = (width/height) * H D*D = W*W + H*H 52^2 = (width/height)^2 * H^2 + H^2 52^2 = (16/9)^2 * H^2 + H^2 H = 25.49 W = 45.32
1)
7
2
3
Returns: {3, 5 }
2)
13
7
10
Returns: {7, 10 }
3)
7
32
47
Returns: {3, 5 }
4)
11
15
16
Returns: {7, 8 }
public class TVSize {
public int[] calcSize(int d, int h, int w) {
double W = d * w / Math.sqrt(h * h + w * w);
double H = W * h / w;
return new int[] {(int)H, (int)W};
}
}