-1

Got the classic assignment where I need to create Student and Course classes and a Driver class.

I'm having trouble calculating the overall class average in my Course class. Everything works, but when I run the Driver, I get '0' back as the class average. In the IDE, it's telling me the 's' in my 's.getAverage();' in the getClassAverage method cannot be resolved, but I'm not really sure why as I am using that in other places just fine. I'm trying to run through an array of students and add all of their averages together.

Any pointers? I appreciate your time!

Student.java

public class Student
{
private String firstName, lastName;
private int testScore1, testScore2, testScore3;
private int studentAverage;

//Constructors
public Student()
{
    firstName = "None";
    lastName = "None";
    testScore1 = 0;
    testScore2 = 0;
    testScore3 = 0;
    studentAverage = 0;
}

public Student(String first, String last, int score1, int score2, int score3)
{
    firstName = first;
    lastName = last;
    testScore1 = score1;
    testScore2 = score2;
    testScore3 = score3;
}

//Setter for test scores
public void setTestScore(int testNum, int score)
{
    if (testNum == 1)
        testScore1 = score;
    else
        if (testNum == 2)
            testScore2 = score;
        else
          if (testNum == 3)
            testScore3 = score;
          else
            throw new IllegalArgumentException(testNum + " is out of range");
    }
//Getter for test scores
public int getTestScore(int testNum2)
{
    if (testNum2 == 1)
        return testScore1;
    else
        if (testNum2 == 2)
            return testScore2;
        else
          if (testNum2 == 3)
            return testScore3;
          else
            throw new IllegalArgumentException(testNum2 + " is out of range");
    }

//Getter for student first name and last name
public String getFirstName()
{
    return firstName;
}

public String getLastName()
{
    return lastName;
}

//Calculates average for each student
public int getAverage()
{
    int studentAverage = (testScore1 + testScore2 + testScore3)/3;
    return studentAverage;
}
//Returns a description of this Student object
public String toString()
{
    String result;

    result = firstName + " " + lastName + "\n";
    result += "Test Score 1:\n" + testScore1 + "\n";
    result += "Test Score 2:\n" + testScore2 + "\n";
    result += "Test Score 3:\n" + testScore3 + "\n";
    result += "Average Test Score:\n" + studentAverage;

    return result;
}
}

Course.java

import java.util.ArrayList;

public class Course {
//What Course class knows
private String courseName;
private int classAverage;
private int studentAverage, sumAverages;
private ArrayList<Student> students;

public static int count = 0;

//Constructor
public Course(String courseName)
{
    this.courseName = courseName;

    students = new ArrayList<Student>();
}

//Add students
public void addStudent(Student s)
{
    students.add(s);
    ++count;
}

//Return average of all student test score averages
public int getClassAverage()
{
    for(Student s : students);
    {
        sumAverages += s.getAverage();
    }

    int classAverage = (int) sumAverages/students.size();
    return classAverage;
}


//Roll call - prints all students in the course
public void roll()
{
    System.out.println("Course: " + courseName);
    System.out.println("Number of Students: " + count);
    System.out.println("Students: ");

    for(Student s : students) 
    {
        System.out.println(s.getFirstName() + " " + s.getLastName());
    }
}

//Prints class average
public String toString()
{
    String result;
    result = "The class average is: "+ classAverage;
    return result;
}

}

Driver

public class CourseDriver {

public static void main(String[] args) {
    //Create a new course
    Course course1 = new Course("CSC 140");

    //Create and add several students to course
    course1.addStudent( new Student("Sarah", "Mauer", 93, 82, 67) );
    course1.addStudent( new Student("Andrew", "Kagan", 85, 80, 75) );
    course1.addStudent( new Student("Carly", "Sanseverino", 95, 83, 80) );

    //Prints roll
    course1.roll();

    System.out.println(course1);
}

}
  • 1
    Get rid of the semicolon here: `for(Student s : students);`. – Hovercraft Full Of Eels Jul 23 '17 at 03:18
  • You never called getClassAverage(), so why do you expect it to be anything but zero? – OneCricketeer Jul 23 '17 at 03:21
  • Try `return "Average " + getClassAverage();`. In other words, there's no need for the field when you are using a method for a calculated variable – OneCricketeer Jul 23 '17 at 03:22
  • You have two errors in your code: First: remove the semicolon after the loop, to avoid the IDE issue: for(Student s : students) { sumAverages += s.getAverage(); } Second: To avoid the 0 average: You declare a new value of classAverage and assign a value to it instead you should assign to the object classAverage: this.classAverage = (int) sumAverages/students.size(); – Fady Saad Jul 23 '17 at 03:26
  • Too many errors -- meaning you're coding in the wrong way. You should test continually and fix all errors immediately, and before trying to add more code. – Hovercraft Full Of Eels Jul 23 '17 at 03:29
  • Thanks for your help everyone! My ultimate bad for having a duplicate question I suppose. – thegatifather Jul 23 '17 at 17:17

1 Answers1

0

The semicolon after the for loop is ending the scope of 's'. Remove that and all should be well.

brian
  • 155
  • 1
  • 7