Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public class Gpa
  2. {
  3. private int[]credits;
  4. private String[] grades;
  5. private int numCourses;
  6. private int maxCourses;
  7.  
  8. public Gpa(int noCourses)
  9. {
  10. maxCourses = noCourses;
  11. numCourses = 0;
  12. credits = new int[maxCourses];
  13. grades = new String[maxCourses];
  14. }
  15.  
  16. public void addCourse(int newCredits, String newGrade)
  17. {
  18. credits[numCourses] = newCredits;
  19. grades[numCourses] = newGrade;
  20. numCourses++;
  21. }
  22.  
  23. public double calcGPA()
  24. {
  25. int totalPts = 0;
  26. int creditSum = 0;
  27. int pts = 0;
  28.  
  29.  
  30. for(int i = 0; i < maxCourses; i++)
  31. {
  32. switch(grades[i])
  33. {
  34. case "A":
  35. case "a": pts = 4;
  36. break;
  37.  
  38. case "B":
  39. case "b": pts = 3;
  40. break;
  41.  
  42. case "C":
  43. case "c": pts = 2;
  44. break;
  45.  
  46. case "D":
  47. case "d": pts = 1;
  48. break;
  49.  
  50. case "F":
  51. case "f": pts = 0;
  52. break;
  53. }
  54.  
  55. creditSum = creditSum + credits[i];
  56. totalPts = totalPts + (credits[i]*pts);
  57. }
  58.  
  59.  
  60. double gpa = (double)totalPts/creditSum;
  61.  
  62. if(gpa > 0)
  63. {
  64. return gpa;
  65. }
  66. else
  67. {
  68. return 0.00;
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement