Program to Verify Answers of Answer Sheets of N Students in Java

You are currently viewing Program to Verify Answers of Answer Sheets of N Students in Java

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

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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply