Bear with me, this is a bit long. This class here is creating a student object:
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;
}
}
This class creates a student object, with values to be set from a text file. This next class creates an array of these student objects, as well as a few other things that aren't important at the moment. The important method right now is the setStudents method, which is creates the array of student objects:
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;
}
}
Finally, this class is the driver that is running the entire thing:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CourseStatistics {
static int numberOfClasses = 3;
static int numberOfStudents = 4;
static int numberOfAssignments = 7;
public static void main(String[] args) {
CourseOffering myCourseOffering = new CourseOffering();
Student myStudent = new Student();
myCourseOffering.students = new Student[numberOfStudents];
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);
for (int i = 0; i < numberOfClasses; i++) {
for (int j = 0; j < numberOfStudents; j++) {
myCourseOffering.students[i] = new Student();
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[j].setFirstName(scanner.next());
System.out.print(myCourseOffering.students[j].getFirstName()
+ " ");
myCourseOffering.students[j].setLastName(scanner.next());
System.out.print(myCourseOffering.students[j].getLastName());
for (int k = 0; k < numberOfAssignments; k++) {
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
}
}
}
}
}
What I can't figure out is how to call an array that is inside the student objects array in courseOfferign. I want to call these two methods:
public void setAssignmentScores(int[] assignmentScore) {
assignmentScores = assignmentScore;
}
public int[] getAssignmentScores() {
return assignmentScores;
}
which are both part of the student object array in CourseOffering. I tried to do something similar to when I got the first and last name from the student object array, which I did in this for loop :
for (int k = 0; k < numberOfAssignments; k++) {
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
}
But obviously that doesn't work. I'm trying to fill the assignmentScore array with the line of numbers in the text file. I assume I have to somehow initialize a new array, but I'm not sure how and where to do it. This is the text file I am attempting to read:
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
Collectionand its variants for something this large.