Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IntegerStats.Java
- /*
- * Write a program that loads an array of Integer
- * with 20 Random values from 1 to a user input value
- * which must be greater than 10.
- *
- * Write method mean() such that it returns the mean
- * average of the values in the list.
- *
- * Write method sortInteger(), such that it
- * sorts the list in ascending order.
- *
- * Write method median() such that it returns the
- * median value in the list. Uses sortInteger().
- *
- * Write method highest() such that it returns the
- * position of the highest value in the list.
- *
- * //hmmm... mode?
- * To calculate the mode
- * 1. Get the original list, L
- * 2. Get a list of unique values from L, U
- * U.length == L.length (size++)
- * 3. List of the frequencies of values from
- * U found in L, F. (F.length == size)
- * 4. Find highest value in F, hValue
- * a) if hValue is unique at position p
- * then L[p] is the mode.
- * b) if hValue is found more than once in F,
- * then there is more than one mode.
- * c) if hValue is found in all the position
- * in F, then there is no mode.
- *
- *
- *
- */
- import java.util.Random;
- public class IntegerStats
- {
- public static void main(String[] args)
- {
- Random R = new Random();
- //value sets for array L
- //sample data set #1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- //sample data set #2: 1, 1, 2, 2, 3, 3, 4, 4
- //sample data set #3: 2, 2, 2, 2, 3, 3, 5
- //sample data set #4:
- //for set #4 L: 1, 1, 2, 2, 3, 3, 3, 3, 3
- // U: 1, 2, 3
- // F: 2, 2, 5
- Integer []L = new Integer[20];
- Integer []U = new Integer[20]; //size might be L.length
- loadInteger(L, R, 10);
- System.out.print("L: ");
- printIntegers(L, 20);
- int size;
- size = loadUnique(L, U);
- System.out.print("U: ");
- printIntegers(U, size);
- Integer []F = new Integer[size];
- loadFrequency(L, U, F);
- System.out.print("F: ");
- printIntegers(F, size);
- }
- /*
- To calculate the mode
- * 1. Get the original list, L
- * 2. Get a list of unique values from L, U
- * U.length == L.length (size++)
- * 3. List of the frequencies of values from
- * U found in L, F. (F.length == size)
- * 4. Find highest value in F, hValue
- * a) if hValue is unique at position p
- * then L[p] is the mode.
- * b) if hValue is found more than once in F,
- * then there is more than one mode.
- * c) if hValue is found in all the position
- * in F, then there is no mode.
- *
- */
- // 1. Get the original list, L
- //1.a)
- public static Integer getInteger()
- {
- return new Integer(Keyboard.readInt());
- }
- //1.b)
- public static void loadInteger(Integer []L, Random R, int max)
- {
- int i;
- for (i = 0; i < L.length; i++)
- L[i] = new Integer(R.nextInt(max));
- // L[i] = getInteger();//for user input
- }
- // 2. Get a list of unique values from L, U
- // U.length == L.length (size++)
- //2.a) returns true if v is a unique value compared
- // to the values that are already in U
- public static boolean isUnique(Integer[] U, int size, Integer v)
- {
- int i;
- for (i = 0; i < size; i++)
- {
- if(v.equals(U[i])) //k == L[i]
- return false;
- }
- return true;
- }
- //2.b) loads array U with unique values from array L
- // returns then number of unique values found,
- // ie, the size of the unique list
- public static int loadUnique(Integer[] L, Integer[] U)
- {
- int k = 0;
- int i;
- for (i = 0; i < L.length; i++)
- if (isUnique(U, k, L[i]))
- {
- U[k] = L[i];
- k++;
- }
- return k;
- }
- /*
- * 3. List of the frequencies of values from
- * U found in L, F. (F.length == size)
- */
- //loads array F with the frequency of corresponding
- //values in U as found in L.
- //Example: L: 1, 1, 2, 2, 3, 3, 3, 3, 3
- // U: 1, 2, 3
- // F: 2, 2, 5
- public static void loadFrequency(Integer []L, Integer []U,
- Integer [] F)
- {
- int i, k; //Note: F.length == size
- int c;
- for (k = 0; k < F.length; k++)
- {
- c = 0;
- for (i = 0; i < L.length; i++)
- if (U[k].equals(L[i]))
- {
- c++;
- }
- F[k] = new Integer(c);
- }
- }
- //call with (L, L.length) or (U, size)
- public static void printIntegers(Integer[] A, int size)
- {
- int i;
- for (i = 0; i < size; i++)
- System.out.print(A[i] + " ");
- System.out.println();
- }
- }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement