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

TruthTableQuiz.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;

public class TruthTableQuiz extends JApplet {
  boolean isStandalone = false;

  int theSeed;
 
  Box topBox=new Box(BoxLayout.Y_AXIS);

  JPanel nlPanel = new JPanel();
  JLabel numLitsLabel=new JLabel("Literals:");
  String [] nl={"1","2","3","4","5"};
  JComboBox numLitsBox=new JComboBox(nl);

  JLabel diffLabel=new JLabel("Difficulty:");
  String [] diff=
     {"trivial","easy","medium","hard","very hard","insane","as if"};
  JComboBox diffBox=new JComboBox(diff);

  JPanel ButtonPanel = new JPanel();
  JButton MeTry = new JButton("Try One");           // for NORTH
  JButton CheckAnswer = new JButton("Check Answer");     // for NORTH

  JPanel CenterPanel = new JPanel();  // CENTER
  TruthTable theTable= new TruthTable();           // for CENTER

  JPanel Bottom=new JPanel(new FlowLayout()); // for SOUTH
  JTextArea Message=new JTextArea("",4,24);
  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception {
    this.setSize(new Dimension(400,450));

    MeTry.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        MeTry_actionPerformed(e); } });

    diffBox.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        diffBox_actionPerformed(e); } });

    numLitsBox.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        numLitsBox_actionPerformed(e); } });

    CheckAnswer.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        CheckAnswer_actionPerformed(e); } });

    ButtonPanel.add(MeTry, null);
    ButtonPanel.add(CheckAnswer);

    nlPanel.add(numLitsLabel);
    nlPanel.add(numLitsBox);
    nlPanel.add(diffLabel);
    nlPanel.add(diffBox);
    topBox.add(nlPanel);
    topBox.add(ButtonPanel);

    CenterPanel.add(theTable);
    Bottom.add(Message);

    this.getContentPane().add(topBox, BorderLayout.NORTH);
    this.getContentPane().add(CenterPanel, BorderLayout.CENTER);
    this.getContentPane().add(Bottom, BorderLayout.SOUTH);

    Message.setBorder(BorderFactory.createEtchedBorder());
    Message.setEditable(false);
    Message.setRows(2);
    Message.setLineWrap(true);
    Message.setWrapStyleWord(true);
       Message.setText("Enter your guesses in the last column.\n"+
            "Click 'Check My Answer' when you are done.");

    theTable.setVisible(false);
    CenterPanel.setBorder(BorderFactory.createTitledBorder(
       BorderFactory.createEtchedBorder(), "",TitledBorder.CENTER, 
       TitledBorder.ABOVE_TOP,new Font("Lucida Sans",Font.PLAIN,16)));
    CenterPanel.validate();
    CheckAnswer.setEnabled(false);
    FontFixer.setUIFont("Lucida Sans");
    SwingUtilities.updateComponentTreeUI(this);


  }

  void numLitsBox_actionPerformed(ActionEvent e) {
       CheckAnswer.setEnabled(false);
       MeTry.setEnabled(true);
       theTable.setVisible(false);
  }

  void diffBox_actionPerformed(ActionEvent e) {
       CheckAnswer.setEnabled(false);
       MeTry.setEnabled(true);
       theTable.setVisible(false);
       
  }

  void MeTry_actionPerformed(ActionEvent e) {
       theSeed=(int) (100000*Math.random());
       int numLits=1+numLitsBox.getSelectedIndex();
       int diff=diffBox.getSelectedIndex();
       if(diffBox.getSelectedItem().equals("as if"))
            diff+=3;
       
       CenterPanel.remove(theTable);
       
       theTable=new TruthTable();
       theTable.getRandomTable(theSeed,numLits,diff);
       CenterPanel.add(theTable);
       theTable.setVisible(true); 
       
       int width=(int) Math.min(theTable.getPreferredSize().getWidth(),
                 CenterPanel.getWidth());
       int height=(int) Math.min(theTable.getPreferredSize().getHeight(),
                 CenterPanel.getHeight()-40);
       theTable.setPreferredSize(new Dimension(width,height));
       Message.setText("Enter your guesses in the last column.\n"+
            "Click 'Check My Answer' when you are done.");
       CenterPanel.setBorder(BorderFactory.createTitledBorder(
          BorderFactory.createEtchedBorder(),
          "\u03A3 = "+theTable.getProp(),
          TitledBorder.CENTER, TitledBorder.ABOVE_TOP,
          new Font("Lucida Sans",Font.PLAIN,16),Color.blue));
       CenterPanel.validate();
       CheckAnswer.setEnabled(true);
       //MeTry.setEnabled(false); // so you can't just keep pinking new ones
       theTable.setVisible(true);
  }

  void CheckAnswer_actionPerformed(ActionEvent e) {
       int numCorrect=theTable.gradeAnswer();   
       Message.setText("You got "+numCorrect+" out of "+
                       theTable.tTable.getRowCount() +" correct answers.\n");
       Message.append("The correct answers are given above.\n");
       Message.append("You got the green ones correct.");
       CenterPanel.setBorder(BorderFactory.createTitledBorder(
          BorderFactory.createEtchedBorder(),
          "\u03A3 = "+theTable.getProp(),
          TitledBorder.CENTER, TitledBorder.ABOVE_TOP,
          new Font("Lucida Sans",Font.PLAIN,16)));
       CheckAnswer.setEnabled(false);
       MeTry.setEnabled(true);
       MeTry.setText("Try Again");
  }
}