Problem 4 (name this Lab3_Problem4) I need this done in Java Write a program tha
ID: 669836 • Letter: P
Question
Problem 4 (name this Lab3_Problem4)
I need this done in Java
Write a program that determines the new compass heading a person will be facing after a turn to the left. The original heading and how many degrees to turn left will be provided by the user as integer values.
For this problem:
Valid compass headings are 1 through 360, inclusive: 360 = north; 90 = east; 180 = south; 270 = west.
A left turn is accomplished by subtracting; a right turn is accomplished by adding.
First, obtain the initial heading from the end user. Use the variable iInitHeading. Construct the dialog as follows (the 45 is just an example of what the user might enter):
Enter the initial heading: 45
Next, prompt the user and obtain the value in degrees to be used for the left turn. Use iTurn as your variable. Construct your dialog as follows. The value of 90 is simply a sample of what the end user might enter:
Enter the number of degrees to turn left: 90
Compile and run your program to make sure it works so far.
Add the code to calculate and display what the heading will be after making the left turn. Use iNewHeading. Construct your output dialog to look like this;
Initial heading: 180 degrees
After 90 degree turn to the left: 90 degrees
Test your program with these values for the initial and target headings; do the math manually:
360 degrees initial, 90 degree left turn
180 degrees initial, 45 degrees left turn
45 degrees initial, 60 degree left turn
90 degrees initial, 90 degree left turn
360 degrees initial; 360 degree left turn (complete circle)
As you test your program by entering various values for original headings, you will discover that some original headings end up with a negative value—which is not possible on a compass.
Also some headings come out to be a value of 0, which is not acceptable since the instructions say that the valid headings are 1 to 360.
To fix this, add an "if" block that will determine whether to apply a "correction factor" to the new heading. Your if test must be designed so that for anything 0 or below, it will "add back" 360 degrees.
Sample output based on an initial heading of 45 degrees (north-northeast) and a 90-degree left turn:
Initial heading: 45 degrees
After 90 degree turn to the left: 315 degrees
Sample output based on an initial heading of 90 degrees (east) and a 90-degree left turn:
Initial heading: 90 degrees
After 90 degree turn to the left: 360 degrees
Explanation / Answer
Please leave me a comment if you have a doubt ;) good luck.
import java.util.Scanner;
public class compass {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
int InitHeading;
int iTurn;
int iNewHeading;
System.out.println("Enter the initial heading:");
InitHeading = input.nextInt();
while ((InitHeading<0)||(InitHeading>360)){
System.out.println("This Value is out of range. Please input a value between 1 and 360");
InitHeading = input.nextInt();
}
System.out.println("Enter the number of degrees to turn left:");
iTurn = input.nextInt();
while ((iTurn <0)||(iTurn >360)){
System.out.println("This Value is out of range. Please input a value between 1 and 360");
iTurn = input.nextInt();
}
iNewHeading=(InitHeading-iTurn);
if (iNewHeading<0){
iNewHeading=((InitHeading -360)*(-1)); //if the value is >0 we multiplicate this value (-1)
}
if (iNewHeading==0){
iNewHeading=360; //if the value is >0 we multiplicate this value (-1)
}
System.out.println("After "+iTurn+" degree turn to the left: ");
System.out.println(iNewHeading);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.