Presentation is loading. Please wait.

Presentation is loading. Please wait.

Case Study 2 – Marking a Multiple-choice Test

Similar presentations


Presentation on theme: "Case Study 2 – Marking a Multiple-choice Test"— Presentation transcript:

1 Case Study 2 – Marking a Multiple-choice Test
In this case study, we learn: How to design a solution for a given problem How to use a two-dimensional array to represent a collection of multiple-choice test answers for a list of students How to process the two-dimensional array How to write, compile, run and test a program to implement the designed solution. 1 1 1

2 Problem Specification
The problem is to represent a collection of test answers for eight students, with each student having answers to ten questions , compare each set of test answers with the set of key answers, and count and print the number of correct answers. 2 2 2 2 2 2 2 2

3 How Would We Do it - Design?
Top-level design 1. Declare, create and initialise an array, answers[8] [10] to represent the test answers 2. Declare, create and initialise an array, keys[10], to represent the key answers 3. Count and print out the number of correct answers of each student 3 3 3 3 3 3 3

4 How Would We Do it - Design?
Final design in pseudo code 1. Declare, create and initialise an array, answers[8][10], to represent the test answers 2. Declare, create and initialise an array, keys[10], to represent the correct answers 3.1. For i = 0 to number of rows 3.2. Set count to 0 3.3. For j = 0 to number of answers - 1 3.4. If answer[i][j] = key[j] then 3.5. Increment count by 1 3.6. Print out student j + count 4 4 4 4 4 4 4 4

5 Implementation public class GradeExam {
public static void main(String[] args) { // Declare, create and initialise an array to represent answers char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}}; // Declare, create and initialise an array to represent correct answers char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'}; // Count correct answers of each students for (int i = 0; i < answers.length; i++) { // Count correct answers of an individual student int correctCount = 0; for (int j = 0; j < answers[i].length; j++) { if (answers[i][j] == keys[j]) correctCount++; } System.out.println("Student " + i + "'s correct count is " + correctCount); 5 5 5 5 5 5 5 5 5 5 5 5 5

6 Two-dimensional Array Basics
Two subscripts are used in a two-dimensional array, one for the row and the other for the column, with the index for each subscript of int type starting from 0. 6 6 6 6 6 6 6 6 6

7 Processing Two-dimensional Arrays
Suppose an array, matrix, is created as follows: int[][] matrix = new int[4][4]; Obtaining the lengths of two dimensional array: A two-dimensional array is a one-dimensional array in which each element is also a one-dimensional array. The matrix is a one-dimensional array of 4 elements, matrix[0], matrix[1], matrix[2], matrix[3], each of which is a one-dimensional array of 4 integers. The method length returns the length of a one-dimensional array. For example matrix.length returns 4, and each of matrix[0].length, matrix[1].length, matrix[2].length, matrix[3].length returns 4. 7 7 7 7 7 7 7 7 7 7 7

8 Processing Two-dimensional Arrays
Initializing arrays with user input values: java.util.Scanner input = new Scanner(System.in); System.out.println("Enter " + matrix.length + ” rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextInt(); } 8 8 8 8 8 8 8 8 8 8 8

9 Processing Two-dimensional Arrays
Printing arrays: for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column] + " "); } System.out.println(); 9 9 9 9 9 9 9 9 9 9 9 9

10 Processing Two-dimensional Arrays
Summing all elements: int total = 0; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { total += matrix[row][column]; } 10 10 10 10 10 10 10 10 10 10 10 10 10

11 Processing Two-dimensional Arrays
Finding the row with the largest sum: int maxRow = 0; int indexOfMaxRow = 0; // Set sum of the first row to maxRow for (int column = 0; column < matrix[0].length; column++) { maxRow += matrix[0][column]; } for (int row = 1; row < matrix.length; row++) { int totalOfThisRow = 0; for (int column = 0; column < matrix[row].length; column++) totalOfThisRow += matrix[row][column]; if (totalOfThisRow > maxRow) { maxRow = totalOfThisRow; indexOfMaxRow = row; System.out.println("Row " + indexOfMaxRow + " has the maximum sum of " + maxRow); 11 11 11 11 11 11 11 11 11 11 11 11 11 11

12 Processing Two-dimensional Arrays
Summing elements by column: for (int column = 0; column < matrix[0].length; column++) { int total = 0; for (int row = 0; row < matrix.length; row++) total += matrix[row][column]; System.out.println("Sum for column " + column + " is “ + total); } How to sum elements by row? 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12


Download ppt "Case Study 2 – Marking a Multiple-choice Test"

Similar presentations


Ads by Google