Beginning Java (Working with Arrays; Class Assignment)
Posted
by Jason
on Stack Overflow
See other posts from Stack Overflow
or by Jason
Published on 2010-03-08T06:35:39Z
Indexed on
2010/03/08
6:36 UTC
Read the original article
Hit count: 615
I am to the point where I feel as if I correctly wrote the code for this homework assignment. We were given a skeleton and 2 classes that we had to import (FileIOHelper and Student).
/*
* Created: *** put the date here ***
*
* Author: *** put your name here ***
*
* The program will read information about students and their
* scores from a file, and output the name of each student with
* all his/her scores and the total score, plus the average score
* of the class, and the name and total score of the students with
* the highest and lowest total score.
*/
//
import java.util.Scanner;
import java.io.*;
// C:\Users\Adam\info.txt
public class Lab6
{
public static void main(String[] args) throws IOException
{
// Fill in the body according to the following comments
Scanner key
boardFile = new Scanner(System.in);
// Input file name
String filename = getFileName(keyboardFile);
//Open the file
// Input number of students
int numStudents = FileIOHelper.getNumberOfStudents(filename);
Student students[] = new Student[numStudents];
// Input all student records and create Student array and
// integer array for total scores
int totalScore[] = new int[students.length];
for (int i = 0; i < students.length; i++){
for(int j = 1; j < 4; j++){
totalScore[i] = totalScore[i] + students[i].getScore(j);
}
}
// Compute total scores and find students with lowest and
// highest total score
int maxScore = 0;
int minScore = 0;
for(int i = 0; i < students.length; i++){
if(totalScore[i] >= totalScore[maxScore]){
maxScore = i;
}
else if(totalScore[i] <= totalScore[minScore]){
minScore = i;
}
}
// Compute average total score
int allScores = 0;
int average = 0;
for (int i = 0; i < totalScore.length; i++){
allScores = allScores + totalScore[i];
}
average = allScores / totalScore.length;
// Output results
outputResults(students, totalScore, maxScore, minScore, average);
}
// Given a Scanner in, this method prompts the user to enter
// a file name, inputs it, and returns it.
private static String getFileName(Scanner in)
{
// Fill in the body
System.out.print("Enter the name of a file: ");
String filename = in.next();
return filename;
// Do not declare the Scanner variable in this method.
// You must use the value this method receives in the
// argument (in).
}
// Given the number of students records n to input, this
// method creates an array of Student of the appropriate size,
// reads n student records using the FileIOHelper, and stores
// them in the array, and finally returns the Student array.
private static Student[] getStudents(int n)
{
Student[] myStudents = new Student[n];
for(int i = 0; i <= n; i++){
myStudents[i] = FileIOHelper.getNextStudent();
}
return myStudents;
}
// Given an array of Student records, an array with the total scores,
// the indices in the arrays of the students with the highest and
// lowest total scores, and the average total score for the class,
// this method outputs a table of all the students appropriately
// formatted, plus the total number of students, the average score
// of the class, and the name and total score of the students with
// the highest and lowest total score.
private static void outputResults(
Student[] students, int[] totalScores,
int maxIndex, int minIndex, int average
)
{
// Fill in the body
System.out.println("\nName \t\tScore1 \tScore2 \tScore3 \tTotal");
System.out.println("--------------------------------------------------------");
for(int i = 0; i < students.length; i++){
outputStudent(students[i], totalScores[i], average);
System.out.println();
}
System.out.println("--------------------------------------------------------");
outputNumberOfStudents(students.length);
outputAverage(average);
outputMaxStudent(students[maxIndex], totalScores[maxIndex]);
outputMinStudent(students[minIndex], totalScores[minIndex]);
System.out.println("--------------------------------------------------------");
}
// Given a Student record, the total score for the student,
// and the average total score for all the students, this method
// outputs one line in the result table appropriately formatted.
private static void outputStudent(Student s, int total, int avg)
{
System.out.print(s.getName() + "\t");
for(int i = 1; i < 4; i++){
System.out.print(s.getScore(i) + "\t");
}
System.out.print(total + "\t");
if(total < avg){
System.out.print("-");
}else if(total > avg){
System.out.print("+");
}else{
System.out.print("=");
}
}
// Given the number of students, this method outputs a message
// stating what the total number of students in the class is.
private static void outputNumberOfStudents(int n)
{
System.out.println("The total number of students in this class is: \t" + n);
}
// Given the average total score of all students, this method
// outputs a message stating what the average total score of
// the class is.
private static void outputAverage(int average)
{
System.out.println("The average total score of the class is: \t" + average);
}
// Given the Student with highest total score and the student's
// total score, this method outputs a message stating the name
// of the student and the highest score.
private static void outputMaxStudent(
Student student,
int score
)
{
System.out.println(student.getName() + " got the maximum total score of: \t" + score);
}
// Given the Student with lowest total score and the student's
// total score, this method outputs a message stating the name
// of the student and the lowest score.
private static void outputMinStudent(
Student student,
int score
)
{
System.out.println(student.getName() + " got the minimum total score of: \t" + score);
}
}
But now I get an error at the line totalScore[i] = totalScore[i] + students[i].getScore(j);
Exception in thread "main" java.lang.NullPointerException
at Lab6.main(Lab6.java:42)
© Stack Overflow or respective owner