Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Gpa
- {
- private int[]credits;
- private String[] grades;
- private int numCourses;
- private int maxCourses;
- public Gpa(int noCourses)
- {
- maxCourses = noCourses;
- numCourses = 0;
- credits = new int[maxCourses];
- grades = new String[maxCourses];
- }
- public void addCourse(int newCredits, String newGrade)
- {
- credits[numCourses] = newCredits;
- grades[numCourses] = newGrade;
- numCourses++;
- }
- public double calcGPA()
- {
- int totalPts = 0;
- int creditSum = 0;
- int pts = 0;
- for(int i = 0; i < maxCourses; i++)
- {
- switch(grades[i])
- {
- case "A":
- case "a": pts = 4;
- break;
- case "B":
- case "b": pts = 3;
- break;
- case "C":
- case "c": pts = 2;
- break;
- case "D":
- case "d": pts = 1;
- break;
- case "F":
- case "f": pts = 0;
- break;
- }
- creditSum = creditSum + credits[i];
- totalPts = totalPts + (credits[i]*pts);
- }
- double gpa = (double)totalPts/creditSum;
- if(gpa > 0)
- {
- return gpa;
- }
- else
- {
- return 0.00;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement