I need help with this assignment. Am I on track? Here is the code that I
have so far.
A school is giving a standardized test to all of its students. There are 50
questions on the test and the school would like to gather some statistics on
how the students performed. Scores can range anywhere from 0 to 50. All the
scores are stored in a file (scores.txt) that you can download. Read the data
from the file and calculate and output the following statistics: average
score, the mode of the scores, the highest/lowest score. In addition, output
a list of the scores and the number of students that received each score.
import java.io.*;
import java util.*;
public static void main (String [ ] args) throws FileNotFoundException
{
int [ ] scores = new int [50];
Scanner inFile = new Scanner(new FileReader (f:\\scores.txt));;
readData(inFile, scores);
print(scores);
System.out.println();
}
public static void readData(Scanner inputFile, int[] list)
{
int score;
int index;
while (inputFile.hasNext())
{
score = inputFile.nextInt();
index = score / ???;
if (index == list.length)
index--;
if (index < list.length)
list[index]++;
}
}
public static void print(int[] list)
{
int range;
int lowRange = 0;
int upperRange = 50;
System.out.println(" Range # of Students");
for (range = 0; range < list.length; range++)
{
System.out.printf("%4d - %3d %10d%n", lowRange,
upperRange, list[range]);
lowRange = upperRange + 1;
upperRange = upperRange + 25;
if (range == list.length - 2)
upperRange++;
}
System.out.println();
}
}
Rhino - 28 Nov 2005 05:14 GMT
>I need help with this assignment. Am I on track? Here is the code that I
> have so far.
I'm not sure what you want: a critique of the coding style; an analysis of
whether the program will do what you want it to do; predictions of compile
errors or something else altogether.
My advice is to see if you can compile it. I see some errors that you will
need to fix: you will learn from the experience of fixing those. If you
don't understand how to fix the problems, ask here and someone will probably
help.
Once you get a clean compile, try executing it. The most important question
is: does it do what you want to do? If not, change it until it does. If this
were a professional program, I'd look for several additional things like
comformance with your employer's standards, documentation of the program,
etc. but for a school course, just having it work correctly is probably all
you'll need to get a decent grade.
If you're looking for style advice, i.e. trying to find out if the program
follows a professional approach and would earn the respect of an employer,
then good for you: I think that exemplifies a good attitude. (Some students
only want to get the assignment out the door to get the teacher off their
backs.) But I'm not going to offer style advice unless you specifically say
that you want some. You are probably too busy with your courses to spend a
lot of time on style issues at this stage; you can polish your style further
down the road.
[snip]
Rhino
anon - 28 Nov 2005 12:21 GMT
> import java.io.*;
import not required
> import java util.*;
missing dot
missing class header
> public static void main (String [ ] args) throws FileNotFoundException
Nothing throws that exception.
> {
> int [ ] scores = new int [50];
Is that the right number to cope with 0 to 50??
> Scanner inFile = new Scanner(new FileReader (f:\\scores.txt));;
filename is a String - use quotes. You don't need the FileReader. Extra
;. Is whitespace the correct delimiter?
> readData(inFile, scores);
> print(scores);
[quoted text clipped - 9 lines]
> {
> score = inputFile.nextInt();
Think about your task - score & index are the same thing.
> index = score / ???;
>
> if (index == list.length)
> index--;
> if (index < list.length)
> list[index]++;
Why do you think you need those "if's"?
> }
> }
[quoted text clipped - 4 lines]
> int lowRange = 0;
> int upperRange = 50;
There is no range involved - you are totalling the students with each
possible score.
> System.out.println(" Range # of Students");
>
[quoted text clipped - 10 lines]
> }
> }
Basically, you need to think about the logic of what you are trying to
achieve - then try coding it.
Mark