Write some JAVA for me please, not working the way I wanted it too...
Here's what I am getting when I go to run it:
C:\Documents and Settings\Brad.HOME\Desktop\FINAL>java project1
Usage: project1 [Array File Path]
C:\Documents and Settings\Brad.HOME\Desktop\FINAL>
What I need is for it to ask me to enter the location of the numbers file and then calculate the Max, Min, Mean, etc off of that file I enter in. Right now it just says:
Usage: project1 [Array File Path]
-java below-
import java.io.*;
public class project1 {
/**
* Array File Path
*/
private String location;
/**
* set Location
*
* @param location String
*/
public void setLocation(String location) {
this.location = location;
}
/**
* Program initialization
*/
public project1() {
int ar[] = new int[50];
ReadFile(location, ar);
sort(ar);
System.out.println("Minimum= " + ar[0]);
System.out.println("Maximum=" + ar[ar.length - 1]);
System.out.println("Mean= " + mean(ar));
System.out.println("Mode= " + modal(ar));
System.out.println("Median= " + median(ar));
System.out.println("Std Dev= " + stdDev(ar));
}
/**
* Main program
*
* @param args String[]
*/
public static void main(String args[]) {
if (args.length == 1) {
new project1().setLocation(args[0]);
}
else {
System.out.println("Usage: project1 [Array File Path]");
}
}
/**
* Calculate the mean
*
* @param ar int[]
* @return double
*/
public double mean(int ar[]) {
int sum = 0;
for (int i = 0; i < ar.length; i++) {
sum += ar;
}
return (sum / ar.length);
}
/**
* Calculate the mode/modal
*
* @param a int[]
* @return int
*/
public int modal(int a[]) {
int n;
int freq = 0;
int Mfreq = 0;
int Mn = a[0];
for (int i = 0; i < a.length; i++) {
n = a;
for (int j = 0; j < a.length; j++) {
if (a[j] == n) {
freq++;
}
}
if (freq > Mfreq) {
Mn = n;
}
}
return Mn;
}
/**
* Calculate the median
*
* @param a int[]
* @return int
*/
public int median(int a[]) {
if (a.length % 2 == 0) {
int x = a[a.length / 2];
int y = a[ (a.length + 2) / 2];
return (x + y) / 2;
}
else {
return a[ (a.length + 1) / 2];
}
}
/**
* Calculate Standard Deviation
*
* @param a int[]
* @return double
*/
public double stdDev(int a[]) {
int s2 = 0;
int s = 0;
for (int i = 0; i < a.length; i++) {
s2 += (a * a);
s += a;
}
return Math.sqrt( (s2 - (s * 2) / a.length) / (a.length - 1));
}
/**
* Sort integer array
*
* @param a int[]
*/
public void sort(int a[]) {
int tmp;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[j] > a[j + 1]) {
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
/**
* Read integer array from specified file
*
* @param filename String
* @param ar int[]
* @return int
*/
public int ReadFile(String filename, int ar[]) {
File infile = new File(filename);
int value, n = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(infile));
String line = null;
while ( (line = reader.readLine()) != null) {
value = Integer.parseInt(line);
ar[n++] = value;
}
return n;
}
catch (IOException e) {
System.err.println("Problem reading input file!");
System.exit(1);
}
return n;
}
}

