Box the compass: Difference between revisions

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 {
public class BoxTheCompass {
N, NbE, NNE, NEbN, NE, NEbE, ENE, EbN,
public static void main(String[] args) {
E, EbS, ESE, SEbE, floatSE, heading;SEbS, SSE, SbE,
S, SbW, SSW, SWbS, SW, SWbW, WSW, WbS,
for (int index = 0; index <= 32; index++) {
W, WbN, WNW, NWbW, NW, NWbN, NNW, NbW;
heading = index * 11.25f;
 
switch (index % 3) {
float boundB = midpoint() + bound;{
case 1 -> heading += 5.62;
float midpoint = (360 / 32f) * case 2 -> heading -= 5.62ordinal();
return midpoint == 0 }? 360 : midpoint;
System.out.printf("%-3d", index % 32);
/* we could also use 'Compass.values()[index % 32]' here */
System.out.printf("%-20s", Compass.parse(heading));
System.out.printf("%6.2f%n", heading);
}
}
 
publicfloat[] enum Compassbounds() {
N,float NbE,bound NNE,= NEbN,(360 NE,/ NEbE,32f) ENE,/ EbN,2f;
E,float EbS,midpoint ESE,= SEbE, SE, SEbS, SSE, SbE,midpoint();
S,float SbW,boundA SSW,= SWbS,midpoint SW,- SWbW, WSW, WbS,bound;
W,float WbN,boundB WNW,= NWbW,midpoint NW,+ NWbN, NNW, NbWbound;
if (boundB > 360) boundB -= 360;
return new float[] { boundA, boundB };
}
 
static Compass public parse(float midpoint(degrees) {
float[] midpoint = (360 / 32f) * ordinal()bounds;
float[] return midpointboundsN == 0 ? 360 : midpointN.bounds();
for (Compass value : Compass.values()) {
bounds = value.bounds();
if (degrees >= boundsN[0] || degrees < boundsN[1])
heading = index * 11.25freturn N;
if (degrees >= bounds[0] && degrees < bounds[1])
return value;
}
return stringnull;
}
 
@Override
public float[]String boundstoString() {
float bound = (360 / 32f) / 2f;
String[] strings = new float midpoint = midpointString[name().length()];
int float boundAindex = midpoint - bound0;
for (char letter : name().toCharArray()) {
float boundB = midpoint + bound;
ifswitch (boundB > 360letter) boundB -= 360;{
return new float[] { boundA,case 'N' -> strings[index] boundB= }"north";
case 1'E' -> headingstrings[index] += 5.62"east";
}
case 'WS' -> strings[index] = "westsouth";
 
case 'bW' -> strings[index] = "bywest";
public static Compass parse(float degrees) {
float case 'b' -> strings[index] bounds= "by";
float[] boundsN = N.bounds();
for (Compass value : Compass.values()) {
bounds = value.bounds();
if (degrees >= boundsN[0] || degrees < boundsN[1])
return N;
if (degrees >= bounds[0] && degrees < bounds[1])
return value;
}
return nullindex++;
}
String string
 
= strings[0].substring(0, 1);.toUpperCase() +
@Override
public String toString strings[0].substring(1) {;
switch String[] (strings = new String[name().length()]; {
intcase index2 -> string += 0strings[1];
forcase (char3 letter : name().toCharArray())-> {
switchif (letterstrings[1].equals("by")) {
casestring 'N'+= ->" %s %s".formatted(strings[index1], = "north"strings[2]);
} else case 'E' -> strings[index] = "east";{
casestring 'S'+= "-> %s%s".formatted(strings[index1], = "south"strings[2]);
case 'W' -> strings[index] = "west";
case 'b' -> strings[index] = "by";
}
index++;
}
Stringcase string4 -> {
string += strings[0]String.substringjoin(0" ", strings[1).toUpperCase()], +strings[2], strings[3]);
strings[0].substring(1);
switch (strings.length) {
case 2 -> string += strings[1];
case 3 -> {
if (strings[1].equals("by")) {
string += " %s %s".formatted(strings[1], strings[2]);
} else {
string += "-%s%s".formatted(strings[1], strings[2]);
}
}
case 4 -> {
string += String.join(" ", strings[1], strings[2], strings[3]);
}
}
return string;
}
return Nstring;
}
}
</syntaxhighlight>
Output:
<pre>
0 North 0.00
118

edits