# Really need help with an Array problem

• 11-07-2012, 03:00 AM
ndsmith20
Really need help with an Array problem
Need to get a GPA from the amount of courses I enter

GPA class:
Code:

``` import java.util.*;   public class GPA   {       private int points;       private double credits;       private String grade;       private double totalPoints;       private double totalCredits;       private double gpa;       private int numCourses;       private int maxCourses;                   public GPA()       {         points = 0;         credits = 0;         totalCredits = 0;         totalPoints = 0;         grade = "";       }         public GPA(int courses)       {         numCourses = courses;         maxCourses = numCourses;         points = 0;         credits = 0;         totalCredits = 0;         totalPoints = 0;         grade = "";       }   public void addCourse(int cr, String gr)       {                int i = 0;                         int [] sumCredits = new int [numCourses];               int [] sumPoints = new int [numCourses];               do{         sumCredits[i] = cr;         grade = gr;         if((grade.equals("A"))||(grade.equals("a")))         {             points = 4;         }         else if((grade.equals("B"))||(grade.equals("b")))         {             points = 3;         }         else if((grade.equals("C"))||(grade.equals("c")))         {             points = 2;         }         else if((grade.equals("D"))||(grade.equals("d")))         {             points = 1;         }         else if((grade.equals("F"))||(grade.equals("f")))         {             points = 0;         }         else         {             points = -1;         }         sumPoints[i] = points;                         i++;                                             }                         while(i < numCourses);                         for (int k = 0; k < sumCredits.length; k++)         {             totalCredits += sumCredits[k];         }         for(int h = 0; h < sumPoints.length; h++)         {             totalPoints += sumPoints[h];         }             }         public double calcGPA()       {                                 gpa = totalPoints/totalCredits;         return gpa;       }```
Code:

```import java.util.Scanner; public class GpaTestEx2 {         public static void main (String[] args)         {                 //declarations                 Scanner in = new Scanner(System.in); //input object                 int numCourses;        //number of courses - can be changed                 int credits;                //number of credits for a course                 String grade;                //grade for course                                                 //read in number of courses                 System.out.print("Enter number of courses: ");                 numCourses = in.nextInt();                                 //create Gpa object to hold specified number of courses                 GPA myGPA = new GPA(numCourses);                                                 //read in all courses and add course information to Gpa object                 for (int k=0; k<numCourses; k++)                 {                         System.out.print("Enter credits for course " + (k+1) + ": ");                         credits = in.nextInt();                         System.out.print("Enter grade for course " + (k+1) + ": ");                         grade = in.next();                                                 myGPA.addCourse(credits, grade);                 }                                 //print results                 System.out.println();                 System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( ));         } //end main }```
Enter number of courses: 2
Enter credits for course 1: 4
Enter grade for course 1: a
Enter credits for course 2: 3
Enter grade for course 2: b

GPA is 1.00

I keep on getting a GPA of 1.00 back instead of 3.57
• 11-07-2012, 04:51 AM
ndsmith20
Re: Really need help with an Array problem
I think the issue is when i enter the data it reset i to 0 everytime just replacing the info in slot 0 of my array...is there any way to fix this?
• 11-07-2012, 12:34 PM
Kagiso
Re: Really need help with an Array problem
how do you calculate GPA?what's the formula?
because, when i use the test data you've given it tells me that totalPoints is equal to seven and totalCredits is also equal to seven.

try rewriting the addCourse() method and make it shorter because that's where the confusion lies
• 11-07-2012, 11:54 PM
ndsmith20
Re: Really need help with an Array problem
I see what your saying...i have a mistake in my adding of the points...GPA is the sum of points divided by sum of credits. Sum of points is credits for that course times points for that course(the part i forgot to put in my program)
• 11-07-2012, 11:57 PM
ndsmith20
Re: Really need help with an Array problem
Its working now...i dont know why i didnt see that...thank you