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'

Recommended Answers

All 10 Replies

and what exactly is your question?

I have to write a java program.
A certain on-line testing program records student exam results in a text file, each line of which has the form:

name examScore
Write a program that (using an array) analyzes student performance on an exam using the information from such a file. The program should input the name of the text file, and then display the worst score, the best score, the average score, the standard deviation, and a histogram -- a bar graph indicating the frequency with which a given score occurred. For example, if three people scored 74, five people scored 75, six people scored 76, no one scored 77 and two people scored 78, then that portion of the histogram should appear as:

74:
75: **
76:
77:
78: **
Entries below the worst or above the best should not be displayed.

you have to write a program ... ok, since you've posted code, I assumed you already had.
don't just say what you have to do, say what you're having difficulties with. seems to me all you need to implement is a very simple if statement.

I have trouble in Function, public static void printHist(double[] sorted). I can't able to display Histogram. And in, public class DoubleArrayOps, I have trouble swaping min and max.

I am geting errors:

Histo.java:26: incompatible types
found   : double[]
required: double
    double sorted = scoreArr;
                    ^
Histo.java:38: printHist(double[]) in Histo cannot be applied to (double)
    printHist(sorted);
    ^
Histo.java:111: cannot find symbol
symbol  : method hasNextInt()
location: class double[]
    while (sorted.hasNextInt())
                 ^
Histo.java:113: cannot find symbol
symbol  : method nextInt()
location: class double[]
        int score = sorted.nextInt();
                          ^
4 errors

I did every thing I know.

so:
error nr 1:

scoreArr is an array, yet you try to pass it as a single double, which is impossible.
either you store one element of that array as:
double sorted = scoreArr[indexOfWantedElement];
or you add all the values, using an iteration, or you turn sorted into an array.

error nr 2
this too 'll be fixed if you change sorted (see error nr 1) into an array, which is what your method expect.

but, then you'll run into this problem:

(double[] sorted)
    {
    sorted = scoreArr[i];

you'll try to assign a single double to an array of doubles, which is also incorrect.

errors nr 3 and 4
just because the Scanner class contains hasNextInt and nextInt methods doesn't mean that each class has them. double, for instance, doesn't have them, nor does an array of doubles.

your question is itself ambiguous and thus as a beginner I tried it to evaluate the things in the program but could not able to find the exact theme of the site.

Celestia, I have a bit trouble trying to figure out what it is you are trying to say.

what "things" have you tried to evaluate, and what "theme" of what "site" couldn't you find?

Thanks'. Stultuske, I finally submitted with no error and I don't know what celestia trying to say.

@stultuske just a spammer dealth with ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.