akosiraff

Download Finding an Average JAVA Answer

Aug 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1.  
  2. Download: https://solutionzip.com/downloads/finding-an-average/
  3. Problem Description: Finding an Average
  4. Your friend Jenny has a class that gives three tests. She would like you to write a program that will take
  5. the three test grades as input and tell her what her average test grade is. For this Lab you are required to
  6. write a program that will read three test grades from the user and then calculate and print the average of
  7. those three test grades.
  8. Step 0: Getting Started
  9. Create a class called
  10. Lab2
  11. . Use the same setup for setting up your class and main method as you did for the
  12. previous examples. Be sure to name your file
  13. Lab2.java
  14. .
  15. Assignment/Lab Documentation
  16. At the beginning of each programming assignment you must have a comment block with the following
  17. information:
  18. /*————————————————————————-
  19. // AUTHOR: your name
  20. // FILENAME: title of the source file
  21. // SPECIFICATION: description of the program
  22. // FOR: CSE 110- Lab #2
  23. // TIME SPENT: how long it took you to complete the assignment
  24. //———————————————————–*/
  25. Step 1: Setting up a Scanner for Input
  26. Since you are required to read in the three test grades from the user, you will have to use a
  27. Scanner
  28. object.
  29. Follow the instructions in the sample video under Chapter 2 Lectures or in the book on page 49 to import
  30. the
  31. Scanner
  32. class from the
  33. java.util
  34. library and create a
  35. Scanner
  36. object to get input from the keyboard
  37. (
  38. System.in
  39. ).Step 2: Declaring Variables
  40. Examining the problem, we see that we will need three inputs from the user. We will need variables to hold
  41. all of the inputs. For this Lab, let’s assume that all the test grades will be integers. Therefore, we will need
  42. 1
  43. three
  44. int
  45. variables to hold the three test grades. Remember, if you need more than one variable of the same
  46. type, you can declare them in the same statement, separated by commas. For example if we needed two
  47. double variables, we could declare them like:
  48. double var1, var2;
  49. Declare three int variables to hold the three test grades. Be sure to give them appropriate
  50. names like test1, test2, etc. rather than x, y, z.
  51. Additionally, looking at the problem, we see that we have the number 3 occurring in the problem. Rather
  52. than simply using this number in the program when needed, it is preferable to declare a constant variable
  53. to hold the number so that when it is used in the program, it will be clear what the 3 refers to. Remember
  54. to create a constant you use the keyword
  55. final
  56. in front of the declaration. Also it is customary to use
  57. ALL_CAPS
  58. for the name of the constant. For example if we wanted a constant to hold the value
  59. PI
  60. , we would
  61. declare:
  62. final double PI = 3.14159;
  63. Declare an int constant to hold the value 3, the number of tests. Be sure to give the constant
  64. an appropriate name like NUM
  65. TESTS.
  66. Finally, when looking at a problem you may need variables to hold the solution or some intermediary steps.
  67. For this problem we need to calculate an average. We will need a variable to hold the average. Usually,
  68. the average of values can contain decimal values, so you will need to declare a
  69. double
  70. variable to hold the
  71. average.Step 3: Getting the Input
  72. Now that we have the needed variables declared, we are ready to use the
  73. Scanner
  74. object we created to get
  75. the input from the user. Before reading in the input though, it is important to give the user a prompt so the
  76. user knows what they are expected to enter. Then we use the
  77. Scanner
  78. object with the appropriate method
  79. to read in the value and store it in a variable. For example to prompt and read in the first test score, we
  80. would use:
  81. System.out.print(“Enter the score on the first test: “); // prompt
  82. test1 = in.nextInt(); // read in the next integer (since test1 is an int) and store it in test1
  83. where the already declared variable
  84. test1
  85. will hold the score for the first test and in is the
  86. Scanner
  87. object.
  88. Write the prompt and read the input for all three tests.Step 4: Calculate the Average
  89. After reading the three input values from the user, we can use them to calculate the average. To do so we
  90. add up all the values and divide them by the number of tests. Naively, this would be:
  91. average = test1 + test2 + test3 / NUM_TESTS;
  92. However, due to operator precedence rules Java will do the division,
  93. test3 / NUM_TESTS
  94. , before the addition,
  95. which will give the wrong result. To force Java to do the addition first, we have to use parentheses:
  96. average = (test1 + test2 + test3) / NUM_TESTS;
  97. This will calculate the average, but there is still a problem. Assume the test grades are 90, 90, and 92, then
  98. the average will be 90.6666, but Java will give the answer as 90 (You should run the program and print the
  99. result to verify). This is because all the variables are integers and so Java does integer division. To force
  100. Java to do decimal division, we have to cast one of the variables to a double.
  101. Download: https://solutionzip.com/downloads/finding-an-average/
Add Comment
Please, Sign In to add comment