2

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
2
  • can you narrow down your problem.? so that you can get your solution quickly. Commented Nov 18, 2013 at 18:59
  • Also it may be better to look into using Collection and its variants for something this large. Commented Nov 18, 2013 at 19:01

5 Answers 5

2

Your method setAssignmentScores expects an int array as a parameter, you are sending in one single integer, which is incorrect.

myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());

Instead, build an array and pass it

int[] myArray = new int[numberOfAssignments];    // notice this
for (int k = 0; k < numberOfAssignments; k++) {
  myArray[i] = scanner.nextInt();
}
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(myArray);
Sign up to request clarification or add additional context in comments.

6 Comments

So what you're saying is to read individual integers and then store it in a new array, and then pass that to the array that I was trying to call?
Yes, pass the array to setAssignmentScores function.
Ok, so I tried that, but I got the error message "Cannot invoke setAssignmentScores(int[]) on the primitive type int".
Sorry, we need to use int[] and not Integer[], i have made the change please see my edit
This is because the function expects int[]
|
1

If you don't want to expose directly the array then you can choose between two equal ways:

1) build the array and then set it

int[] tempAssignments = new int[numberOfAssignments];
for (int i = 0; i < numberOfAssignments; ++i)
  tempAssignments[i] = scanner.nextInt();
student.setAssignmentScores(tempAssignments);

2) initialize the array in Student and fill it with data

class Student {
  void initializeAssignments(int count) {
    assignmentScores = new int[count];
  }
  void setAssignmentScore(int index, int value) {
    assignmentScores[index] = value;
  ..
}

student.initializeScores(numberOfAssignments);
for (int i = 0; i < numberOfAssignments; ++i)
  student.setAssignmentScore(i, scanner.nextInt());
}

Or you can just use a List<Integer> and live in peace:

class Student {
  final List<Integer> assignmentScores = new ArrayList<Integer>();

  void addScore(int score) {
    assignmentScore.add(score);
  }
}

for (int i = 0; i < numberOfAssignments; ++i)
  student.addScore(scanner.nextInt());

Comments

1
public void setAssignmentScores(int[] assignmentScore)

You are calling this function with passing an integer instead of an array:

myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());

Try to read the assignment score at once to save in an array and then invoke this function with the array.

int assignScores[] = new int[numberOfAssignments] ;
for (int k = 0; k < numberOfAssignments; k++) {
    assignScores[k] =  scanner.nextInt();
}

 myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(assignScores);

Comments

1

The problem is in the line

myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());

assignmentScores is an array of ints, and setAssignmentScores() is a method of the class Student, not the class int. In .assignmentScores[k].setAssignmentScores(..., you are trying to call int's method .setAssignmentScores(..., which doesn't exist. Instead, you need to call Student's method.

Look at the code below, which should replace the line myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());

int[] scores = new int[numberOfAssignments];
for(int k = 0; scanner.hasNextInt(); k++)
{
    scores[k] = scanner.nextInt();
}
myCourseOffering.students[j].setAssignmentScores(scores);

Comments

0
  1. Make assignmentScores an ArrayList or Collection.

  2. I would make a method called addAssignmentScore() that adds to the array of assignment scores.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.