Skip to content

Instantly share code, notes, and snippets.

@Chunky1022
Created April 10, 2015 01:01
Show Gist options
  • Save Chunky1022/0775fba6692456ae1c8c to your computer and use it in GitHub Desktop.
Save Chunky1022/0775fba6692456ae1c8c to your computer and use it in GitHub Desktop.
Stats
package simplestatistics;
import java.util.*;
import java.lang.*;
import java.util.InputMismatchException;
public class SimpleStatistics {
public static double[] getUserInput(Scanner sc) {
List<Double> inputList = new ArrayList<Double>();
System.out.println("Please enter how many numbers you will be inputing");
int numberOfInputs = sc.nextInt();
for (int i = 0; i < numberOfInputs; i++) {
System.out.println("Please enter a number");
double userInput = sc.nextDouble();
inputList.add(userInput);
}
sc.close();
double[] arr = new double[inputList.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = inputList.get(i);
}
return arr;
}
public static double arithmeticMean(double[] nums) {
double mean = 0;
double sum = 0;
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
mean = sum / nums.length;
return mean;
}
public static double geometricMean(double[] nums) {
double gm = 1.0;
for (int i = 0; i < nums.length; i++) {
gm *= nums[i];
}
gm = Math.pow(gm, 1.0 / (double) nums.length);
return gm;
}
public static double[] minAndmax(double[] nums) {
double min = nums[0];
double max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
} else if (nums[i] > max) {
max = nums[i];
} else {
}
}
double[] minAndmax = {min, max};
return minAndmax;
}
public static double[] scaleUp(double[] nums, int factor) {
for (int i = 0; i < nums.length; i++) {
nums[i] *= factor;
}
return nums;
}
public static double[] scaleDown(double[] nums, int factor) {
for (int i = 0; i < nums.length; i++) {
nums[i] /= factor;
}
return nums;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] input = {1, 2.8, 5.3, 100, -5, -6.5};
System.out.println("Choose a option 1-6");
boolean exit = false;
while (!exit) {
System.out.println();
System.out.println("1) Arithmetic mean, 2) Geometric mean, 3) minAndmax, 4) Scale Up, 5) Scale Down, 6) Quit");
System.out.print("Input -> ");
int choice = sc.nextInt();
System.out.println();
switch (choice) {
case 1: {
// Arithmetic mean
System.out.println("Arithmetic mean");
System.out.println(arithmeticMean(getUserInput(sc)));
break;
}
case 2: {
// Geometric mean
System.out.println("Geometric mean");
System.out.println(arithmeticMean(getUserInput(sc)));
break;
}
case 3: {
// Min and max
System.out.println("Min and Max");
for (double i : minAndmax(getUserInput(sc))) {
System.out.print(i + ", ");
}
break;
}
case 4: {
// Scale Up
System.out.println("Scale Up");
System.out.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleUp(input, factor)) {
System.out.print(i + ", ");
}
break;
}
case 5: {
// Scale Down
System.out.println("Scale Down");
System.out.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleDown(input, factor)) {
System.out.print(i + ", ");
}
break;
}
case 6: {
exit = true;
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment