Advertisement
Guest User

Untitled

a guest
May 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. package generalPractice1;
  2. /*
  3.  * Practice Problem:
  4.  *   - There are a ton of students and you need to determine what letter grade each student got depending on their
  5.  *     percentage.
  6.  *     * Design your code as if there were a thousand students, not just the ones provided.
  7.  *   - Print out how many of each letter grade there are using a count variable.
  8.  *   - print each student's name along with their percentage and grade letter. One student per line.
  9.  *
  10.  * Notes / Recommendations:
  11.  *   - Be sure your code is organized.
  12.  *   - Keep your coding style constant throughout. This includes bracket placement, spaces, etc.
  13.  *     * This makes it easier to read for other people and looks better.
  14.  *   - While not required, disabling the auto complete settings will get you into the habit of opening and closing
  15.  *     parentheses, brackets, etc. Along with where to place semicolons. This will come in handy down the road when
  16.  *     you're taking a Computer Science class and are required to take a written test (meaning no auto-complete).
  17.  */
  18.  
  19. public class Main {
  20.     public static void main(String[] args) {
  21.         int[] grades = {30, 60, 85, 50, 90, 40, 90, 75, 99, 54};
  22.         String[] students = {"Harry", "Josh", "Kyle", "Katelyn", "Tommy", "Jackson", "Jane", "Conner", "Paul", "Nancy"};
  23.         checkGrades(grades, students); // Note: Don't forget to call it like this (in the future). Otherwise it can't run the code below.
  24.        
  25.        
  26.     }
  27.  
  28.     public static void checkGrades(int[] grades, String[] students) { // this is like functions aka modules in Python.
  29.         int countA = 0, countB = 0, countC = 0, countD = 0, countF = 0;
  30.         String pluralCountA = "", pluralCountB = "", pluralCountC = "", pluralCountD = "", pluralCountF = "";
  31.         String pluralCountA2 = "", pluralCountB2 = "", pluralCountC2 = "", pluralCountD2 = "", pluralCountF2 = "";
  32.         String letterGrade = "";
  33.        
  34.         // Determines what each student's grade is for the part that is supposed to print student name, %, and letter grade.
  35.         for (int i = 0; i < grades.length; i++) {
  36.             if (grades[i] >= 90) {
  37.                 letterGrade = "A";
  38.             }
  39.             else if (grades[i] >= 80) {
  40.                 letterGrade = "B";
  41.             }
  42.             else if (grades[i] >= 70) {
  43.                 letterGrade = "C";
  44.             }
  45.             else if (grades[i] >= 60) {
  46.                 letterGrade = "D";
  47.             }
  48.             else if (grades[i] <= 59) {
  49.                 letterGrade = "F";
  50.             }
  51.            
  52.             // Will go through the loop and each time run it for the corresponding student & their grade.
  53.             System.out.println(students[i] + ", " + grades[i] + ", " + letterGrade);
  54.            
  55.             // Counts how many of each letter grade there are.
  56.             if (i >= 90) {
  57.                 ++countA;
  58.             }
  59.             else if (i >= 80) {
  60.                 ++countB;
  61.             }
  62.             else if (i >= 70) {
  63.                 ++countC;
  64.             }
  65.             else if (i >= 60) {
  66.                 ++countD;
  67.             }
  68.             else if (i <= 59) {
  69.                 ++countF;
  70.             }
  71.         } // End of the for loop.
  72.        
  73.         // Determines what type of grammar to use in the println statement later on, depending on whether it is
  74.         // singular or plural.
  75.         if (countA < 2) {
  76.             pluralCountA = "is ";
  77.             pluralCountA2 = ".";
  78.         }
  79.         else {
  80.             pluralCountA = "are ";
  81.             pluralCountA2 = "s.";
  82.         }
  83.        
  84.         if (countB < 2) {
  85.             pluralCountB = "is ";
  86.             pluralCountB2 = ".";
  87.         }
  88.         else {
  89.             pluralCountB = "are ";
  90.             pluralCountB2 = "s.";
  91.         }
  92.        
  93.         if (countC < 2) {
  94.             pluralCountC = "is ";
  95.             pluralCountC2 = ".";
  96.         }
  97.         else {
  98.             pluralCountC = "are ";
  99.             pluralCountC2 = "s.";
  100.         }
  101.         if (countD < 2) {
  102.             pluralCountD = "is ";
  103.             pluralCountD2 = ".";
  104.         }
  105.         else {
  106.             pluralCountD = "are ";
  107.             pluralCountD2 = "s.";
  108.         }
  109.         if (countF < 2) {
  110.             pluralCountF = "is ";
  111.             pluralCountF2 = ".";
  112.         }
  113.         else {
  114.             pluralCountF = "are ";
  115.             pluralCountF2 = "s.";
  116.         }
  117.         System.out.println("There " + pluralCountA + countA + " A grade" + pluralCountA2);
  118.         System.out.println("There " + pluralCountB + countB + " B grade" + pluralCountB2);
  119.         System.out.println("There " + pluralCountC + countC + " C grade" + pluralCountC2);
  120.         System.out.println("There " + pluralCountD + countD + " D grade" + pluralCountD2);
  121.         System.out.println("There " + pluralCountF + countF + " F grade" + pluralCountF2);
  122.  
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement