Results 1 to 5 of 5
- 11-07-2012, 02:00 AM #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 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; }Enter number of courses: 2Java 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 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, 03:51 AM #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?
- 11-07-2012, 11:34 AM #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
- 11-07-2012, 10:54 PM #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)
- 11-07-2012, 10:57 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
[Problem] Enhanced for-loop with 2D arrays (or array in array)
By thewrongsyntax in forum New To JavaReplies: 0Last Post: 10-07-2012, 08:49 PM -
array problem
By surgeon in forum New To JavaReplies: 3Last Post: 05-16-2012, 08:57 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
array problem
By jabo in forum New To JavaReplies: 2Last Post: 03-31-2010, 09:54 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks