0

Histo.java

```
import java.util.*; // Scanner
import java.io.*; // PrintStream
class Histo extends Object
{
static PrintStream theScreen = new PrintStream(System.out);
static Scanner theKeyboard = new Scanner(System.in);
public static void main(String args[])
throws FileNotFoundException
{
final int MAX_SCORES = 5000;
// define nameArr as an array of String objects
String[] nameArr = new String[MAX_SCORES];
// define scoreArr as an array of double objects
double[] scoreArr = new double[MAX_SCORES];
//double[] sorted = new double[MAX_SCORES];
int scores = fillArraysFile( nameArr, scoreArr);
//convert the array into one that just contains valid scores
scoreArr = DoubleArrayOps.subArray(scoreArr, 0, scores-1);
// theScreen.println(Arrays.toString(nameArr));
// DoubleArraysOps.printArray(theScreen, scoreArr);
theScreen.print( "\nMean score: "
+ DoubleArrayOps.average(scoreArr)
+ "\nStd. Dev: "
+ DoubleArrayOps.standardDev(scoreArr)+"\n");
displayArrays( nameArr, scoreArr);
theScreen.print("\n");
printHist(sorted);
}
public static int fillArraysFile (String[] nameArr,double[] scoreArr)
throws FileNotFoundException
{
theScreen.println("Welcome to the grade calculator." +
"\nWritten by Mical.\n");
String name;
double score;
int numscores = 0;
theScreen.print("Enter the name of the data file: ");
String filename = theKeyboard.next();
Scanner input = new Scanner (new File(filename));
while (true)
{
//theScreen.println("Enter name (or 'done' to stop): ");
name = input.next();
if (name.equals("done")) break;
// theScreen.println("\nNow enter score: ");
score = input.nextDouble();
nameArr[numscores] = name;
scoreArr[numscores] = score;
numscores++;
}
return numscores;
}
public static void displayArrays( String[] nameArr,double[] scoreArr)
{
theScreen.println("Name Score");
for (int i = 0; i<scoreArr.length; i++)
theScreen.printf("%-10s%3.2f\n"
,nameArr[i]
,scoreArr[i]);
theScreen.println();
}
public static void printHist(double[] sorted)
{
sorted = scoreArr[i];
int[] counts = new int[101];
//String filename = theKeyboard.next();
//Scanner input = new Scanner (new File(filename));
while (sorted.hasNextInt())
{ // read file into counts array
int score = sorted.nextInt();
counts[score]++; // if score is 87, then counts[87]++
}
for (int i = 0; i < counts.length; i++)
{
if (counts[i] > 0)
{
// counts[i] stores how many students scored i on the test,
// so print a star (counts[i]) times
System.out.print(i + ": ");
for (int j = 0; j < counts[i]; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
}
```

I need to print Histogram with function public static void printHist(double[] sorted) and also I can't able to Max , min in

DoubleArayOps.java

```
import java.io.*; // PrintStream
public class DoubleArrayOps
{
public static double[] subArray(double data[], int start, int stop)
{
double newData[] = new double[stop-start+1];
int storeAt = 0;
for (int i = start; i<= stop; i++)
{
newData[storeAt] = data[i];
storeAt++;
}
return newData;
}
public static void printArray(PrintStream out, double data[])
{
for (int i = 0; i< data.length; i++)
out.print(data[i] + " ");
}
// Swaps a[i] with a[j].
public static void swap(double[] a, int i, int j)
{
if (i != j)
{
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
public void selectionSort(double[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
// find index of smallest element
int smallest = i;
for (int j = i + 1; j < a.length; j++)
{
if (a[j] < a[smallest])
{
smallest = j;
}
}
swap(a, i, smallest); // swap smallest to front
}
}
public static double average (double data[])
{
double sum = 0.0;
for (int i = 0;i<data.length; i++)
{
sum = sum + data[i];
}
return sum/data.length;
}
public static double standardDev (double data[])
{
double sumSqTerms = 0.0;
double avg = average(data);
for (int i = 0; i<data.length; i++)
{
double term = data[i] - avg;
sumSqTerms = sumSqTerms + term * term;
}
return Math.sqrt( sumSqTerms/data.length);
}
}
```

I have to read data files like:

joan 96

joe 99

jane 92

jim 97

janet 96

john 95

johanna 99

jack 91

joeline 93

jacques 92

josh 93

janna 98

jason 94

jadzia 90

jon 95

jackie 93

done

Thanks'

*Edited 3 Years Ago by mical700*