I think the problem I\'m having is more to do with syntax rather than logic. Ok,
ID: 3630146 • Letter: I
Question
I think the problem I'm having is more to do with syntax rather than logic.Ok, so I'm creating a Java program to calculate student GPAs. I've created three classes for the project.
First is the User_Interface, which contains main and isn't really going to do a whole lot.
Next is the Student class, which has methods for student_name, number_of_courses taken, and GPA_calculation.
Finally, the Course class has methods for hours_per_course, and course_grade.
I'm having the most problems with the GPA method of the Student class. In order to compute the GPA, I need to know the total grade points earned and the number of hours attempted. The method for number_of_courses will pass that particular int to GPA_calculation.
I was thinking I should use a "for" loop to create the appropriate number of Course objects based on the int passed from the number_of_courses method. After the creation of the Course object, the hours_per_course and course_grade methods should be called in order to set that information. The object will be stored in an array. After the "for" loop loops enough times, I should have an array of Course objects, i.e. course[0], course[1], etc.
Now that I've got my array of Course objects, I can do the actual calculation for the GPA. Something like this:
gradePointsEarned = (course[0].gradePoints + course[1].gradePoints + etc.);
hoursAttempted = (course[0].hours + course[1].hours + etc.);
GPA = gradePointsEarned/hoursAttempted;
Similarly, I would store Student objects in an array of their own and could print information on various students from main.
So Java experts, please let me know if the logic here is sound, and give me some detailed syntax (which I'm not very good at). I will certainly rate "Lifesaver" for a detailed explanation. Thanks!
Explanation / Answer
From your own explanation you can already see that what the student needs is an array of courses rather than a course grade and a coursesTaken integer. Even logically, it allows for smoother operations.
From what I understand in GPA computations, each course has credit-hours and a grade, and total points earned for each course is credit-hours * grade value (0 to 4+) , we sum all the ponits earned for all courses taken, and divide by the total credit-hours attempted from all the courses to get the semester GPA.
Now, I wrote a prototype following this approach, and I usually add getter methods (they just return private declarations' values safely) as a good practice whether I need them or not. So here are the Course class, the Student class and the UserInterface class (didn't put much thought into the last one).
Course.java
public class Course {
private int hours_per_course;
private double course_grade;
public Course (int hours, double grade)
{
this.hours_per_course = hours;
this.course_grade = grade;
}
public int getHoursPerCourse()
{
return this.hours_per_course;
}
public double getCourseGrade()
{
return this.course_grade;
}
}
Student.java
public class Student {
private String studentName;
private Course[] coursesTaken;
public Student(String name, Course[] courses)
{
this.studentName = name;
this.coursesTaken = courses;
}
public String getStudentName()
{
return this.studentName;
}
public double doGPACalculation()
{
int totalHours = 0;
double totalCredit = 0;
for(int i = 0; i < coursesTaken.length; i++)
{
totalHours = totalHours + coursesTaken[i].getHoursPerCourse();
totalCredit = totalCredit +
( coursesTaken[i].getCourseGrade() *
coursesTaken[i].getHoursPerCourse() );
}
return (double)totalCredit/totalHours;
}
}
UserInterface.java
public class UserInterface {
public static void main(String[] args) {
Course[] courses = new Course[3];
Course cs101 = new Course(3,3); // got a B
Course mth101 = new Course(3,4); // got an A
Course chem101 = new Course(4,3.8); // got a B+
// this should add up to a 3.62 GPA
courses[0] = cs101;
courses[1] = mth101;
courses[2] = chem101;
Student student = new Student("Harry Potter", courses);
System.out.println("The Student: " +
student.getStudentName() +
" achieved a GPA of: " +
student.doGPACalculation() );
}
}