mmmcalc/src/MMMCalc.java

155 lines
3.5 KiB
Java
Raw Normal View History

2014-04-09 16:55:21 -05:00
import java.lang.Math;
import java.util.HashMap;
import java.util.Map;
/**
* @author Nicolás A. Ortega
* @copyright (C) Nicolás A. Ortega
* @license GNU General Public License 3.0 (GPLv3)
* @year 2014
*
*/
public class MMMCalc {
2014-04-09 16:17:15 -05:00
private static boolean verbose = false;
private static float[] numArray;
2014-04-09 16:55:21 -05:00
private static float mean = 0;
private static float median = 0;
private static float mode = 0;
private static float range = 0;
private static float stdDev = 0;
2014-04-09 17:00:35 -05:00
private static float varience = 0;
public static void main(String[] args) {
2014-04-09 16:55:21 -05:00
System.out.println("Welcome to MMMCalc v0.2, a simple tool for basic statistics calculations.\n" +
"This software is licensed under the GNU GPLv3 license and comes WITHOUT WARRANTY.\n");
if(args.length > 0) {
2014-04-08 22:02:56 -05:00
if(args[0].equals("-h")) {
System.out.println("Usage:\n" +
" MMMCalc [options] [variables]\n\n" +
"Options:\n" +
2014-04-09 16:55:21 -05:00
" -h -- Show this help information.\n" +
" -v | -V -- Be verbose (show the work)\n");
2014-04-09 11:59:26 -05:00
} else if(args[0].equals("-v") || args[0].equals("-V")) {
2014-04-09 16:17:15 -05:00
verbose = true;
numArray = new float[args.length - 1];
for(int i = 0; i < numArray.length; i++) {
numArray[i] = Float.parseFloat(args[i+1]);
}
sortArray();
calcMean();
calcMedian();
calcMode();
calcRange();
2014-04-09 16:55:21 -05:00
calcStdDev();
2014-04-09 17:00:35 -05:00
calcVarience();
2014-04-08 22:02:56 -05:00
} else {
numArray = new float[args.length];
2014-04-08 22:02:56 -05:00
for(int i = 0; i < args.length; i++) {
numArray[i] = Float.parseFloat(args[i]) - 0f;
}
2014-04-09 16:17:15 -05:00
sortArray();
2014-04-08 22:02:56 -05:00
calcMean();
calcMedian();
calcMode();
2014-04-09 10:30:04 -05:00
calcRange();
2014-04-09 16:55:21 -05:00
calcStdDev();
2014-04-09 17:00:35 -05:00
calcVarience();
2014-04-08 22:02:56 -05:00
}
} else {
2014-04-08 22:02:56 -05:00
System.out.println("You did not mention any variables. Use the -h argument for help.");
}
}
2014-04-09 16:17:15 -05:00
private static void sortArray() {
int nL = numArray.length;
float tmp = 0;
for(int i = 0; i < nL; i++) {
for(int j = (nL-1); j >= (i+1); j--) {
if(numArray[j] < numArray[j-1]) {
tmp = numArray[j];
numArray[j] = numArray[j-1];
numArray[j-1] = tmp;
}
}
}
}
private static void calcMean() {
float sum = 0;
for(float i: numArray) {
sum += i;
}
mean = sum / (float)numArray.length;
System.out.println("Mean: " + mean);
}
private static void calcMedian() {
2014-04-09 16:55:21 -05:00
int midVar = numArray.length / 2;
2014-04-09 16:55:21 -05:00
median = numArray[midVar];
2014-04-09 16:55:21 -05:00
System.out.println("Median: " + median);
}
private static void calcMode() {
HashMap<Float, Float> fx = new HashMap<Float, Float>();
for(float x: numArray) {
Float f = fx.get(x);
if(f == null) {
fx.put(x, (float)1);
} else {
fx.put(x, f + 1);
}
}
float modeFreq = 0;
for(Map.Entry<Float, Float> entry: fx.entrySet()) {
float freq = entry.getValue();
if(freq > modeFreq) {
modeFreq = freq;
mode = entry.getKey();
}
}
System.out.println("Mode: " + mode + " (frequency: " + modeFreq + ")");
}
2014-04-09 10:30:04 -05:00
private static void calcRange() {
int l = numArray.length -1;
range = numArray[l] - numArray[0];
System.out.println("Range: " + range);
2014-04-09 16:17:15 -05:00
if(verbose) {
System.out.println(numArray[l] + " - " + numArray[0] + " = " + range + "\n");
}
2014-04-09 10:30:04 -05:00
}
2014-04-09 16:55:21 -05:00
private static void calcStdDev() {
float difSum = 0;
for(int i = 0; i < numArray.length; i++) {
difSum += numArray[i] - mean;
}
stdDev = difSum / (float)Math.sqrt((double)numArray.length);
System.out.println("Standard Deviation: " + stdDev);
}
2014-04-09 17:00:35 -05:00
private static void calcVarience() {
// NOTE: I'm doing it this way so I don't have to convert the variables to doubles and lose precision.
varience = stdDev * stdDev;
System.out.println("Varience: " + varience);
}
}