# Posted By

eiger824 on 02/08/13

# Statistics

Viewed 664 times
Favorited by 0 user(s)

# Test-Type Exam Correction With Java

/ Published in: Java

A simple way of correcting tests with Java. In this piece of code I just defined two possible models or answers, they're all random, but the only thing needed is to update them with valid answers. The length of the test? I just did it for a 10-question test, but it can naturally be extended to the number of answers the teacher may want.

Copy this code and paste it in your HTML
`package com.santi; import java.io.BufferedReader;import java.io.InputStreamReader; public class Main { 	static char [] calif;	static char [] answers = {'a','b','b','c','a','a','d','d','c','b'}; //example1 of right answers	static char [] answers2 = {'b','b','c','a','a','b','d','b','a','c'};//example2 of right answers	int result;	static int cont;	static String cal=null;	public static void main(String[] args) {		int option;		String opt=getInput("Which test model are you correcting? 1-A; 2-B: ");		option=Integer.parseInt(opt);		switch(option){		case 1:	cont=0;				cal = checkForTypedLength(cal);				calif=cal.toCharArray();				cont = updateCounters(cont, cal);				System.out.println("Result: "+cont+"/10");				break;		case 2:			cont=0;			cal = checkForTypedLength(cal);			calif=cal.toCharArray();			cont = updateCounters2(cont, cal);			System.out.println("Result: "+cont+"/10");			break;		} 	}	public static int updateCounters(int cont, String cal) {		for(int i=0;i<cal.length();i++){			if(calif[i]==answers[i]){				cont++;			}			else if(calif[i]=='0'){ 			}			else if(calif[i]!=answers[i]){				cont--;			}		}		return cont;	}	public static int updateCounters2(int cont, String cal) {		for(int i=0;i<cal.length();i++){			if(calif[i]==answers2[i]){				cont++;			}			else if(calif[i]=='0'){ 			}			else if(calif[i]!=answers2[i]){				cont--;			}		}		return cont;	}	public static String checkForTypedLength(String cal) {		boolean go=true;		while(go==true){			cal=getInput("Enter answers (i.e.: acbbaabcddc; 0 if unanswered): ");			if(cal.length()!=10){				System.out.println("There are only 10 questions in the test!" +						" Please enter them correctly.");			}			else{ 				go=false;		} 		}		return cal;	}	private static String getInput(String prompt) {		BufferedReader stdin = new BufferedReader(				new InputStreamReader(System.in)); 		System.out.print(prompt);		System.out.flush(); 		try {			return stdin.readLine();		} catch (Exception e) {			return "Error: " + e.getMessage();		}	} }`