Test-Type Exam Correction With Java


/ Published in: Java
Save to your folder(s)

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
  1. package com.santi;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class Main {
  7.  
  8. static char [] calif;
  9. static char [] answers = {'a','b','b','c','a','a','d','d','c','b'}; //example1 of right answers
  10. static char [] answers2 = {'b','b','c','a','a','b','d','b','a','c'};//example2 of right answers
  11. int result;
  12. static int cont;
  13. static String cal=null;
  14. public static void main(String[] args) {
  15. int option;
  16. String opt=getInput("Which test model are you correcting? 1-A; 2-B: ");
  17. option=Integer.parseInt(opt);
  18. switch(option){
  19. case 1: cont=0;
  20. cal = checkForTypedLength(cal);
  21. calif=cal.toCharArray();
  22. cont = updateCounters(cont, cal);
  23. System.out.println("Result: "+cont+"/10");
  24. break;
  25. case 2:
  26. cont=0;
  27. cal = checkForTypedLength(cal);
  28. calif=cal.toCharArray();
  29. cont = updateCounters2(cont, cal);
  30. System.out.println("Result: "+cont+"/10");
  31. break;
  32. }
  33.  
  34. }
  35. public static int updateCounters(int cont, String cal) {
  36. for(int i=0;i<cal.length();i++){
  37. if(calif[i]==answers[i]){
  38. cont++;
  39. }
  40. else if(calif[i]=='0'){
  41.  
  42. }
  43. else if(calif[i]!=answers[i]){
  44. cont--;
  45. }
  46. }
  47. return cont;
  48. }
  49. public static int updateCounters2(int cont, String cal) {
  50. for(int i=0;i<cal.length();i++){
  51. if(calif[i]==answers2[i]){
  52. cont++;
  53. }
  54. else if(calif[i]=='0'){
  55.  
  56. }
  57. else if(calif[i]!=answers2[i]){
  58. cont--;
  59. }
  60. }
  61. return cont;
  62. }
  63. public static String checkForTypedLength(String cal) {
  64. boolean go=true;
  65. while(go==true){
  66. cal=getInput("Enter answers (i.e.: acbbaabcddc; 0 if unanswered): ");
  67. if(cal.length()!=10){
  68. System.out.println("There are only 10 questions in the test!" +
  69. " Please enter them correctly.");
  70. }
  71. else{
  72.  
  73. go=false;
  74. }
  75.  
  76. }
  77. return cal;
  78. }
  79. private static String getInput(String prompt) {
  80.  
  81. System.out.print(prompt);
  82. System.out.flush();
  83.  
  84. try {
  85. return stdin.readLine();
  86. } catch (Exception e) {
  87. return "Error: " + e.getMessage();
  88. }
  89. }
  90.  
  91. }

URL: http://programmingeiger824.blogspot.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.