Advertisement
476179

Assignment2

Nov 8th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. package assignment;
  2.  
  3. //Mitchell Palermo
  4. //06.11.2019
  5. /*this program asks the user for a compass bearing between 0 and 359, the program will then output
  6. the direction the bearing is facing as:
  7. North, North/East, East, South/East, South, South/West, West, or North/West
  8. */
  9.  
  10.  
  11. //this imports the Scanner from the java library
  12. import java.util.Scanner;
  13.  
  14. public class CompassAssignment
  15. {
  16.  
  17. public static void main(String[] args)
  18. {
  19. //--------------Variable Declaration----------------------
  20. //this declares the scanner reference variable as "keyboard"
  21. Scanner keyboard = new Scanner(System.in);
  22. //declaration of an input variable used to store the users input bearing
  23. int input;
  24.  
  25.  
  26. //-------------------Input--------------------------------
  27. //this asks the user to enter an input bearing between 0 and 359
  28. System.out.print("Please enter a bearing between 0 and 359: ");
  29. //this collects the user input and stores it in the "input" integer type variable
  30. input = keyboard.nextInt();
  31.  
  32.  
  33.  
  34. //-------------------Processing & Output-------------------
  35.  
  36. //Primary if statement
  37. //this if statement sorts invalid bearings from valid ones
  38. if (input >= 0 && input < 360)
  39. {
  40.  
  41. //Secondary if statement
  42. //valid input bearings are then sorted producing output displaying its
  43. //correct directional equivalent
  44. if (input >= 0 && input < 45 )
  45. {
  46. //this bearing is North
  47. System.out.println("A bearing of "+input+" means you are heading North.");
  48. }
  49. else if (input == 45)
  50. {
  51. //this bearing is precisely N/E
  52. System.out.println("A bearing of "+input+" means you are heading North/East.");
  53. }
  54. else if (input > 45 && input < 135)
  55. {
  56. //this bearing is East
  57. System.out.println("A bearing of "+input+" means you are heading East.");
  58. }
  59. else if (input == 135)
  60. {
  61. //this bearing is precisely S/E
  62. System.out.println("A bearing of "+input+" means you are heading South/East.");
  63. }
  64. else if (input > 135 && input < 225)
  65. {
  66. //this bearing is South
  67. System.out.println("A bearing of "+input+" means you are heading South.");
  68. }
  69. else if (input == 225)
  70. {
  71. //this bearing is precisely S/W
  72. System.out.println("A bearing of "+input+" means you are heading South/West.");
  73. }
  74. else if (input > 225 && input < 315)
  75. {
  76. //this bearing is West
  77. System.out.println("A bearing of "+input+" means you are heading West.");
  78. }
  79. else if (input == 315)
  80. {
  81. //this bearing is precisely N/W
  82. System.out.println("A bearing of "+input+" means you are heading North/West.");
  83. }
  84. else
  85. {
  86. //if a bearing is to fall under the else of this nested if statement the bearing must be
  87. // 315 < input < 360
  88. System.out.println("A bearing of "+input+" means you are heading North.");
  89. }
  90.  
  91.  
  92. }
  93. else
  94. {
  95. // invalid bearings will produce an output of this error message prompting the user to
  96. //restart the program and enter a valid bearing
  97. System.out.println("That is not a valid bearing. Please restart the program and enter "
  98. + "a bearing between 0 and 359 degrees.");
  99. }
  100.  
  101. //this closes the keyboard scanner so it can no longer accept input
  102. keyboard.close();
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement