Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. Code to Copy:
  2.  
  3. import java.util.Scanner;
  4.  
  5. //Implementation of Scorer class
  6.  
  7. public class Scorer {
  8.  
  9. //Implementation of main function
  10.  
  11. public static void main(String[] args)
  12.  
  13. {
  14.  
  15. //Declaration of scores as double type array
  16.  
  17. //with three rows and columns
  18.  
  19. double[][] scores = new double[3][3];
  20.  
  21. userInputValues(scores);
  22.  
  23. double[] average = computeAverage(scores);
  24.  
  25. displaythreerowAverages(average);
  26.  
  27. }
  28.  
  29. //Implementation of userInputValues function with
  30.  
  31. //parameter type double
  32.  
  33. public static void userInputValues(double s[][])
  34.  
  35. {
  36.  
  37. //Declare scanner object
  38.  
  39. Scanner inputObject = new Scanner(System.in);
  40.  
  41. int k =0,j;
  42.  
  43. int rowCount = 1;
  44.  
  45. //Iterate the loop
  46.  
  47. while(k<s.length)
  48.  
  49. {
  50.  
  51. j = 0;
  52.  
  53. //Iterate the loop
  54.  
  55. while(j<s[k].length)
  56.  
  57. {
  58.  
  59. //Get the input from command line
  60.  
  61. System.out.print("value "+rowCount+" : ");
  62.  
  63. s[k][j] = inputObject.nextDouble();
  64.  
  65. j++;
  66.  
  67. rowCount = rowCount + 1;
  68.  
  69. }
  70.  
  71. k++;
  72.  
  73. }
  74.  
  75. }
  76.  
  77.  
  78.  
  79. //Implementation of computeAverage function
  80.  
  81. public static double[] computeAverage(double s[][])
  82.  
  83. {
  84.  
  85. //Declare threerowAverages as type of double
  86.  
  87. double[] threerowAverages = new double[s.length];
  88.  
  89.  
  90.  
  91. //Iterate the loop
  92.  
  93. for(int k=0; k<threerowAverages.length; k++)
  94.  
  95. {
  96.  
  97. threerowAverages[k] = 0;
  98.  
  99. int j = 0;
  100.  
  101. //Iterate the loop
  102.  
  103. while(j<3){
  104.  
  105. threerowAverages[k] = threerowAverages[k] + s[k][j];
  106.  
  107. j++;
  108.  
  109. }
  110.  
  111. //calculate the threerowAverages
  112.  
  113. threerowAverages[k] = threerowAverages[k] / 3;
  114.  
  115. }
  116.  
  117. return threerowAverages;
  118.  
  119. }
  120.  
  121. //Implementation of displaythreerowAverages function
  122.  
  123. //with parameter type double
  124.  
  125. public static void displaythreerowAverages(double[] threerowAverages)
  126.  
  127. {
  128.  
  129. //Display statement for first row
  130.  
  131. System.out.println("The average of row 1 is: " + threerowAverages[0]);
  132.  
  133. //Display statement for second row
  134.  
  135. System.out.println("The average of row 2 is: " + threerowAverages[1]);
  136.  
  137. //Display statement for third row
  138.  
  139. System.out.println("The average of row 3 is: " + threerowAverages[2]);
  140.  
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement