Advertisement
nyiaj

gradebook

Nov 28th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*
  4.  * Grade Book
  5.  *
  6.  * By: Nyia Lor
  7.  *
  8.  * The program allows the user to enter student name and scores for
  9.  * exams, quizzes, and homework.  It also has an option of computing
  10.  * the grade for each student and plot the class distribution.  User can go in
  11.  * and add more student data if necessary.  The program supports up to 200 students
  12.  * and a max of 10 grade items for each category.
  13.  *
  14.  */
  15. public class GradeBook {
  16.  
  17.     public static void main(String[] args) {
  18.  
  19.         Scanner s = new Scanner(System.in);
  20.        
  21.         // switch on the type of test
  22.         // add counters -- every time a person is added it
  23.         // starts at the first position at i = 0
  24.        
  25.         int eCounter = 0;
  26.         int qCounter = 0;
  27.         int hCounter = 0;
  28.        
  29.         // arrays for exam, quizzes, homework
  30.         int[][] e = new int[200][10];
  31.         int[][] q = new int[200][10];
  32.         int[][] h = new int[200][10];
  33.         String[] students = new String[200];
  34.  
  35.        
  36.         //There are 200 possible students with one final grade
  37.         double [][] gradeArray = new double[200][1];
  38.  
  39.         System.out.println("Welcome to GradeBook!");
  40.        
  41.         System.out.println("Please provide grade item details");
  42.  
  43.         // prompt the user to enter scores
  44.  
  45.         System.out.print("Exams (number, points, weight)   : ");
  46.  
  47.         // setting exam into an array
  48.  
  49.         String[] exam = s.nextLine().split("\\s+");
  50.  
  51.         // setting quizzes into an array
  52.  
  53.         System.out.print("Quizzes (number, points, weight) : ");
  54.         String[] quizzes = s.nextLine().split("\\s+");
  55.  
  56.         // setting hw into an array
  57.  
  58.         System.out.print("Homework (number, points, weight): ");
  59.         String[] hw = s.nextLine().split("\\s+");
  60.  
  61.         // prompt the user for options
  62.  
  63.         outer: for (;;) {
  64.             System.out.println("\n----------------------------");
  65.             System.out.println("What would you like to do?");
  66.             System.out.println("  1 Add student data");
  67.             System.out.println("  2 Display student grades & statistics");
  68.             System.out.println("  3 Plot grade distribution");
  69.             System.out.println("  4 Quit");
  70.             System.out.print("Your choice: ");
  71.             int choice = s.nextInt();
  72.             s.nextLine();
  73.            
  74.            
  75.  
  76.             // user's option
  77.  
  78.             switch (choice) {
  79.            
  80.             case 1:
  81.                 System.out
  82.                         .println("Enter student data (Enter done with finish): ");
  83.                
  84.                
  85.  
  86.                 for (int i = 0; i < students.length; i++) {
  87.            
  88.                     String data = s.nextLine();
  89.  
  90.                     if (data.equals("done"))
  91.                         break;
  92.  
  93.                     // finding the index of :
  94.  
  95.                     int indexColon = data.indexOf(':');
  96.  
  97.                     String name = data.substring(0, indexColon);
  98.  
  99.                     students[i] = name;
  100.  
  101.                     String scores = data.substring(indexColon + 1,
  102.                             data.length()).trim();
  103.  
  104.                     // regular expression: doesn't matter how many spaces -->
  105.                     // use one space
  106.                     String[] studScore = scores.split("\\s+");
  107.  
  108.                     /**
  109.                      * looking through all of student i's scores via a for loop
  110.                      */
  111.  
  112.                     for (int j = 0; j < studScore.length; j++) {
  113.                        
  114.                        
  115.                        
  116.                         String currentScore = (String) studScore[j];
  117.  
  118.                         // example of studScore[j] : a23
  119.                         // 1. find and store the type of test
  120.  
  121.                         char typeOftest = currentScore.charAt(0);
  122.                         // 2. find and store the test score
  123.  
  124.                         int score = Integer.parseInt(currentScore.substring(1,
  125.                                 currentScore.length()));
  126.  
  127.                             switch (typeOftest) {
  128.                             // store type of test score into the specific array e,
  129.                             // q, and h
  130.                             case 'e':
  131.    
  132.                                 e[i][eCounter] = score;
  133.    
  134.                                 eCounter++;
  135.    
  136.                                 break;
  137.                             case 'q':
  138.    
  139.                                 q[i][qCounter] = score;
  140.    
  141.                                 qCounter++;
  142.    
  143.                                 break;
  144.                             case 'h':
  145.    
  146.                                 h[i][hCounter] = score;
  147.    
  148.                                 hCounter++;
  149.    
  150.                                 break;
  151.                             default:
  152.                                 break;
  153.                             }
  154.                     }
  155.  
  156.                 }
  157.  
  158.                 break;
  159.             case 2:
  160.                 System.out.println("Display student grades & statistics: ");
  161.                
  162.                 // loop for student names
  163.  
  164.                 for (int i = 0; i < students.length; i++) {
  165.                                        
  166.                     if (students[i] == null)break;
  167.  
  168.                     String name = students[i];
  169.  
  170.                     double totalGrade = 0.0;
  171.  
  172.                     double eGrade = 0.0;
  173.  
  174.                     System.out.print("Name");
  175.                     System.out.print(name);
  176.                    
  177.                     // loop for each type of test exam scores
  178.                     // e[i] ==> finding the length of the array inside the
  179.                     // position of e[i]
  180.                                        
  181.                     for (int E = 0; E < e[i].length; E++) {
  182.                                                
  183.                         if(e[i].length == 0)break;
  184.                        
  185.                         //score = the score of exam entered
  186.                        
  187.                         double score = e[i][E];
  188.                        
  189.                         // set grade in here.
  190.                        
  191.                         /*value of what the exam is out of (3 100 60)
  192.                          * exam is out of 100
  193.                          */
  194.                         double outOf = Double.parseDouble(exam[1]);
  195.                        
  196.                         double percentage = score / outOf;
  197.                        
  198.                         eGrade = eGrade + (percentage * 100);
  199.                        
  200.                         System.out.print("Exam");
  201.                         System.out.print(e[i][E]);
  202.                            
  203.                        
  204.                     }
  205.                         /*
  206.                          * Calculating total grade
  207.                          * exam[0] = the number of exams
  208.                          * exam[2] = the weight of exam
  209.                          */
  210.                         eGrade = (eGrade / Double.parseDouble(exam[0]))
  211.                             * (Double.parseDouble(exam[2]) / 100);
  212.                        
  213.                     double qGrade = 0.0;
  214.  
  215.                     // loop for each type of test exam scores
  216.                     // q[i] ==> finding the length of the array inside the
  217.                     // position of q[i]
  218.                                        
  219.                     for (int Q = 0; Q < q[i].length; Q++) {
  220.                        
  221.                        
  222.                         if(q[i].length == 0) break;
  223.                        
  224.                         //score = the score of quiz entered
  225.                        
  226.                         double score = q[i][Q];
  227.                        
  228.                         // set grade in here.
  229.                        
  230.                         /*value of what the quiz is out of (3 100 20)
  231.                          * quiz is out of 100
  232.                          */
  233.                         double outOf = Double.parseDouble(quizzes[1]);
  234.                        
  235.                         double percentage = score / outOf;
  236.                        
  237.                         qGrade = qGrade + (percentage * 100);
  238.                        
  239.                         System.out.print("Quiz");
  240.                         System.out.print(q[i][Q]);
  241.                            
  242.                        
  243.                     }
  244.                     /*
  245.                      * Calculating total grade
  246.                      * quizzes[0] = the number of quizzes
  247.                      * quizzes[2] = the weight of quizzes
  248.                      */
  249.                         qGrade = (qGrade / Double.parseDouble(quizzes[0]))
  250.                             * (Double.parseDouble(quizzes[2]) / 100);
  251.    
  252.                     double hGrade = 0.0;
  253.                                    
  254.                     for (int H = 0; H < h[i].length; H++) {
  255.                                                
  256.                         if(h[i].length == 0)break;
  257.                        
  258.                         //score = the score of hw entered
  259.                        
  260.                         double score = h[i][H];
  261.                        
  262.                         // set grade in here.
  263.                         /*value of what the hw is out of (3 100 20)
  264.                          * hw is out of 100
  265.                          */
  266.                         double outOf = Double.parseDouble(hw[1]);
  267.                        
  268.                         double percentage = score / outOf;
  269.                        
  270.                         hGrade = hGrade + (percentage * 100);
  271.                        
  272.                         System.out.print("Hwork");
  273.                         System.out.print(h[i][H]);
  274.                            
  275.                        
  276.                     }
  277.                     /*
  278.                      * Calculating total grade
  279.                      * hw[0] = the number of homework
  280.                      * hw[2] = the weight of homework
  281.                      */
  282.                         hGrade = (hGrade / Double.parseDouble(hw[0]))
  283.                             * (Double.parseDouble(hw[2]) / 100);
  284.                                            
  285.                         //total grade
  286.                        
  287.                         totalGrade = eGrade + qGrade + hGrade;
  288.                        
  289.                         System.out.print("Grade");
  290.                         System.out.print(totalGrade);
  291.                 }              
  292.                
  293.                 break;
  294.             case 3:
  295.                 System.out.println("Plot grade distribution: ");
  296.                
  297.                 //gradeArray = new double[students[i]][totalGrade]; //array capable of storing the number of grades output
  298.                
  299.                
  300.                
  301.                 break;
  302.             case 4:
  303.                 System.out.println("Goodbye!");
  304.                 break outer;
  305.             default:
  306.                 System.out
  307.                         .println("Please select of the the following option.");
  308.                 break;
  309.             }
  310.         }
  311.  
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement