Title: Gradebook/Fantasy Football
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Gradebook and Fantasy Football

In your textbook, there is an example called Gradebook. In this video, the Gradebook example is examined. This example uses 2-dimensional arrays and 1 dimensional arrays.

One of your Homework assignments is called "Fantasy Football" and it is similar to Gradebook. I would like you to try to write your Fantasy football assignment from scratch, but it is similar to the Gradebook example. In this video, the similarities are discussed.

The following is the Gradebook example from the textbook (slightly modified to add the main from GradebookDemo to Gradebook).




import java.util.Scanner;

public class GradeBook
{

    private int numberOfStudents; // Same as studentAverage.length.
    private int numberOfQuizzes; // Same as quizeAverage.length.

    private int[][] grade; //numberOfStudents rows and numberOfQuizzes columns.
    private double[] studentAverage;
    private double[] quizAverage;

    public static void main(String[] args)
    {
        GradeBook book = new GradeBook( );
        book.display( );
    }

    public GradeBook(int[][] a)
    {
        if (a.length == 0 || a[0].length == 0)
        {
            System.out.println("Empty grade records. Aborting.");
            System.exit(0);
        }

        numberOfStudents = a.length;
        numberOfQuizzes = a[0].length;
        fillGrade(a);
        fillStudentAverage( );
        fillQuizAverage( );
    }

    public GradeBook(GradeBook book)
    {
        numberOfStudents = book.numberOfStudents;
        numberOfQuizzes = book.numberOfQuizzes;
        fillGrade(book.grade);
        fillStudentAverage( );
        fillQuizAverage( );
    }

    public GradeBook( )
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter number of students:");
        numberOfStudents = keyboard.nextInt( );

        System.out.println("Enter number of quizzes:");
        numberOfQuizzes = keyboard.nextInt( );

        grade = new int[numberOfStudents][numberOfQuizzes];

        for (int studentNumber = 1;
                     studentNumber <= numberOfStudents; studentNumber++)
            for (int quizNumber = 1;
                            quizNumber <= numberOfQuizzes; quizNumber++)
            {
                System.out.println("Enter score for student number "
                                             + studentNumber);
                System.out.println("on quiz number " + quizNumber);
                grade[studentNumber - 1][quizNumber - 1] =
                                                 keyboard.nextInt( );
            }

        fillStudentAverage( );
        fillQuizAverage( );
    }

    private void fillGrade(int[][] a)
    {
        grade = new int[numberOfStudents][numberOfQuizzes];

        for (int studentNumber = 1;
                        studentNumber <= numberOfStudents; studentNumber++)
        {
            for (int quizNumber = 1;
                         quizNumber <= numberOfQuizzes; quizNumber++)
                grade[studentNumber][quizNumber] =
                                        a[studentNumber][quizNumber];
        }
    }

    /**
      Fills the array studentAverage using the data from the array grade.
    */
    private void fillStudentAverage( )
    {
        studentAverage = new double[numberOfStudents];

        for (int studentNumber = 1;
                        studentNumber <= numberOfStudents; studentNumber++)
        {//Process one studentNumber:
            double sum = 0;
            for (int quizNumber = 1;
                         quizNumber <= numberOfQuizzes; quizNumber++)
                sum = sum + grade[studentNumber - 1][quizNumber - 1];
            //sum contains the sum of the quiz scores for student number studentNumber.
            studentAverage[studentNumber - 1] = sum/numberOfQuizzes;
            //Average for student studentNumber is studentAverage[studentNumber - 1]
        }
    }

    /**
      Fills the array quizAverage using the data from the array grade.
    */
    private void fillQuizAverage( )
    {
        quizAverage = new double[numberOfQuizzes];

        for (int quizNumber = 1; quizNumber <= numberOfQuizzes; quizNumber++)
        {//Process one quiz (for all students):
            double sum = 0;
            for (int studentNumber = 1;
                          studentNumber <= numberOfStudents; studentNumber++)
                sum = sum + grade[studentNumber - 1][quizNumber - 1];
            //sum contains the sum of all student scores on quiz number quizNumber.
            quizAverage[quizNumber - 1] = sum/numberOfStudents;
            //Average for quiz quizNumber is the value of quizAverage[quizNumber - 1]
        }
    }

    public void display( )
    {
        for (int studentNumber = 1;
                    studentNumber <= numberOfStudents; studentNumber++)

        {//Display for one studentNumber:
            System.out.print("Student " + studentNumber + " Quizzes: ");
            for (int quizNumber = 1;
                             quizNumber <= numberOfQuizzes; quizNumber++)
              System.out.print(grade[studentNumber - 1][quizNumber - 1] + " ");
            System.out.println(" Ave = " + studentAverage[studentNumber - 1] );
        }

        System.out.println("Quiz averages: ");
        for (int quizNumber = 1; quizNumber <= numberOfQuizzes; quizNumber++)
            System.out.print("Quiz " + quizNumber
                            + " Ave = " + quizAverage[quizNumber - 1] + " ");
        System.out.println( );
    }
}



