Guest User

File to be use

a guest
Jan 14th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ScoresList
  4. {
  5.  
  6. static final int EXIT = 7;
  7.  
  8. public static void main (String [] args)
  9. {
  10.  
  11. Scanner scan = new Scanner(System.in);
  12. int value = 0;
  13.  
  14. System.out.print("Enter the minimum value that you will be
  15.  
  16. storing: ");
  17. int minScore = scan.nextInt();
  18.  
  19. System.out.print("Enter the maximum value that you will be
  20.  
  21. storing: ");
  22. int maxScore = scan.nextInt();
  23.  
  24. // instantiate an IntegerList object for the purpose of
  25.  
  26. keeping of with a set of scores sending the grade range for data
  27.  
  28. validation purposes
  29. IntegerList myScores = new IntegerList(minScore, maxScore);
  30.  
  31. int choice = EXIT;
  32. do
  33. {
  34. // print menu and prompt for user's choice
  35. System.out.print("\n\nThis is the Scores List Menu\n \n"
  36.  
  37. +
  38. "1. Add a score to the collection of
  39.  
  40. scores. \n" +
  41. "2. Find the average of the scores
  42.  
  43. now in the collection. \n" +
  44. "3. Find the minimum value of the
  45.  
  46. scores now in the collection. \n"+
  47. "4. Find the maximum value of scores
  48.  
  49. now in the collection. \n" +
  50. "5. Display all scores. \n" +
  51. "6. Find number of occurrences for a
  52.  
  53. particular score\n" +
  54. "7. Exit \n " +
  55. "Enter the number of your choice: "
  56.  
  57. );
  58. // get user's choice
  59. choice = scan.nextInt();
  60. // process the user's choice
  61. switch(choice)
  62. {
  63. case 1 : do {
  64. System.out.print
  65.  
  66. ("Enter the value to be added to the collection or -1 when done ");
  67. value = scan.nextInt();
  68. myScores.add(value);
  69. }while(value !=-
  70.  
  71. 1);
  72. break;
  73. case 2 : System.out.println("The average of my
  74.  
  75. scores is " + myScores.average());
  76. break;
  77. case 3 : System.out.println("The mimimum of my
  78.  
  79. scores is " + myScores.min());
  80. break;
  81. case 4: System.out.println("The maximum of my
  82.  
  83. scores is " + myScores.max());
  84. break;
  85. case 5: myScores.display();
  86. break;
  87. case 6: System.out.print("What score do you want to
  88.  
  89. find?");
  90. int score = scan.nextInt();
  91. System.out.println("Counting how many
  92.  
  93. scores of " + score + " were made...");
  94. System.out.println("Result is : " +
  95.  
  96. myScores.countOf(score));
  97. break;
  98. case 7: System.out.println("Good-bye!");
  99. break;
  100. default : System.out.println("Invalid choice ");
  101.  
  102. } // end switch
  103.  
  104. } while (choice != EXIT);
  105.  
  106. } //end main
  107. } // end class
  108.  
  109.  
  110.  
  111.  
  112. public class IntegerList
  113. {
  114. // Declare constant for default capacity
  115. public final static int CAPACITY=10;
  116.  
  117. //Declare the instance variables
  118. private int count; // holds the number of
  119.  
  120. integers stored in the list
  121. private int [] list; // holds the reference to
  122.  
  123. the array of integers
  124.  
  125. private int minValue;
  126. private int maxValue;
  127.  
  128. //default constructor method used to instantiate an array of
  129.  
  130. size 10 by default
  131. public IntegerList()
  132. {
  133. list = new int[CAPACITY];
  134. count = 0;
  135. minValue=0;
  136. maxValue=100;
  137. }
  138.  
  139. //constructor method used to instantiate an array of a user
  140.  
  141. specified size
  142. public IntegerList( int size )
  143. {
  144. list = new int[size];
  145. count=0;
  146. minValue=0;
  147. maxValue=100;
  148. }
  149.  
  150. //constructor method used to instantiate an array of a user
  151.  
  152. specified size
  153. public IntegerList( int minValue, int maxValue )
  154. {
  155. list = new int[CAPACITY];
  156. count=0;
  157. this.minValue = minValue;
  158. this.maxValue = maxValue;
  159. }
  160.  
  161. /**Appends the specified element to the end of the list
  162. @param v - the element to be added
  163. @return void
  164. */
  165. public void add(int v)
  166. {
  167. if (v >= minValue && v <= maxValue) // if the value is
  168.  
  169. within range
  170. {
  171. if (count < list.length) // if
  172.  
  173. there is still an available slot in the array
  174. {
  175. //Task #1
  176. //NOT IMPLEMENTED
  177.  
  178. YET
  179.  
  180.  
  181. // assign the value v to the next available slot in list.
  182.  
  183.  
  184. //Note that count keeps track of the current empty slot.
  185. }
  186.  
  187. else
  188. {
  189. increase(); //
  190.  
  191. list is full - increase size of list array by calling
  192.  
  193.  
  194. // the private method increase
  195.  
  196. list[count] = v; //now add
  197.  
  198. the new value to list
  199.  
  200. }
  201. count++; // increment the
  202.  
  203. counter
  204. }
  205. else
  206. {
  207. System.out.println("Error: The value is out of
  208.  
  209. range. Value not added");
  210. }
  211.  
  212. }
  213. /**Finds the minimum value store in the list
  214. @return the minimum of all values
  215. */
  216. public int min ()
  217. {
  218. //Task #2
  219. //NOT IMPLEMENTED YET
  220. int min = list[0];
  221. // Use count as the
  222.  
  223. upper boundary of your for-loop
  224. // Why is count used
  225.  
  226. as the upper boundary vs. list.length?
  227.  
  228. return min;
  229. }
  230.  
  231.  
  232. /**Finds the maximum value store in the list
  233. @return the maximum of all values
  234. */
  235. public int max ()
  236. {
  237. //Task #3
  238. //NOT IMPLEMENTED YET
  239. int max = list[0];
  240.  
  241. //Use count as the
  242.  
  243. upper boundary of your for-loop
  244. // Why is count used
  245.  
  246. as the upper boundary vs. list.length?
  247.  
  248.  
  249.  
  250. return max;
  251. }
  252.  
  253. /**Finds the average value store in the list
  254. @return the average of all values
  255. */
  256. public double average ()
  257. {
  258. //Task #4
  259. //NOT IMPLEMENTED YET
  260. // first we calculate the sum
  261. double sum =0;
  262. //Again, use count
  263.  
  264. as the upper boundary rather than list.length?
  265.  
  266. // now we can calculate and return the average - Note: why
  267.  
  268. is count used as the denominator vs. array.length?
  269. return sum / count;
  270. }
  271.  
  272. /**Finds the number of times a score occurs in the list
  273. @return the number of times score occurs
  274. */
  275.  
  276. public int countOf(int score)
  277. {
  278. //Task #5
  279. //NOT IMPLEMENTED YET
  280. int scoreCount=0;
  281. //Again, use count
  282.  
  283. as the upper boundary vs. list.length?
  284. //Step through
  285.  
  286. array. If the element is equal to the "score" I'm looking
  287. //for, then
  288.  
  289. increment scoreCount
  290.  
  291.  
  292. return scoreCount;
  293. }
  294.  
  295. /**Adds elements to the list array by adding CAPACITY number of
  296.  
  297. elements
  298. */
  299. private void increase()
  300. {
  301. int newLength = list.length + CAPACITY; //Add 10
  302.  
  303. to current list.length
  304. int [ ] temp = new int[newLength]; //
  305.  
  306. Create a temp array that is the new list length
  307.  
  308. //Task #6
  309. //NOT IMPLEMENTED YET
  310.  
  311. // Copy contents of list array into the temp array
  312.  
  313.  
  314.  
  315.  
  316. //assign list to temp (Note that this statement uses
  317.  
  318. the address of temp and list)
  319.  
  320.  
  321. }
  322.  
  323. /**Displays all values in the array
  324. */
  325. public void display()
  326. {
  327. for (int i=0; i < list.length; i++)
  328. System.out.println(list[i]);
  329. }
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment