Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Alvin Bello
- * Block 6
- * Grade Book Lab
- */
- package main;
- import java.text.DecimalFormat;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Main {
- static Scanner scan = new Scanner(System.in);
- String[][] grades;
- int students, tests, avg;
- DecimalFormat dec;
- public static void main(String args[]) {
- int students = 0;
- while(true) {
- try {
- while(students < 1 || students > 30) {
- System.out.println("How many students do you want? You must have at least 1 and at most 30.");
- students = scan.nextInt();
- }
- } catch (InputMismatchException e) {
- System.out.println("Students are integers -_-");
- scan.nextLine();
- continue;
- } catch (Exception e) {
- System.out.println("An unexpected exception occured, lets try again.");
- scan.nextLine();
- continue;
- }
- break;
- }
- while(true) {
- try {
- System.out.println("How many tests will you have? If you enter a negative amount you will have 0");
- int tests = scan.nextInt();
- tests = (tests < 0) ? 0 : tests;
- scan.nextLine();
- Main run = new Main(students, tests);
- } catch (InputMismatchException e) {
- System.out.println("You must have an integral amount of tests -_-");
- scan.nextLine();
- continue;
- } catch (Exception e) {
- System.out.println("An unexpected exception occured, lets try again.");
- scan.nextLine();
- continue;
- }
- break;
- }
- }
- public Main(int students, int tests) {
- this.students = students;
- this.tests = tests;
- grades = new String[students+1][tests+3];
- grades[0][0] = "";
- grades[0][grades[0].length-2] = "Average";
- grades[0][grades[0].length-1] = "Grade";
- dec = new DecimalFormat("0.##");
- while(true) {
- try {
- System.out.println("Now let's enter the names of your students");
- for(int i = 1; i < grades.length; i++) {
- System.out.println("Enter the name of student " + i);
- grades[i][0] = scan.nextLine();
- }
- } catch (Exception | Error e) {
- System.out.println("You messed up");
- scan.nextLine();
- continue;
- }
- break;
- }
- while(true) {
- try {
- System.out.println("Now let's enter the names of your tests.\n");
- for(int i = 1; i < grades[0].length-2; i++) {
- System.out.println("Enter the name of test " + i);
- grades[0][i] = scan.nextLine();
- }
- } catch (Exception | Error e) {
- System.out.println("You messed up");
- scan.nextLine();
- continue;
- }
- break;
- }
- while(true) {
- try {
- System.out.println("Now let's enter the scores of the students.\n");
- for(int i = 1; i < grades.length; i++) {
- for(int j = 1; j < grades[i].length-2; j++) {
- System.out.println("Enter " + grades[i][0] + "\'s grade for test " + grades[0][j]);
- double score = scan.nextDouble();
- score = score<0?0:score;
- grades[i][j] = dec.format(score);
- }
- }
- } catch (InputMismatchException e) {
- System.out.println("I told you to enter numbers! Now we have to start over.");
- scan.nextLine();
- continue;
- } catch (Exception e) {
- System.out.println("An unexpected exception occured, lets try again.");
- scan.nextLine();
- continue;
- }
- break;
- }
- for(int i = 1; i < grades.length; i++) {
- for(int j = 1; j < grades[i].length-2; j++)
- avg += Double.parseDouble(grades[i][j]);
- avg /= grades[i].length-3;
- grades[i][grades[i].length-2] = dec.format(avg);
- if(avg >= 89.5) grades[i][grades[i].length-1] = "A";
- else if(avg >= 79.5) grades[i][grades[i].length-1] = "B";
- else if(avg >= 69.5) grades[i][grades[i].length-1] = "C";
- else if(avg >= 59.5) grades[i][grades[i].length-1] = "D";
- else if(avg < 59.5) grades[i][grades[i].length-1] = "F";
- }
- System.out.println("Here is your class:\n");
- for(int i = 0; i < grades.length; i++) {
- for(int j = 0; j < grades[i].length; j++)
- System.out.print(grades[i][j] + "\t");
- System.out.println();
- }
- System.out.println("Here are your test averages:\n");
- double testAvg = 0;
- for(int j = 1; j < grades[0].length-2; j++) {
- for(int i = 1; i < grades.length; i++)
- testAvg += Double.parseDouble(grades[i][j]);
- testAvg /= grades.length - 1;
- System.out.println("The average for " + grades[0][j] + " is " + testAvg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement