Box the compass: Difference between revisions
→{{header|Java}}
Line 2,746:
1 North 354.38</syntaxhighlight>
=={{header|Java}}==
<p>
For this task, I used an <kbd>enumeration</kbd> to, contain all 32-points of the compass.<br />
The bounds and mid-points can then be derived using the <code>enum</code> ordinal value.
</p>
<p>
The <code>toString</code> method breaks down the <kbd>enum</kbd> name into characters, and
replaces each with there corresponding written value.
</p>
<syntaxhighlight lang="java">
enum Compass {
N, NbE, NNE, NEbN, NE, NEbE, ENE, EbN,
E, EbS, ESE, SEbE,
S, SbW, SSW, SWbS, SW, SWbW, WSW, WbS,
W, WbN, WNW, NWbW, NW, NWbN, NNW, NbW;
heading = index * 11.25f;▼
case 1 -> heading += 5.62;▼
float midpoint = (360 / 32f) *
return midpoint == 0
}▼
}
if (boundB > 360) boundB -= 360;
return new float[] { boundA, boundB };
static Compass
float[]
}
String[] strings = new
int
for (char letter : name().toCharArray()) {
▲ float boundB = midpoint + bound;
▲ }
▲ for (Compass value : Compass.values()) {
▲ bounds = value.bounds();
▲ if (degrees >= boundsN[0] || degrees < boundsN[1])
▲ if (degrees >= bounds[0] && degrees < bounds[1])
▲ return value;
}
}
String string
▲ @Override
switch
} else
▲ case 'W' -> strings[index] = "west";
▲ case 'b' -> strings[index] = "by";
}
}
string +=
▲ strings[0].substring(1);
}
▲ return string;
}
}
}
</syntaxhighlight>
<pre>
0 North 0.00
|