- This is the solution to Challenge 1703
about the direction and compass.
It is an extremely simple exercise
whose purpose is more to get you thinking
and come up with a solution.
Solutions to problems are not always complicated lines
of code but sometimes simple and elegant.
If they serve the purpose for which they were intended.
Then they are a great solution.
If the code is lengthy and convoluted
but it doesn't serve the purpose
it doesn't matter how great the program is.
So let's take a look.
We are going to implement a function,
getDirection that does everything in this program
and then print the results.
So it's pretty simple.
Gonna declare a variable double.
That's going to be our direction, 22 Line.
And we're going to ask the user
to enter a direction between zero and 360.
Then we are using the Scan F
to capture the input from the user.
And basically we have different options right now
depending on the quadrant in which
the direction is measured to be.
So the direction could be either between zero and 90.
Then between 90 and 180, 180 to 270,
or 270 through 360.
So the first if would be if its less than 90.
Meaning it's between zero and 90.
In that case the direction would be Northeast
and whatever the degrees have been entered.
The same thing the direction is less than 180.
Meaning it is between 90 and 180.
Then the direction is South and we calculate it
to be 180 minus the director because if you recall
the graph of the quadrants and angles
if we go below 180 then we are
going below the horizontal line.
And because those are ifs and else ifs the moment
the correct direction is identified
the other ifs are ignored.
The only error correction would be
if the angle entered is large than 360.
Then the program will display an error stating
that the only legal answers are between zero and 360.
That's pretty much it.
Let's test it.
Enter a direction between zero and 360.
I'm going to enter 25
and I'm headed North 25 East.
Run it again, I'm going to enter 186.
Six degree, I'm just above the line.
And that's it!