So this is my code: import java.io.*; import java.util.*; import java.text.*; pu
ID: 3625443 • Letter: S
Question
So this is my code:import java.io.*;
import java.util.*;
import java.text.*;
public class Statistics {
static DecimalFormat df = new DecimalFormat("#.###");
public static void main(String[] args)
throws FileNotFoundException {
int i = 0, j;
int[]index = new int[10];
int count = 0;
boolean file;
double[]x = new double[1000];
double average,sdd,variance,med;
String filename;
PrintStream output;
System.out.print("Enter input file name: ");
Scanner console = new Scanner(System.in);
filename = console.nextLine();
Scanner input = new Scanner(new File(filename));
System.out.print("Enter output file name(enter if screen): ");
filename = console.nextLine();
if ((filename.length()) == 0){
output = System.out;
}else{
output = new PrintStream(new File(filename));
}
count = input.nextInt();
for (i = 0; i < count; i++){
x[i] = input.nextDouble();
}
Arrays.sort(x);
average = mean(x,count);
med = median(x,count);
j = mode(x,count,index);
variance = calcvariance(x,count,average);
sdd = standardd(variance);
print(output,x,count,average,med,j,index,sdd);
}
public static void print(PrintStream output,double x[], int count,double
average, double med, int j, int index[], double sdd){
int i;
double max = (x[count -1]);
double min = x[0];
output.println("Max: " + df.format(max)+".000");
output.println("Min: " + df.format(min)+".000");
output.println("Average: " + df.format(average));
output.println("Median: " + df.format(med));
output.print("Mode: ");
if(j == -1){
output.println("none");
}
else if(j==0){
output.println(x[index[0]]);
}else{
output.println(df.format(x[j]));
}
output.println("Standard deviation: " + df.format(sdd));
}
public static int mode(double x[], int n, int index[]){
int []ctr = {0,0,0};
double temp = 0;
for(int i =0; i<(n-1); i++){
temp = x[i];
while (temp == x[i]){
i++;
ctr[1]++;
}
if (ctr[1] > ctr[0]){
ctr[0] = ctr[1];
ctr[1] = 0;
i--;
ctr[2] = i;
}
ctr[1]=0;
}
return(ctr[2]);
}
public static double median(double x[],int n){
double mid;
if(n%2 == 1){
mid = x[n/2];
}else{
mid = x[n/2]+x[n/2-1];
mid = mid/2.;
}
return mid;
}
public static double mean(double x[], int n){
double sum = 0;
int i;
for (i = 0; i < n; i ++){
sum += x[i];
}
return (double)sum/n;
}
public static double calc(double x, double avg){
double temp;
temp = x-avg;
return temp*temp;
}
public static double calcvariance(double x[], int n, double average){
double mean, sum = 0;
int i;
for(i = 0; i < n; i++){
sum+=calc(x[i],average);
}
mean = sum/n;
return mean;
}
public static double standardd(double variance){
return Math.sqrt(variance);
}
}
This is its output:
Enter input file name: data_in.txt
Enter output file name(enter if screen):
Max: 10.000
Min: 0.000
Average: 5.076
Median: 5.1
Mode: 4
Standard deviation: 2.819
This is what I need it to output:
Enter input file name: data_in.txt
Enter output file name(enter if screen):
There are 1000 numbers in your input file.
Max: 10.000
Min: 0.000
Mean: 5.076
Median: 5.100
Mode: 4.000
Standard deviation: 2.820
Explanation / Answer
So to make the output correct, first make a variable to hold the count of how many numbers have been read in so that you can print that number later. Your other output mistake is change output of this line output.println("Average: " + df.format(average)); to instead print mean instead of average. Your last output error is use System.out.printf("%.3f",med) also for mode to print out trailing zeros.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.