# Thread: Really need help with an Array problem

1. Member
Join Date
Oct 2012
Posts
46
Rep Power
0

## Really need help with an Array problem

Need to get a GPA from the amount of courses I enter

GPA class:
Java Code:
import java.util.*;

public class GPA
{
private int points;
private double credits;
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;
}

public GPA(int courses)
{
numCourses = courses;
maxCourses = numCourses;
points = 0;
credits = 0;
totalCredits = 0;
totalPoints = 0;
}

public void addCourse(int cr, String gr)
{
int i = 0;
int [] sumCredits = new int [numCourses];
int [] sumPoints = new int [numCourses];
do{
sumCredits[i] = cr;
{
points = 4;
}
{
points = 3;
}
{
points = 2;
}
{
points = 1;
}
{
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;
}
Java 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

System.out.print("Enter number of courses: ");
numCourses = in.nextInt();

//create Gpa object to hold specified number of courses
GPA myGPA = new GPA(numCourses);

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) + ": ");

}

//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

2. Member
Join Date
Oct 2012
Posts
46
Rep Power
0

## 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?

3. Member
Join Date
Nov 2012
Location
Johannesburg, South Africa
Posts
39
Rep Power
0

## 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

4. Member
Join Date
Oct 2012
Posts
46
Rep Power
0

## 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)

5. Member
Join Date
Oct 2012
Posts
46
Rep Power
0

## Re: Really need help with an Array problem

Its working now...i dont know why i didnt see that...thank you

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•