(15 points) Java Practice(JPHR):

This Practice does some similar things to the Gradebook example above (textbook example). It is structured differently. Your job is to place appropriate code wherever you see:
// ***** FILL IN CODE **************

After this code, I have also placed some sample output that should come out of your program if you enter in the same user inputs.


import java.util.Scanner;

public class MultiDimArrays {

    int[][] multiDimArr;
    Scanner keyboard = new Scanner(System.in);

    void fillArray()
    {
        System.out.println("Enter in the number of rows: ");
        int rows = keyboard.nextInt();
        System.out.println("Enter in the number of columns: ");
        int cols = keyboard.nextInt();

        // ***** FILL IN CODE **************
        // Create code to allocate memory for multiDimArr.


        for (int r=0; r < rows; r++)
        {
            for (int c=0; c < cols; c++)
            {
                System.out.println("Enter in the value for row="+r + ", col="+c);
                int value = keyboard.nextInt();
                // ****** FILL IN CODE ******
                // Store value into the proper place in multiDimArr

            }
        }
    }

    int averageOfCompleteArray()
    {
        int rows = multiDimArr.length;
        int cols = multiDimArr[0].length; // Assuming this is not a ragged array


        // ***** FILL IN CODE **************
        // Compute the average value in multiDimArr and return its value.

    }
    int[] averageRowValues()
    {

        int rows = multiDimArr.length;
        int cols = multiDimArr[0].length; // Assuming this is not a ragged array


        // ***** FILL IN CODE **************
        // Return an array containing an entry for each row.
        // Each entry will contain the average value of a specific row
    }

    int[] averageColumnValues()
    {

        int rows = multiDimArr.length;
        int cols = multiDimArr[0].length; // Assuming this is not a ragged array

        // ***** FILL IN CODE **************
        // Return an array containing an entry for each column.
        // Each entry will contain the average value of a specific column
    }

    void printArr(int[] arr)
    {
        System.out.println("***** printArr: *****");
        for (int i=0; i < arr.length; i++)
            System.out.println(arr[i]);

    }
    void printMultiDimenArr()
    {
        int[][] arr = multiDimArr;
        System.out.println("***** printMultiDimenArr: *****");

        int rows = multiDimArr.length;
        int cols = multiDimArr[0].length; // Assuming this is not a ragged array

        for (int r=0; r < rows; r++)
        {
            for (int c=0; c < cols; c++)
            {
                System.out.print(arr[r][c] +"\t");
            }
            System.out.println();
        }

    }

    public static void main(String[] args) {

        MultiDimArrays mda = new MultiDimArrays();
        mda.fillArray();
        mda.printMultiDimenArr();

        int average = mda.averageOfCompleteArray();
        System.out.println("Average of whole array = "+ average);

        int[] avgRowValues = mda.averageRowValues();
        System.out.println("Row Averages:");
        mda.printArr(avgRowValues);

        int[] avgColumnValues = mda.averageColumnValues();
        System.out.println("Column Averages:");
        mda.printArr(avgColumnValues);

    }

}



Sample output from a properly finished program:

Enter in the number of rows:
3
Enter in the number of columns:
4
Enter in the value for row=0, col=0
0
Enter in the value for row=0, col=1
1
Enter in the value for row=0, col=2
2
Enter in the value for row=0, col=3
3
Enter in the value for row=1, col=0
4
Enter in the value for row=1, col=1
5
Enter in the value for row=1, col=2
6
Enter in the value for row=1, col=3
7
Enter in the value for row=2, col=0
8
Enter in the value for row=2, col=1
9
Enter in the value for row=2, col=2
10
Enter in the value for row=2, col=3
11
***** printMultiDimenArr: *****
0	1	2	3	
4	5	6	7	
8	9	10	11	
Average of whole array = 5
Row Averages:
***** printArr: *****
1
5
9
Column Averages:
***** printArr: *****
4
5
6
7



Last Modified: December 6, 2014 2:55 PM