This site requires JavaScript, please enable it in your browser!
Greenfoot back
thekidj
thekidj wrote ...

2013/10/29

I'm not sure what's wrong with my code

thekidj thekidj

2013/10/29

#
I'm trying to get it to return quality points based on the letter grade that's entered, but it won't return them. I just keep getting 0.
/**
 * Write a description of class Course here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Course
{
    private String courseName;
    private int creditHours;
    private char letterGrade;
    private int qualityPoints;

    /**
     * Constructor for objects of class Course
     */
    public Course(String newCourseName, int newCreditHours, char newLetterGrade)
    {
        setNameHoursGrade(newCourseName, newCreditHours, newLetterGrade);
    }
    
    /**
     * 
     */
    public void setNameHoursGrade (String newCourseName, int newCreditHours, char newLetterGrade)
    {
        courseName = newCourseName;
        creditHours = newCreditHours;
        letterGrade = newLetterGrade;
    }
    
    /**
     * 
     */
    private void calculateQPoints()
    {
        if (letterGrade == 'A' || letterGrade == 'a')
        {
            qualityPoints = 4;
        }
        if (letterGrade == 'B' || letterGrade == 'b')
        {
            qualityPoints = 3;
        }
        if (letterGrade == 'C' || letterGrade == 'c')
        {
            qualityPoints = 2;
        }
        if (letterGrade == 'D' || letterGrade == 'd')
        {
            qualityPoints = 1;
        }
        if (letterGrade == 'F' || letterGrade == 'f')
        {
            qualityPoints = 0;
        }
    }
    
    public String getCourseName()
    {
        return courseName;
    }
    public int getCreditHours()
    {
        return creditHours;
    }
    public char getLetterGrade()
    {
        return letterGrade;
    }
    public int getQualityPoints()
    {
        return qualityPoints;
    }
}
thekidj thekidj

2013/10/29

#
Nevermind, fixed it!
thekidj thekidj

2013/10/29

#
I'm trying to access the getQualityPoints and getCreditHours methods from the above class and use them in this class (code below) and it says I can't reference a non-static method from a static context. I tried to do Course c = new Course(); but I don't have any parameters. I just want those values.
/**
 * Write a description of class Student here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Student
{
    private String studentName;
    private Course[] courses;
    private double GPA;
    private int numberOfCourses;

    /**
     * Constructor for objects of class Student
     */
    public Student(String newStudentName, int numCourses)
    {
       studentName = newStudentName;
       numberOfCourses = 0;
       courses = new Course[numCourses];
    }
    
    private void calculateGPA()
    {
        GPA = Course.getQualityPoints() / Course.getCreditHours;
    }

}
danpost danpost

2013/10/29

#
You cannot use the class name to call a method that should be used on an instance of that class (see line 26). That is not the worst of it, as 'calculateQPoints' is not being called anywhere and the calculation of the GPA does not quite look right (though, it may be just the way I am looking at it). Are you even creating Course objects and adding them to the array of the Student object that hold the courses for that student?
thekidj thekidj

2013/10/29

#
danpost wrote...
You cannot use the class name to call a method that should be used on an instance of that class (see line 26). That is not the worst of it, as 'calculateQPoints' is not being called anywhere and the calculation of the GPA does not quite look right (though, it may be just the way I am looking at it).
I called calculateQPoints in getQualityPoints after I posted this and you're correct, my GPA calculation is off. I tried creating an instance of Course by using "Course c = new Course();" but it's saying that I need parameters although I want to use what's already there.
danpost danpost

2013/10/29

#
Yes. For each Course object you create, you must supply (1) a String for the name of the course (2) an int for the number of credit hours for that course and (3) a char for the grade achieved by the student for that course (see the constructor signature for the class Course).
You need to login to post a reply.