/** @author Scott Marshall @author University of Glasgow, MSc IT Project 2001 @author Building an OnLine Course in Computing Fundamentals */ package ukacgla_TruthTable; import ukacgla_ATT.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; /** Class contains methods to calculate the correct answer, to compare the student answer to the correct answer and to display the correct answer for a singl line. */ public class Ex01_Checker extends Checker{ private int ansM; //hold student answer for M, (output from gate 1) private int ansN; //hold student answer for N, (output from gate 2) private int ansO; //hold student answer for O, (output from gate 3) private int ans1; //holds correct answer for gate 1 private int ans2; //holds correct answer for gate 2 private int ans3; //holds correct answer for gate 3 private int cols; //keeps track of which truth table column is an input for which gate public Ex01_Checker(TableUI t,TTRandom r){ super(t,r); table = t; random = r; } /** calcAnswer() calculates the correct answer for a single row Each random.getRandomN() gets the type of gate at Gates 1, 2 and 3. Each gate uses a switch to determine the type of gate, gets the relevent inputs to each gate as a string, concatenates the two input string and uses the concatenated string as an input argument to the correct truth table for each gate. The result is stored as an int in the ans1,2,3 variables for later comparison. */ protected void calcAnswer(int row){ //initialised here with default values. String inputOne; String inputTwo; String inputThree; String inputFour; String inputFive; String inputSix; Integer temp1; Integer temp2; String gateOneInput; String gateTwoInput; String gateThreeInput; cols = 1; //tracks columns to be read for input. Begins at 1 to avoid the label line //gate 1 switch (random.getRandom1()) { case 0: inputOne = table.getCellValue(row,cols); inputTwo = table.getCellValue(row,cols+1); cols+=2; gateOneInput = inputOne+inputTwo ; ans1 = chkAndTable(gateOneInput); break;//And case 1: inputOne = table.getCellValue(row,cols); inputTwo = table.getCellValue(row,cols+1); cols+=2; gateOneInput = inputOne+inputTwo ; ans1 = chkOrTable(gateOneInput);break;//Or case 2: inputOne = table.getCellValue(row,cols); cols++; ans1 = chkInvTable(inputOne);break;//Inverter case 3: inputOne = table.getCellValue(row,cols); inputTwo = table.getCellValue(row,cols+1); cols+=2; gateOneInput = inputOne+inputTwo ; ans1 = chkNAndTable(gateOneInput);break;//Nand case 4: inputOne = table.getCellValue(row,cols); inputTwo = table.getCellValue(row,cols+1); cols+=2; gateOneInput = inputOne+inputTwo ; ans1 = chkXOrTable(gateOneInput);break;//Xor } //gate 2 switch (random.getRandom2()) { case 0: inputThree = table.getCellValue(row,cols); inputFour = table.getCellValue(row,cols+1); cols+=2; gateTwoInput = inputThree+inputFour ; ans2 = chkAndTable(gateTwoInput); break;//And case 1: inputThree = table.getCellValue(row,cols); inputFour = table.getCellValue(row,cols+1); cols+=2; gateTwoInput = inputThree+inputFour ; ans2 = chkOrTable(gateTwoInput);break;//Or case 2: inputThree = table.getCellValue(row,cols); cols++; ans2 = chkInvTable(inputThree);break;//Inverter case 3: inputThree = table.getCellValue(row,cols); inputFour = table.getCellValue(row,cols+1); cols+=2; gateTwoInput = inputThree+inputFour ; ans2 = chkNAndTable(gateTwoInput);break;//Nand case 4: inputThree = table.getCellValue(row,cols); inputFour = table.getCellValue(row,cols+1); cols+=2; gateTwoInput = inputThree+inputFour ; ans2 = chkXOrTable(gateTwoInput);break;//Xor } //gate 3 switch (random.getRandom3()) { case 0: temp1 = new Integer(ans1); inputFive = temp1.toString(); temp2 = new Integer(ans2); inputSix = temp2.toString(); gateThreeInput = inputFive+inputSix ; ans3 = chkAndTable(gateThreeInput);break;//And case 1: temp1 = new Integer(ans1); inputFive = temp1.toString(); temp2 = new Integer(ans2); inputSix = temp2.toString(); gateThreeInput = inputFive+inputSix ; ans3 = chkOrTable(gateThreeInput);break;//Or case 2: temp1 = new Integer(ans1); inputFive = temp1.toString(); temp2 = new Integer(ans2); inputSix = temp2.toString(); gateThreeInput = inputFive+inputSix ; ans3 = chkXOrTable(gateThreeInput);break;//Xor - deliberate breaking of randomness case 3: temp1 = new Integer(ans1); inputFive = temp1.toString(); temp2 = new Integer(ans2); inputSix = temp2.toString(); gateThreeInput = inputFive+inputSix ; ans3 = chkNAndTable(gateThreeInput);break;//Nand case 4: temp1 = new Integer(ans1); inputFive = temp1.toString(); temp2 = new Integer(ans2); inputSix = temp2.toString(); gateThreeInput = inputFive+inputSix ; ans3 = chkXOrTable(gateThreeInput);break;//Xor } } //Truth table - And gate private int chkAndTable(String input){ int ans = -1; if(input.equals("00")) ans = 0; if(input.equals("01")) ans = 0; if(input.equals("10")) ans = 0; if(input.equals("11")) ans = 1; return ans; } //Truth table - Or gate private int chkOrTable(String input){ int ans = -1; if(input.equals("00")) ans = 0; if(input.equals("01")) ans = 1; if(input.equals("10")) ans = 1; if(input.equals("11")) ans = 1; return ans; } //Truth table - Inv gate private int chkInvTable(String input){ int ans = -1; if(input.equals("0")) ans = 1; if(input.equals("1")) ans = 0; return ans; } //Truth table - NAnd gate private int chkNAndTable(String input){ int ans = -1; if(input.equals("00")) ans = 1; if(input.equals("01")) ans = 1; if(input.equals("10")) ans = 1; if(input.equals("11")) ans = 0; return ans; } //Truth table - XOr gate private int chkXOrTable(String input){ int ans = -1; if(input.equals("00")) ans = 0; if(input.equals("01")) ans = 1; if(input.equals("10")) ans = 1; if(input.equals("11")) ans = 0; return ans; } /** checkAnswer() will mark the answer being input for a single line. calcAnswer() calculates the correct answer for the line, and stores the result in the ans1, ans2 and ans3 variables The student answers are read from the table and stored in ansM,ansN,ansO variables. The results for the line are displayed. The numbers of the lines containing errors are converted to string and added to the wrongLines vector. */ protected void checkAnswer(int row){ calcAnswer(row); ansM = new Integer(table.getCellValue(row,cols)).intValue(); ansN = new Integer(table.getCellValue(row,cols+1)).intValue(); ansO = new Integer(table.getCellValue(row,cols+2)).intValue(); Integer iRow = new Integer(row+1); if (ansM == ans1 && ansN == ans2 && ansO ==ans3){ //do nothing } else { table.setTxtArea("Error at Line " + (row+1)); wrongLines.add(iRow.toString()); } } /** revealAnswer will display the correct answer for a single row, after calcAnswer has calculated the correct answer for the row.. */ protected void revealAnswer(int row){ calcAnswer(row); table.setTxtArea("Line " + (row+1) + ". Correct Answer is = " + ans1 + " " + ans2 + " " + ans3); } }