Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // SuperStat
- // Spec: With the set of numbers, the program will give you information about them.
- import java.util.Arrays;
- public class SuperStat
- {
- private String[] strNums;
- private int[] intNums;
- private int[] modes;
- /** Default / No arg Constructor - sets both strNums and intNums arrays to zero */
- public SuperStat()
- {
- strNums = new String[0];
- intNums = new int[0];
- }
- /** Constructor that takes a String nums and builds both a String[] and an int[] from it */
- public SuperStat(String nums)
- {
- strNums = nums.split(",");
- intNums = new int[strNums.length];
- for(int i = 0; i < strNums.length; i++)
- {
- intNums[i] = Integer.parseInt(strNums[i]);
- }
- }
- /** Calculates the Min for this intNums array */
- public int getMin()
- {
- int min = intNums[0];
- for(int i = 0; i < intNums.length; i++)
- {
- if(intNums[i] < min)
- {
- min = intNums[i];
- }
- }
- return min;
- }
- /** Calculates the Max for this intNums array */
- public int getMax()
- {
- int max = intNums[0];
- for(int i = 0; i <intNums.length; i++)
- {
- if(intNums[i] > max)
- {
- max = intNums[i];
- }
- }
- return max;
- }
- /** Calculates the Range for this intNums array
- /* Note: You should make use of getMax() and getMin() here, don't re-implement code you already have */
- public int getRange()
- {
- int range = getMax() - getMin();
- return range;
- }
- /** Calculates the Arithmetic Mean for this intNums array */
- public double getMean()
- {
- int sum = 0;
- int mean = 0;
- for(int i = 0; i < intNums.length; i++)
- {
- sum += intNums[i];
- }
- mean = (sum / intNums.length);
- return mean;
- }
- //Added a method for myself. Returns the number of modes.
- public int getNumberOfModes()
- {
- Arrays.sort(intNums);
- int x = 1;
- int y = 1;
- int mode = -1;
- int dx = 0;
- int dy = 0;
- int numberOfModes = 0;
- for(int i = 1; i < intNums.length; i++)
- {
- dx = intNums[i];
- dy = intNums[i - 1];
- if(dx == dy)
- {
- x++;
- }
- else
- {
- x = 1;
- }
- if(x > y)
- {
- numberOfModes = 1;
- y = x;
- x = 1;
- }
- else if(x == y)
- {
- numberOfModes++;
- }
- else
- {
- x = 1;
- continue;
- }
- }
- return numberOfModes;
- }
- /** Populates an int[] with a mode or modes from the intNums array
- /* Returns null if no mode found
- /* Note: Be sure to first sort the array (built-in method in Java)
- /* Consider using nested for loops (and several vars) to track potential modes */
- public int[] getMode()
- {
- Arrays.sort(intNums);
- int x = 1;
- int y = 1;
- int mode = -1;
- int dx;
- int dy;
- int dxy = 0;
- modes = new int[getNumberOfModes()];
- if(getNumberOfModes() == 1)
- {
- for(int i = 1; i < intNums.length; i++)
- {
- dx = intNums[i];
- dy = intNums[i - 1];
- if(dx == dy)
- {
- x++;
- }
- else
- {
- x = 1;
- }
- if(x > y)
- {
- modes[0] = intNums[i - 1];
- y = x;
- x = 1;
- }
- else
- {
- x = 1;
- continue;
- }
- }
- }
- else if(getNumberOfModes() == intNums.length - 1) // this makes the method return 0
- {
- modes[0] = 0;
- }
- else if(getNumberOfModes() > 1)
- {
- for(int i = 1; i < intNums.length; i++)
- {
- dx = intNums[i];
- dy = intNums[i - 1];
- if(dx == dy)
- {
- x++;
- }
- else
- {
- x = 1;
- }
- if(x > y)
- {
- dxy = 0;
- modes[dxy] = dx;
- y = x;
- x = 1;
- dxy++;
- }
- else if(x == y)
- {
- modes[dxy] = dx;
- dxy++;
- }
- else
- {
- x = 1;
- continue;
- }
- }
- }
- else
- {
- modes[0] = 0;
- }
- return modes;
- }
- /** Calculates the Geometric Mean for this intNums array */
- public double getGeometricMean()
- {
- double product = 1;
- double geoMean = 0;
- for(int i = 0; i < intNums.length; i++)
- {
- product *= intNums[i];
- }
- geoMean = Math.pow(product, (1.0 / intNums.length));
- return geoMean;
- }
- /** Calculates the median for this intNums array */
- public double getMedian()
- {
- Arrays.sort(intNums);
- double median = 0;
- int x = 0;
- x = (int)(intNums.length/2);
- if(intNums.length % 2 == 1)
- {
- median = intNums[x++];
- return median;
- }
- else
- {
- median = (intNums[x - 1] + intNums[x]) / 2.0;
- return median;
- }
- }
- /** Standard Deviation is calculated
- /* std dev = Square root of the sum of the square differences
- /* from the mean divided by the number of values */
- public double getStdDev()
- {
- double value = 0.0;
- double mean = getMean();
- double sum = 0.0;
- double[] arr = new double[intNums.length];
- for(int i = 0; i < intNums.length; i++)
- {
- arr[i] = intNums[i];
- }
- for(int i = 0; i < intNums.length; i++)
- {
- arr[i] = Math.pow(arr[i] - mean, 2);
- }
- for(int i = 0; i < intNums.length; i++)
- {
- sum += arr[i];
- }
- mean = sum / (arr.length - 1);
- mean = Math.sqrt(mean);
- return mean;
- }
- /** Returns a String with each of the Stats on a separate line
- /* ie. Number of Values, Mean, Median, Mode, Min, Max, Range and Standard Deviation */
- public String toString()
- {
- String result = "";
- result += "\nNumber of Values: " + intNums.length;
- result += "\nArithmetic Mean: " + getMean();
- result += "\nGeometric Mean: " + getGeometricMean();
- result += "\nMedian: " + getMedian();
- result += "\nMode: " + Arrays.toString(getMode());
- result += "\nMin: " + getMin();
- result += "\nMax: " + getMax();
- result += "\nRange: " + getRange();
- result += "\nStandard Deviation: " + getStdDev();
- return result;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // SuperStat
- // Spec: With the set of numbers, the program will give you information about them.
- public class SuperStatClient
- {
- public static void main(String[] args)
- {
- SuperStat nums = new SuperStat("12,40,50,55,33,23");
- System.out.println(nums);
- }
- }
- /*
- Number of Values: 6
- Arithmetic Mean: 35.0
- Geometric Mean: 31.632677318956212
- Median: 36.5
- Mode: [0, 0, 0, 0, 0]
- Min: 12
- Max: 55
- Range: 43
- Standard Deviation: 16.291101865742537
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement