Created
December 11, 2023 11:21
-
-
Save nikolavojicic/fcd9156170c1fc6e8c68f0a957d3ed47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClockDegree { | |
private static void assertValid(String n, double v) { | |
if (v < 1) throw new IllegalArgumentException(n + " < 1"); | |
if (v > 12) throw new IllegalArgumentException(n + " > 12"); | |
} | |
public static double degree(double h, double m) { | |
assertValid("h", h); | |
assertValid("m", m); | |
h = h == 12 ? 0 : h * 5 * 6; | |
m = m == 12 ? 0 : m * 5 * 6; | |
return h > m | |
? 360 - (h - m) | |
: Math.abs(h - m); | |
} | |
public static void assertSame(double x, double y) { | |
if (x != y) | |
throw new AssertionError(x + " != " + y); | |
} | |
public static void main(String[] args) { | |
assertSame(degree( 1.0, 1.0), 0.0); | |
assertSame(degree( 2.0, 1.0), 330.0); | |
assertSame(degree( 1.0, 2.0), 30.0); | |
assertSame(degree( 9.0, 11.0), 60.0); | |
assertSame(degree(11.0, 9.0), 300.0); | |
assertSame(degree(11.5, 9.5), 300.0); | |
assertSame(degree(12.0, 12.0), 0.0); | |
assertSame(degree( 3.0, 1.5), 315.0); | |
assertSame(degree( 3.3, 1.5), 306.0); | |
assertSame(degree( 1.5, 3.3), 54.0); | |
System.out.println("OK"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment