Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- * Grade Book
- *
- * By: Nyia Lor
- *
- * The program allows the user to enter student name and scores for
- * exams, quizzes, and homework. It also has an option of computing
- * the grade for each student and plot the class distribution. User can go in
- * and add more student data if necessary. The program supports up to 200 students
- * and a max of 10 grade items for each category.
- *
- */
- public class GradeBook {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- // switch on the type of test
- // add counters -- every time a person is added it
- // starts at the first position at i = 0
- int eCounter = 0;
- int qCounter = 0;
- int hCounter = 0;
- // arrays for exam, quizzes, homework
- int[][] e = new int[200][10];
- int[][] q = new int[200][10];
- int[][] h = new int[200][10];
- String[] students = new String[200];
- //There are 200 possible students with one final grade
- double [][] gradeArray = new double[200][1];
- System.out.println("Welcome to GradeBook!");
- System.out.println("Please provide grade item details");
- // prompt the user to enter scores
- System.out.print("Exams (number, points, weight) : ");
- // setting exam into an array
- String[] exam = s.nextLine().split("\\s+");
- // setting quizzes into an array
- System.out.print("Quizzes (number, points, weight) : ");
- String[] quizzes = s.nextLine().split("\\s+");
- // setting hw into an array
- System.out.print("Homework (number, points, weight): ");
- String[] hw = s.nextLine().split("\\s+");
- // prompt the user for options
- outer: for (;;) {
- System.out.println("\n----------------------------");
- System.out.println("What would you like to do?");
- System.out.println(" 1 Add student data");
- System.out.println(" 2 Display student grades & statistics");
- System.out.println(" 3 Plot grade distribution");
- System.out.println(" 4 Quit");
- System.out.print("Your choice: ");
- int choice = s.nextInt();
- s.nextLine();
- // user's option
- switch (choice) {
- case 1:
- System.out
- .println("Enter student data (Enter done with finish): ");
- for (int i = 0; i < students.length; i++) {
- String data = s.nextLine();
- if (data.equals("done"))
- break;
- // finding the index of :
- int indexColon = data.indexOf(':');
- String name = data.substring(0, indexColon);
- students[i] = name;
- String scores = data.substring(indexColon + 1,
- data.length()).trim();
- // regular expression: doesn't matter how many spaces -->
- // use one space
- String[] studScore = scores.split("\\s+");
- /**
- * looking through all of student i's scores via a for loop
- */
- for (int j = 0; j < studScore.length; j++) {
- String currentScore = (String) studScore[j];
- // example of studScore[j] : a23
- // 1. find and store the type of test
- char typeOftest = currentScore.charAt(0);
- // 2. find and store the test score
- int score = Integer.parseInt(currentScore.substring(1,
- currentScore.length()));
- switch (typeOftest) {
- // store type of test score into the specific array e,
- // q, and h
- case 'e':
- e[i][eCounter] = score;
- eCounter++;
- break;
- case 'q':
- q[i][qCounter] = score;
- qCounter++;
- break;
- case 'h':
- h[i][hCounter] = score;
- hCounter++;
- break;
- default:
- break;
- }
- }
- }
- break;
- case 2:
- System.out.println("Display student grades & statistics: ");
- // loop for student names
- for (int i = 0; i < students.length; i++) {
- if (students[i] == null)break;
- String name = students[i];
- double totalGrade = 0.0;
- double eGrade = 0.0;
- System.out.print("Name");
- System.out.print(name);
- // loop for each type of test exam scores
- // e[i] ==> finding the length of the array inside the
- // position of e[i]
- for (int E = 0; E < e[i].length; E++) {
- if(e[i].length == 0)break;
- //score = the score of exam entered
- double score = e[i][E];
- // set grade in here.
- /*value of what the exam is out of (3 100 60)
- * exam is out of 100
- */
- double outOf = Double.parseDouble(exam[1]);
- double percentage = score / outOf;
- eGrade = eGrade + (percentage * 100);
- System.out.print("Exam");
- System.out.print(e[i][E]);
- }
- /*
- * Calculating total grade
- * exam[0] = the number of exams
- * exam[2] = the weight of exam
- */
- eGrade = (eGrade / Double.parseDouble(exam[0]))
- * (Double.parseDouble(exam[2]) / 100);
- double qGrade = 0.0;
- // loop for each type of test exam scores
- // q[i] ==> finding the length of the array inside the
- // position of q[i]
- for (int Q = 0; Q < q[i].length; Q++) {
- if(q[i].length == 0) break;
- //score = the score of quiz entered
- double score = q[i][Q];
- // set grade in here.
- /*value of what the quiz is out of (3 100 20)
- * quiz is out of 100
- */
- double outOf = Double.parseDouble(quizzes[1]);
- double percentage = score / outOf;
- qGrade = qGrade + (percentage * 100);
- System.out.print("Quiz");
- System.out.print(q[i][Q]);
- }
- /*
- * Calculating total grade
- * quizzes[0] = the number of quizzes
- * quizzes[2] = the weight of quizzes
- */
- qGrade = (qGrade / Double.parseDouble(quizzes[0]))
- * (Double.parseDouble(quizzes[2]) / 100);
- double hGrade = 0.0;
- for (int H = 0; H < h[i].length; H++) {
- if(h[i].length == 0)break;
- //score = the score of hw entered
- double score = h[i][H];
- // set grade in here.
- /*value of what the hw is out of (3 100 20)
- * hw is out of 100
- */
- double outOf = Double.parseDouble(hw[1]);
- double percentage = score / outOf;
- hGrade = hGrade + (percentage * 100);
- System.out.print("Hwork");
- System.out.print(h[i][H]);
- }
- /*
- * Calculating total grade
- * hw[0] = the number of homework
- * hw[2] = the weight of homework
- */
- hGrade = (hGrade / Double.parseDouble(hw[0]))
- * (Double.parseDouble(hw[2]) / 100);
- //total grade
- totalGrade = eGrade + qGrade + hGrade;
- System.out.print("Grade");
- System.out.print(totalGrade);
- }
- break;
- case 3:
- System.out.println("Plot grade distribution: ");
- //gradeArray = new double[students[i]][totalGrade]; //array capable of storing the number of grades output
- break;
- case 4:
- System.out.println("Goodbye!");
- break outer;
- default:
- System.out
- .println("Please select of the the following option.");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement