swattw

Untitled

Apr 10th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.FileWriter;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. /**
  12. * Assignment #2</p>
  13. * @author Swattw</p>
  14. * Student ID: 403620022</p>
  15. * Description: This program can calculate score.</p>
  16. * Time Spent: 180 minutes.</p> *
  17. *
  18. */
  19.  
  20. public class scorecal {
  21.  
  22. /**
  23. * Collect the data and output the resluts.
  24. * @param args the main program
  25. * @param i as the length of array
  26. */
  27. public static void main(String[] args)
  28. {
  29. //
  30.  
  31. int a[]=new int[1024];
  32. //Declare the new array within 1024 numbers for unknown data.
  33. int i;
  34. Scanner input =new Scanner(System.in);
  35. System.out.println("Please enter the scores:");
  36.  
  37. for( i = 0; i < 1023; i++)
  38. {
  39. int c = input.nextInt();
  40. if (c==-1)
  41. {
  42.  
  43. // as the request. when the program find "-1" than don't put it in the array and stop collecting numbers.
  44. break;
  45. }
  46. a[i] = c;
  47. // collect the value of the scores which user inputs.
  48.  
  49. }
  50.  
  51.  
  52. input.close();
  53. int result1 = minScore(a,i);
  54. // collect the minscore which found in the method.
  55. int result2 = maxScore(a,i);
  56. // collect the maxscore which found in the method.
  57. int result3 = avgScore(a,i);
  58. // collect the avgscore which calculated in the method.
  59. int result4 = numOfNinety(a,i);
  60. // collect the numOfNinety which counted in the method.
  61. boolean result5 = improve(a,i);
  62. // collect the boolean value which tested in the method.
  63.  
  64.  
  65. System.out.println("The min score is:"+result1);
  66. System.out.println("The max score is:"+result2);
  67. System.out.println("The avg score is:"+result3);
  68. System.out.println("The number of score 90 is:"+result4);
  69. if (result5==true )
  70. {
  71. System.out.println("Scores improving!");
  72. }
  73. if (result5==false)
  74. {
  75. System.out.println("Scores NOT improving!");
  76. }
  77.  
  78.  
  79.  
  80. }
  81. /**
  82. Find the minimal score in the array.
  83. @param a[] is the value of the array
  84. @param i is the length of the array
  85. @return the minScore of the array
  86. */
  87. public static int minScore(int a[], int i)
  88.  
  89. {
  90. int minScore=a[0];
  91. for (int j=1; j<i;j++)
  92. {
  93. if (a[j]<minScore)
  94.  
  95. {
  96.  
  97. minScore = a[j];
  98. }
  99.  
  100. }
  101.  
  102.  
  103.  
  104. return minScore;
  105.  
  106. }
  107. /**
  108. Find the maximal score in the array.
  109. @param a[] is the value of the array
  110. @param i is the length of the array
  111. @return the maxScore of the array
  112. */
  113. public static int maxScore(int a[], int i)
  114. {
  115. int maxScore=a[0];
  116. for (int j=1; j<i;j++)
  117. {
  118. if (a[j]>maxScore)
  119.  
  120. {
  121.  
  122. maxScore = a[j];
  123. }
  124.  
  125. }
  126.  
  127. return maxScore;
  128.  
  129. }
  130. /**
  131. Calculate the average score in the array.
  132. @param int is the value of the array
  133. @param int is the length of the array
  134. @return the average of the array
  135. */
  136. public static int avgScore(int a[], int i)
  137. {
  138. int avgScore=a[0];
  139. int sum=0;
  140. for (int j=0; j<=i;j++)
  141. {
  142.  
  143. sum+=a[j];
  144. avgScore = sum/i;
  145. if (sum%i !=0)
  146. {
  147. avgScore ++;
  148. }
  149.  
  150. }
  151. return avgScore;
  152.  
  153. }
  154. /**
  155. Find the numbers of score over 90 in the array.
  156. @param a[] is the value of the array
  157. @param i is the length of the array
  158. @return the numOfNinety of the array
  159. */
  160. public static int numOfNinety(int a[], int i)
  161. {
  162.  
  163. int numOfNinety = 0 ;
  164. for (int j=0;j<i;j++)
  165.  
  166. if (a[j]>=90)
  167. {
  168. numOfNinety++;
  169.  
  170. }
  171.  
  172. return numOfNinety;
  173.  
  174. }
  175. /**
  176. Test if the score is improving.
  177. @param a[] is the value of the array
  178. @param i is the length of the array
  179. @return the improve boolean of the array
  180. */
  181. public static boolean improve (int a[], int i)
  182. {
  183.  
  184. boolean improve = false;
  185. for (int j=1;j<i;j++)
  186. {
  187.  
  188. if (a[j-1]<a[j])
  189. {
  190. improve = true;
  191.  
  192. }
  193. if (a[j-1]>=a[j])
  194. {
  195. improve = false;
  196. break;
  197. }
  198.  
  199. }
  200.  
  201. return improve;
  202.  
  203. }
  204.  
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment