【发布时间】:2013-11-18 16:07:39
【问题描述】:
这里的这个类正在创建一个学生对象:
public class Student {
String firstName;
String lastName;
int assignmentScores[];
int labScores[];
int attendanceScore;
int totalHomeworkScore;
int midterm1;
int midterm2;
int finalExam;
int zyanteScore;
int patScore;
int totalTestScore;
String letterGrade;
public Student() {
}
public void setFirstName(String fName) {
firstName = fName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lName) {
lastName = lName;
}
public String getLastName() {
return lastName;
}
public void setAssignmentScores(int[] assignmentScore) {
assignmentScores = assignmentScore;
}
public int[] getAssignmentScores() {
return assignmentScores;
}
public void setLabScores(int[] labScore) {
assignmentScores = labScore;
}
public int[] getLabScores() {
return labScores;
}
public void setAttendanceScore(int attenScore) {
attendanceScore = attenScore;
}
public int getAttendanceScore() {
return attendanceScore;
}
public void setTotalHomeworkScore(int hScore) {
totalHomeworkScore = hScore;
}
public int getTotalHomeworkScore() {
return totalHomeworkScore;
}
public void setMidTerm1(int mT1) {
midterm1 = mT1;
}
public int getMidterm1() {
return midterm1;
}
public void setMidterm2(int mT2) {
midterm2 = mT2;
}
public int getMidterm2() {
return midterm2;
}
public void setFinalExam(int fExam) {
finalExam = fExam;
}
public int getFinalExam() {
return finalExam;
}
public void setZyanteScore(int zyant) {
zyanteScore = zyant;
}
public int getZyanteScore() {
return zyanteScore;
}
public void setPatScore(int pat) {
patScore = pat;
}
public int getPatScore() {
return patScore;
}
public void setTotalTestScore(int tScore) {
totalTestScore = tScore;
}
public int getTotalTestScore() {
return totalTestScore;
}
public void computeGrade() {
if (getTotalHomeworkScore() <= 599 || getTotalTestScore() <= 149
|| getTotalHomeworkScore() <= 719 && getTotalTestScore() <= 179
|| getTotalHomeworkScore() <= 779 && getTotalTestScore() <= 164
|| getTotalHomeworkScore() <= 659 && getTotalTestScore() <= 209) {
letterGrade = "P";
}
if (getTotalHomeworkScore() >= 1140 && getTotalTestScore() >= 180
|| getTotalHomeworkScore() >= 1080
&& getTotalTestScore() >= 195 || getTotalHomeworkScore() >= 960
&& getTotalTestScore() >= 210 || getTotalHomeworkScore() >= 900
&& getTotalTestScore() >= 225 || getTotalHomeworkScore() >= 840
&& getTotalTestScore() >= 240 || getTotalHomeworkScore() >= 780
&& getTotalTestScore() >= 255 || getTotalHomeworkScore() >= 720
&& getTotalTestScore() >= 285) {
letterGrade = "G";
} else {
letterGrade = "A";
}
}
public String getGrade() {
return letterGrade;
}
}
该类创建一个学生对象,其值可从文本文件中设置。下一个类创建了这些学生对象的数组,以及其他一些目前不重要的东西。现在重要的方法是 setStudents 方法,它创建学生对象数组:
public class CourseOffering {
Student[] students;
String description;
double homeworkAverage;
double testAverage;
int passingStudents;
public CourseOffering() {
}
public void setStudents(Student[] studentArray) {
students = studentArray;
}
public void setDescription(String descript) {
description = descript;
}
public String getDescription() {
return description;
}
public double computeHomeworkAverage() {
int temp = 0;
for (int i = 0; i < students.length; i++) {
temp += students[i].getTotalHomeworkScore();
}
homeworkAverage = temp / students.length;
return homeworkAverage;
}
public double computeTestAverage() {
int temp = 0;
for (int j = 0; j < students.length; j++) {
temp += students[j].getTotalTestScore();
}
testAverage = temp / students.length;
return testAverage;
}
public int countPassingStudents() {
int temp = 0;
for (int k = 0; k < students.length; k++) {
if (students[k].getGrade() == "G") {
temp++;
}
}
passingStudents = temp;
return passingStudents;
}
}
最后,这个类是运行整个事情的驱动程序:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CourseStatistics {
static int numberOfClasses = 0;
static int numberOfStudents = 0;
public static void main(String[] args) {
Student myStudent = new Student();
CourseOffering myCourseOffering = new CourseOffering();
Scanner scanner = new Scanner(System.in);
try {
scanner = new Scanner(new File("gradesA5.txt"));
} catch (FileNotFoundException e) {
System.out
.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
System.exit(0);
}
numberOfClasses = scanner.nextInt();
System.out.println(numberOfClasses);
myCourseOffering.setDescription(scanner.next()); // CSCE
myCourseOffering.setDescription(scanner.next()); // 155A
myCourseOffering.setDescription(scanner.next()); // -
myCourseOffering.setDescription(scanner.next()); // Reads Semester
System.out.print(myCourseOffering.getDescription() + " "); // Prints
// Semester
myCourseOffering.setDescription(scanner.next()); // Reads Year
System.out.println(myCourseOffering.getDescription()); // Prints Year
numberOfStudents = scanner.nextInt(); // Number Of Students
System.out.println(numberOfStudents); // Prints number of students
System.out.println("Name" + "\t" + "\t" + "Assignment Score" + "\t"
+ "Test Score" + "\t" + "Grade");
myCourseOffering.students[0].setFirstName(scanner.next());
System.out.println(myCourseOffering.students[0].getFirstName());
// for (int i = 0; i < numberOfClasses; i++) {
// }
}
}
好的,现在是我遇到困难的部分。这段代码是我试图调用学生对象数组的第一个索引并通过读取文本文件设置 .firstName 值的地方:
myCourseOffering.students[0].setFirstName(scanner.next());
System.out.println(myCourseOffering.students[0].getFirstName());
但我保留了空指针异常。异常指向 myCourseOffering.students[0].setFirstName(scanner.next());作为问题线,但我不确定问题是什么。这是我试图阅读的文本文件:
3
CSCE 155A - Fall 2011
4
Anthony Hopkins 80 90 95 87 80 78 25 17 20 22 21 24 19 22 21 23 24 21 20 25 20 55 56 110 30 20 25 8
John Smith 99 95 82 72 64 52 15 14 11 21 25 12 19 20 21 23 21 12 12 10 15 50 50 60 25 15 20 9
Pan Mei 85 92 72 45 82 78 22 13 16 22 24 10 18 12 21 24 25 10 11 14 20 58 51 95 28 14 28 7
Rafael Vega 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
CSCE 155A - Spring 2012
1
Paul Kubi 80 90 5 87 80 0 25 0 20 22 21 24 19 22 21 0 24 21 20 25 20 0 0 0 30 20 25 8
CSCE 155A - Fall 2012
3
Tianna Delp 99 99 99 99 99 99 24 15 16 21 25 15 19 20 21 22 21 21 23 15 15 60 50 60 20 17 20 9
Taylor Delp 95 92 80 90 82 78 25 25 25 25 24 10 25 25 25 25 25 25 25 25 25 58 51 95 28 14 28 7
Rachel Valenz 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
【问题讨论】: