Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

TruthTableQuiz


Description

Run it

BinaryTree.java
Connectives.java
FontFixer.java
Proposition.java
TreeNode.java
TruthTable.java
TruthTableQuiz.java

TruthTable.java

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

class TruthTable extends JScrollPane {
   public TruthModel TTModel;
   public JTable tTable;
   String []Guesses={"","T","F"};
   JComboBox guessBox=new JComboBox(Guesses);

   public TruthTable() { 
          initTable("A"); }
   public TruthTable(String S) { 
          initTable(S); }

  // difficulty: 0 is easy, 1 is medium, etc.
   public void getRandomTable(int seed,int terms,int difficulty) {
        Proposition rand=new Proposition();
        rand.randomProp(seed,terms,difficulty);
        initTable(rand.getProp());
   }
   public void initTable(String S) {
        TTModel=new TruthModel(S);
        tTable=new JTable(TTModel) {
             public TableCellRenderer getCellRenderer(int row,int column) {
                    if(column==tTable.getColumnCount()-1)  {
                      return new AnswerRenderer();
                    }
                    else  {
                       DefaultTableCellRenderer GRend=
                                new DefaultTableCellRenderer();
                       GRend.setBackground(new Color(150,150,250));
                       return GRend;
                   }
             }
        };
        tTable.getTableHeader().setReorderingAllowed(false);
        tTable.setCellEditor(new DefaultCellEditor(guessBox));
        tTable.setSelectionMode(0); // 0==single row selection only.
                                    // stupid JAVA.

        this.setViewportView(tTable);
        int width=initColumnStyles();
        int height=(tTable.getRowCount()+1)*tTable.getRowHeight()+4;
        this.setPreferredSize(new Dimension(width,height));
        this.validate();
   }

   public int gradeAnswer() {
       return TTModel.gradeAnswer();
   }
   public void giveAnswer() {
       TTModel.giveAnswer();
   }
   public String getProp() {
        return TTModel.getProp();
   }

private int initColumnStyles() {
        TableColumn column=null;
        int totalSize=0;
        int i=0;
        for (i = 0; i < TTModel.getColumnCount()-1; i++) {
            column=tTable.getColumnModel().getColumn(i);
            column.setWidth(20);
            totalSize+=20;
        }
        column=tTable.getColumnModel().getColumn(i);
        column.setCellEditor(new DefaultCellEditor(guessBox));
        column.sizeWidthToFit();
        totalSize+=column.getWidth();
        return totalSize;
    }

   class AnswerRenderer extends JLabel implements TableCellRenderer {
        public AnswerRenderer() {
            super();
        }
        public void setValue(Object value) {
            super.setText((String)value);
        }
        public Component getTableCellRendererComponent(
                                JTable table, Object value,
                                boolean isSelected, boolean hasFocus,
                                int row, int column) {
            
            DefaultTableCellRenderer GRend=new DefaultTableCellRenderer();
            GRend.setText((String) value);
            if(TTModel.solve==false) 
                  GRend.setBackground(new Color(200,200,250));
            else if(TTModel.isCorrect(row))
                  GRend.setBackground(new Color(150,250,150));
            else  GRend.setBackground(new Color(250,150,150)); 
            return GRend;
        }
    }
}
//---------------------------------------------------------------------
class TruthModel extends AbstractTableModel {
   Proposition prop;
   boolean solve;
   String[] tHead;
   String aHead="\u03A3";
   Boolean[][] theTable;
   Object[][] theGuess;
   TruthModel(String S) {
       initTable(S);
   }

   public void initTable(String S) {
      solve=false;
      prop=new Proposition(S);
      tHead=prop.getTruthTableTitle();
      theTable=prop.getAllTruthAssignments();
      boolean [] theAnswer=prop.getAllTruthValues();
      theGuess=new Object[theAnswer.length][2];
      for(int i=0;i<theGuess.length;i++)  {
             theGuess[i][0]=theAnswer[i]==true?"T":"F";
             theGuess[i][1]=" ";
      }
      fireTableStructureChanged();
   }
   public void giveAnswer() {
       for(int i=0;i<getRowCount();i++) { theGuess[i][1]=theGuess[i][0]; }
       solve=true;
       fireTableStructureChanged();
   }
   public int gradeAnswer() {
       int numCorrect=0;
       for(int i=0;i<getRowCount();i++) {
          if(theGuess[i][0]==theGuess[i][1]) numCorrect++;
       }
       solve=true;
       fireTableStructureChanged();
       return numCorrect;
   }
   public boolean isCorrect(int i) { return theGuess[i][0]==theGuess[i][1]; }
   public String getProp() { return prop.getProp(); }
   public int getColumnCount() { return tHead.length+1; }
   public int getRowCount() { return theTable.length; }
   public String getColumnName(int col) {
       if(col<tHead.length) return tHead[col];
       else                 return aHead;
   }
   public Object getValueAt(int row, int col) {
       if(col<tHead.length) {
           if( (theTable[row][col]).equals(Boolean.TRUE) )
                return "T";
           else return "F";
       }
      else if(col==tHead.length && solve==false) 
           return theGuess[row][1];
      else   // col==tHead.length && showAnwer=true);
           return theGuess[row][0];
   }
   public Class getColumnClass(int c) {
       return getValueAt(0, c).getClass();
   }
   public boolean isCellEditable(int row, int col) {
       if (col==tHead.length && solve==false) return true; 
       else         return false;
   }
   public void setValueAt(Object value, int row, int col) {
           if(col==tHead.length) { theGuess[row][1]=value; }
   }
};