Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. //IntegerStats.Java
  2. /*
  3. * Write a program that loads an array of Integer
  4. * with 20 Random values from 1 to a user input value
  5. * which must be greater than 10.
  6. *
  7. * Write method mean() such that it returns the mean
  8. * average of the values in the list.
  9. *
  10. * Write method sortInteger(), such that it
  11. * sorts the list in ascending order.
  12. *
  13. * Write method median() such that it returns the
  14. * median value in the list. Uses sortInteger().
  15. *
  16. * Write method highest() such that it returns the
  17. * position of the highest value in the list.
  18. *
  19. * //hmmm... mode?
  20. * To calculate the mode
  21. * 1. Get the original list, L
  22. * 2. Get a list of unique values from L, U
  23. * U.length == L.length (size++)
  24. * 3. List of the frequencies of values from
  25. * U found in L, F. (F.length == size)
  26. * 4. Find highest value in F, hValue
  27. * a) if hValue is unique at position p
  28. * then L[p] is the mode.
  29. * b) if hValue is found more than once in F,
  30. * then there is more than one mode.
  31. * c) if hValue is found in all the position
  32. * in F, then there is no mode.
  33. *
  34. *
  35. *
  36. */
  37. import java.util.Random;
  38.  
  39. public class IntegerStats
  40. {
  41.  
  42. public static void main(String[] args)
  43. {
  44. Random R = new Random();
  45.  
  46. //value sets for array L
  47. //sample data set #1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  48. //sample data set #2: 1, 1, 2, 2, 3, 3, 4, 4
  49. //sample data set #3: 2, 2, 2, 2, 3, 3, 5
  50.  
  51. //sample data set #4:
  52. //for set #4 L: 1, 1, 2, 2, 3, 3, 3, 3, 3
  53. // U: 1, 2, 3
  54. // F: 2, 2, 5
  55.  
  56. Integer []L = new Integer[20];
  57. Integer []U = new Integer[20]; //size might be L.length
  58.  
  59. loadInteger(L, R, 10);
  60.  
  61. System.out.print("L: ");
  62. printIntegers(L, 20);
  63.  
  64. int size;
  65. size = loadUnique(L, U);
  66.  
  67. System.out.print("U: ");
  68. printIntegers(U, size);
  69.  
  70. Integer []F = new Integer[size];
  71.  
  72. loadFrequency(L, U, F);
  73.  
  74. System.out.print("F: ");
  75. printIntegers(F, size);
  76.  
  77. }
  78.  
  79.  
  80.  
  81. /*
  82. To calculate the mode
  83. * 1. Get the original list, L
  84. * 2. Get a list of unique values from L, U
  85. * U.length == L.length (size++)
  86. * 3. List of the frequencies of values from
  87. * U found in L, F. (F.length == size)
  88. * 4. Find highest value in F, hValue
  89. * a) if hValue is unique at position p
  90. * then L[p] is the mode.
  91. * b) if hValue is found more than once in F,
  92. * then there is more than one mode.
  93. * c) if hValue is found in all the position
  94. * in F, then there is no mode.
  95. *
  96. */
  97.  
  98. // 1. Get the original list, L
  99. //1.a)
  100. public static Integer getInteger()
  101. {
  102. return new Integer(Keyboard.readInt());
  103. }
  104. //1.b)
  105. public static void loadInteger(Integer []L, Random R, int max)
  106. {
  107. int i;
  108. for (i = 0; i < L.length; i++)
  109. L[i] = new Integer(R.nextInt(max));
  110.  
  111. // L[i] = getInteger();//for user input
  112.  
  113. }
  114.  
  115.  
  116. // 2. Get a list of unique values from L, U
  117. // U.length == L.length (size++)
  118. //2.a) returns true if v is a unique value compared
  119. // to the values that are already in U
  120. public static boolean isUnique(Integer[] U, int size, Integer v)
  121. {
  122. int i;
  123.  
  124. for (i = 0; i < size; i++)
  125. {
  126. if(v.equals(U[i])) //k == L[i]
  127. return false;
  128. }
  129.  
  130. return true;
  131. }
  132. //2.b) loads array U with unique values from array L
  133. // returns then number of unique values found,
  134. // ie, the size of the unique list
  135. public static int loadUnique(Integer[] L, Integer[] U)
  136. {
  137. int k = 0;
  138. int i;
  139.  
  140. for (i = 0; i < L.length; i++)
  141. if (isUnique(U, k, L[i]))
  142. {
  143. U[k] = L[i];
  144. k++;
  145. }
  146.  
  147. return k;
  148. }
  149.  
  150. /*
  151. * 3. List of the frequencies of values from
  152. * U found in L, F. (F.length == size)
  153. */
  154. //loads array F with the frequency of corresponding
  155. //values in U as found in L.
  156. //Example: L: 1, 1, 2, 2, 3, 3, 3, 3, 3
  157. // U: 1, 2, 3
  158. // F: 2, 2, 5
  159. public static void loadFrequency(Integer []L, Integer []U,
  160. Integer [] F)
  161. {
  162. int i, k; //Note: F.length == size
  163. int c;
  164. for (k = 0; k < F.length; k++)
  165. {
  166. c = 0;
  167. for (i = 0; i < L.length; i++)
  168. if (U[k].equals(L[i]))
  169. {
  170. c++;
  171. }
  172. F[k] = new Integer(c);
  173.  
  174. }
  175. }
  176.  
  177.  
  178. //call with (L, L.length) or (U, size)
  179. public static void printIntegers(Integer[] A, int size)
  180. {
  181. int i;
  182.  
  183. for (i = 0; i < size; i++)
  184. System.out.print(A[i] + " ");
  185. System.out.println();
  186. }
  187. }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement