Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ScoresList
- {
- static final int EXIT = 7;
- public static void main (String [] args)
- {
- Scanner scan = new Scanner(System.in);
- int value = 0;
- System.out.print("Enter the minimum value that you will be
- storing: ");
- int minScore = scan.nextInt();
- System.out.print("Enter the maximum value that you will be
- storing: ");
- int maxScore = scan.nextInt();
- // instantiate an IntegerList object for the purpose of
- keeping of with a set of scores sending the grade range for data
- validation purposes
- IntegerList myScores = new IntegerList(minScore, maxScore);
- int choice = EXIT;
- do
- {
- // print menu and prompt for user's choice
- System.out.print("\n\nThis is the Scores List Menu\n \n"
- +
- "1. Add a score to the collection of
- scores. \n" +
- "2. Find the average of the scores
- now in the collection. \n" +
- "3. Find the minimum value of the
- scores now in the collection. \n"+
- "4. Find the maximum value of scores
- now in the collection. \n" +
- "5. Display all scores. \n" +
- "6. Find number of occurrences for a
- particular score\n" +
- "7. Exit \n " +
- "Enter the number of your choice: "
- );
- // get user's choice
- choice = scan.nextInt();
- // process the user's choice
- switch(choice)
- {
- case 1 : do {
- System.out.print
- ("Enter the value to be added to the collection or -1 when done ");
- value = scan.nextInt();
- myScores.add(value);
- }while(value !=-
- 1);
- break;
- case 2 : System.out.println("The average of my
- scores is " + myScores.average());
- break;
- case 3 : System.out.println("The mimimum of my
- scores is " + myScores.min());
- break;
- case 4: System.out.println("The maximum of my
- scores is " + myScores.max());
- break;
- case 5: myScores.display();
- break;
- case 6: System.out.print("What score do you want to
- find?");
- int score = scan.nextInt();
- System.out.println("Counting how many
- scores of " + score + " were made...");
- System.out.println("Result is : " +
- myScores.countOf(score));
- break;
- case 7: System.out.println("Good-bye!");
- break;
- default : System.out.println("Invalid choice ");
- } // end switch
- } while (choice != EXIT);
- } //end main
- } // end class
- public class IntegerList
- {
- // Declare constant for default capacity
- public final static int CAPACITY=10;
- //Declare the instance variables
- private int count; // holds the number of
- integers stored in the list
- private int [] list; // holds the reference to
- the array of integers
- private int minValue;
- private int maxValue;
- //default constructor method used to instantiate an array of
- size 10 by default
- public IntegerList()
- {
- list = new int[CAPACITY];
- count = 0;
- minValue=0;
- maxValue=100;
- }
- //constructor method used to instantiate an array of a user
- specified size
- public IntegerList( int size )
- {
- list = new int[size];
- count=0;
- minValue=0;
- maxValue=100;
- }
- //constructor method used to instantiate an array of a user
- specified size
- public IntegerList( int minValue, int maxValue )
- {
- list = new int[CAPACITY];
- count=0;
- this.minValue = minValue;
- this.maxValue = maxValue;
- }
- /**Appends the specified element to the end of the list
- @param v - the element to be added
- @return void
- */
- public void add(int v)
- {
- if (v >= minValue && v <= maxValue) // if the value is
- within range
- {
- if (count < list.length) // if
- there is still an available slot in the array
- {
- //Task #1
- //NOT IMPLEMENTED
- YET
- // assign the value v to the next available slot in list.
- //Note that count keeps track of the current empty slot.
- }
- else
- {
- increase(); //
- list is full - increase size of list array by calling
- // the private method increase
- list[count] = v; //now add
- the new value to list
- }
- count++; // increment the
- counter
- }
- else
- {
- System.out.println("Error: The value is out of
- range. Value not added");
- }
- }
- /**Finds the minimum value store in the list
- @return the minimum of all values
- */
- public int min ()
- {
- //Task #2
- //NOT IMPLEMENTED YET
- int min = list[0];
- // Use count as the
- upper boundary of your for-loop
- // Why is count used
- as the upper boundary vs. list.length?
- return min;
- }
- /**Finds the maximum value store in the list
- @return the maximum of all values
- */
- public int max ()
- {
- //Task #3
- //NOT IMPLEMENTED YET
- int max = list[0];
- //Use count as the
- upper boundary of your for-loop
- // Why is count used
- as the upper boundary vs. list.length?
- return max;
- }
- /**Finds the average value store in the list
- @return the average of all values
- */
- public double average ()
- {
- //Task #4
- //NOT IMPLEMENTED YET
- // first we calculate the sum
- double sum =0;
- //Again, use count
- as the upper boundary rather than list.length?
- // now we can calculate and return the average - Note: why
- is count used as the denominator vs. array.length?
- return sum / count;
- }
- /**Finds the number of times a score occurs in the list
- @return the number of times score occurs
- */
- public int countOf(int score)
- {
- //Task #5
- //NOT IMPLEMENTED YET
- int scoreCount=0;
- //Again, use count
- as the upper boundary vs. list.length?
- //Step through
- array. If the element is equal to the "score" I'm looking
- //for, then
- increment scoreCount
- return scoreCount;
- }
- /**Adds elements to the list array by adding CAPACITY number of
- elements
- */
- private void increase()
- {
- int newLength = list.length + CAPACITY; //Add 10
- to current list.length
- int [ ] temp = new int[newLength]; //
- Create a temp array that is the new list length
- //Task #6
- //NOT IMPLEMENTED YET
- // Copy contents of list array into the temp array
- //assign list to temp (Note that this statement uses
- the address of temp and list)
- }
- /**Displays all values in the array
- */
- public void display()
- {
- for (int i=0; i < list.length; i++)
- System.out.println(list[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment