C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs » Java basic programs

Program to verify answers of answer sheets of N students in Java

Here, we are implementing a java program that will read number of students their answers which they marked in answer sheets, answer key (correct answers) and finally calculate the result of all students.
Submitted by IncludeHelp, on December 24, 2017

Given number of students, their answers of answer sheet and answer keys and we have to calculate their result using java program.

Program

import java.util.Scanner;

public class ExArrayCalculateMarksByMatching 
{
	// declare array objects.
	char A[][],K[];
	int S[],n;

	void input()
	{
		// create object of scanner class.
		Scanner sc = new Scanner(System.in);

		// enter number of students
		System.out.print("Enter number of Students : ");
		n = sc.nextInt();

		if(n<4 || n>10)
		{
			// enter size for taking range.
			System.out.println("INPUT SIZE OUT OF RANGE");
			System.exit(0);
		}
		// Array to store the answers of students.
		A = new char[n][5]; 

		// Array to store answer key
		K = new char[5]; 

		// Array to store score of Students.
		S = new int[n]; 

		// enter answers row-wise.
		System.out.println("\n* Enter answer of each participant row-wise in a single line *\n");
		for(int i = 0; i<n; i++)
		{
			System.out.println("Participant "+(i+1)+" : ");
			for(int j=0; j<5; j++)
			{
				A[i][j] = sc.next().charAt(0);
			}
		}

		// enter correct answer.
		System.out.println("\nEnter Answer Key : ");
		for(int i = 0; i<5; i++)
		{
			K[i] = sc.next().charAt(0);
		}
	}

	// Function to calculate score Students.
	void CalcScore() 
	{
		for(int i = 0; i<n; i++)
		{
			S[i] = 0;
			for(int j=0; j<5; j++)
			{
				// matching the students answers with correct answer.
				if(A[i][j] == K[j]) 
				{
					S[i]++;
				}
			}
		}
	}

	// function to print score of students.
	void printScore()
	{
		int max = 0;
		System.out.println("\nSCORES : ");
		for(int i = 0; i<n; i++)
		{
			System.out.println("\tParticipant "+(i+1)+" = "+S[i]);
			if(S[i]>max)
			{
				// Storing the Highest Score
				max = S[i]; 
			}
		}
		System.out.println();

		System.out.println("\tHighest Score : "+max);

		System.out.println("\tHighest Scorers : ");

		// Printing all highest score
		for(int i = 0; i<n; i++) 
		{
			if(S[i] == max)
			{
				System.out.print("\t\t\tParticipant "+(i+1));
			}
		}
	}

	public static void main(String args[])
	{
		ExArrayCalculateMarksByMatching ob = new ExArrayCalculateMarksByMatching();
		ob.input();
		ob.CalcScore();
		ob.printScore();
	}
}

Output

Enter number of Students : 4

* Enter answer of each participant row-wise in a single line *

Participant 1 : 
A
A
B
D
D
Participant 2 : 
C
D
B
B
A
Participant 3 : 
C
D
A
B
A
Participant 4 : 
A
C
C
D
B

Enter Answer Key : 
A
C
C
B
D

SCORES : 
	Participant 1 = 2
	Participant 2 = 1
	Participant 3 = 1
	Participant 4 = 3

	Highest Score : 3
	Highest Scorers : 
			Participant 4


Liked this article? Do share with your friends :)


COMMENTS