Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GpaTestEx1
  4. {
  5. public static void main(String[] args)
  6. {
  7. final int NUM_COURSES = 4;
  8.  
  9. Scanner scan = new Scanner(System.in);
  10.  
  11. int[] credits;
  12. credits = new int[NUM_COURSES];
  13. char[] grades;
  14. grades = new char[NUM_COURSES];
  15.  
  16. int creditSum = 0;
  17. int totalPts = 0;
  18.  
  19. for(int i = 0; i < NUM_COURSES; i++)
  20. {
  21. System.out.print("Enter credits for course "+(i+1)+": ");
  22. int credit = scan.nextInt();
  23. credits[i] = credit;
  24.  
  25. System.out.print("Enter grade for course " + (i+1) + ": ");
  26. char grade = scan.next().charAt(0);
  27. grades[i] = grade;
  28.  
  29. switch(grade)
  30. {
  31. case 'A':
  32. case 'a': grade = 4;
  33. break;
  34.  
  35. case 'B':
  36. case 'b': grade = 3;
  37. break;
  38.  
  39. case 'C':
  40. case 'c': grade = 2;
  41. break;
  42.  
  43. case 'D':
  44. case 'd': grade = 1;
  45. break;
  46.  
  47. case 'F':
  48. case 'f': grade = 0;
  49. break;
  50. }
  51.  
  52.  
  53.  
  54. creditSum = creditSum + credit;
  55. totalPts = totalPts + (credit*grade);
  56.  
  57.  
  58. }
  59.  
  60. System.out.printf("\nTotal number of credits: " + creditSum);
  61. System.out.printf("\nTotal number of points: " + totalPts);
  62.  
  63. double gpa = 0;
  64. gpa = (double)totalPts/creditSum;
  65.  
  66. if(gpa > 0)
  67. {
  68. System.out.printf("\nGPA: %5.2f", gpa);
  69. }
  70. else
  71. {
  72. System.out.printf("\nGPA: 0.00");
  73. }
  74.  
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement