SimpleFlashCards (SFC) is just as the name describes it. SFC allows the user to select one of four subjects (addition, subtraction, multiplication, or division) and enter a number of questions to answer. The user is then prompted with "flash cards" which are actually windows with special flash card panels that contain questions to answer. The user enters what they think is the correct answer in the box, and are told immediatly whether it is correct or not. After all questions are answered, the user is told how many questions they got right out of how many asked and are returned to the main screen.
SFC's content is around 4th grade level as none of the numbers randomly selected for the flash cards are over 12 (including addition and subtraction). All numbers for the division also divide evenly, which I accomplished by making a multiplication table and pulling numbers from that. The code is a little long, so you may want to download and read it instead of just reading the posting here if you are interested. I will post the code here however, just for completeness.
FlashCardGUI.java: Contains all code for the creation of the "flash cards"
package simpleflashcards;
import javax.swing.*;
import java.awt.*;
public class FlashCardGUI extends JFrame
{
JPanel mainPanel = new JPanel();
FlashPanel card;
public FlashCardGUI(int firstNumber, int secondNumber, String operand)
{
super("Flash Card");
setSize(100, 200);
this.setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
card = new FlashPanel(firstNumber, secondNumber, operand);
add(card);
setVisible(true);
}
public void setAnswer(int answer)
{
card.setAnswer(answer);
card.repaint();
}
}
FlashEngine.java: Contains all code for the generation of problems
package simpleflashcards;
import java.util.*;
public class FlashEngine
{
int numberCorrect;
int totalNumber;
int randNumber1;
int randNumber2;
int[][] multTable = new int[12][12];
int answer;
public FlashEngine()
{
numberCorrect = 0;
totalNumber = 0;
for(int j = 0; j <= 11; j++)
for(int i = 0; i <= 11; i++)
multTable[j][i] = (j+1) * (i+1);
}
public void newProblem(String operand)
{
if (operand.equals("+") || operand.equals("-") || operand.equals("*"))
{
randNumber1 = (int)(Math.random() * 12) + 1;
randNumber2 = (int)(Math.random() * 12) + 1;
}
else if (operand.equals("/"))
{
randNumber2 = (int)(Math.random() * 12) + 1;
randNumber1 = multTable[randNumber2-1][(int)(Math.random() *12)];
}
if (operand.equals("+"))
answer = randNumber1 + randNumber2;
else if (operand.equals("-"))
answer = randNumber1 - randNumber2;
else if (operand.equals("*"))
answer = randNumber1 * randNumber2;
else if (operand.equals("/"))
answer = randNumber1 / randNumber2;
}
public int[] getNumbers()
{
return new int[]{randNumber1, randNumber2};
}
public int getAnswer()
{
return answer;
}
public boolean problemRight(int answer)
{
if (this.answer == answer)
{
numberCorrect++;
totalNumber++;
return true;
}
totalNumber++;
return false;
}
public int getCorrect()
{
return numberCorrect;
}
public int getTotal()
{
return totalNumber;
}
public void reset()
{
numberCorrect = 0;
totalNumber = 0;
}
}
FlashPanel.java: The panel that the "flash cards" reside in
package simpleflashcards;
import javax.swing.*;
import java.awt.*;
public class FlashPanel extends JPanel
{
int firstNumber;
int secondNumber;
int answer;
boolean answerSet;
String operand;
public FlashPanel(int firstNumber, int secondNumber, String operand)
{
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
if (operand == "+" || operand == "-" || operand == "*" || operand == "/")
this.operand = operand;
this.answerSet = false;
}
public void setAnswer(int answer)
{
this.answer = answer;
answerSet = true;
}
public int calcOffset(int number)
{
int digitOffset = 0;
for(int i = 0; i < (number + "").length(); i++)
digitOffset += 24;
return digitOffset;
}
public void paintComponent(Graphics comp)
{
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setFont(new Font("Lucida Console", Font.PLAIN, 36));
comp2D.drawString(firstNumber + "", this.getBounds().width - calcOffset(firstNumber), 50);
comp2D.drawString(secondNumber + "", this.getBounds().width - calcOffset(secondNumber), 100);
comp2D.drawString(operand, 15, 100);
if (answerSet)
comp2D.drawString(answer + "", this.getBounds().width - calcOffset(answer), 150);
}
}
SimpleFlashCardsEvent: Handles events from the GUI
package simpleflashcards;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleFlashCardsEvent implements ActionListener, ItemListener
{
SimpleFlashCardsGUI currentGui;
FlashEngine currentEngine = new FlashEngine();
int problemNumber;
String operand;
public SimpleFlashCardsEvent(SimpleFlashCardsGUI currentGui)
{
this.currentGui = currentGui;
operand = "+";
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == currentGui.start)
{
currentEngine.reset();
try
{
problemNumber = Integer.parseInt(currentGui.probInput.getText());
} catch (Exception e) {
problemNumber = 10;
JOptionPane.showMessageDialog(currentGui, "Bad entry... 10 questions will be asked");
} finally {
if (problemNumber <= 0)
{
JOptionPane.showMessageDialog(currentGui, "Questions not greater than zero... \nNo questions will be asked");
return;
}
}
currentGui.helpMenu.setEnabled(false);
currentGui.add.setEnabled(false);
currentGui.sub.setEnabled(false);
currentGui.mult.setEnabled(false);
currentGui.div.setEnabled(false);
currentGui.start.setEnabled(false);
for(int i = 0; i < problemNumber; i++)
{
currentEngine.newProblem(operand);
int[] numbers = currentEngine.getNumbers();
FlashCardGUI check = new FlashCardGUI(numbers[0], numbers[1], operand);
try
{
int answer = Integer.parseInt(JOptionPane.showInputDialog("Input answer for current card: "));
check.setAnswer(currentEngine.getAnswer());
if (currentEngine.problemRight(answer))
JOptionPane.showMessageDialog(currentGui, "Correct");
else
JOptionPane.showMessageDialog(currentGui, "Incorrect");
} catch (Exception e)
{
JOptionPane.showMessageDialog(currentGui, "Incorrect");
}
check.dispose();
}
JOptionPane.showMessageDialog(currentGui, "Questions Correct: " + currentEngine.getCorrect() + "\nTotal Questions: " + currentEngine.getTotal());
currentGui.helpMenu.setEnabled(true);
currentGui.add.setEnabled(true);
currentGui.sub.setEnabled(true);
currentGui.mult.setEnabled(true);
currentGui.div.setEnabled(true);
currentGui.start.setEnabled(true);
}
else if (evt.getSource() == currentGui.help)
{
try
{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "help.pdf");
} catch(Exception e){
JOptionPane.showMessageDialog(currentGui, "Adobe Reader must be installed to read this document");
}
}
else if (evt.getSource() == currentGui.about)
{
JOptionPane.showMessageDialog(currentGui, "Simple Flash Cards\nCopyright 2009\nAdam Weidner\nsimplecode.webs.com");
}
}
public void itemStateChanged(ItemEvent evt)
{
if (evt.getSource() == currentGui.add)
operand = "+";
else if (evt.getSource() == currentGui.sub)
operand = "-";
else if (evt.getSource() == currentGui.mult)
operand = "*";
else if (evt.getSource() == currentGui.div)
operand = "/";
}
}
SimpleFlashCardsGUI.java: Shows the main GUI for the program
package simpleflashcards;
import javax.swing.*;
import java.awt.*;
public class SimpleFlashCardsGUI extends JFrame
{
SimpleFlashCardsEvent evtListener = new SimpleFlashCardsEvent(this);
JMenuBar flashCardsBar = new JMenuBar();
JMenu helpMenu = new JMenu("Help");
JMenuItem help = new JMenuItem("Help");
JMenuItem about = new JMenuItem("About");
JPanel checkPanel = new JPanel();
ButtonGroup cardType = new ButtonGroup();
JCheckBox add = new JCheckBox("Addition", true);
JCheckBox sub = new JCheckBox("Subtraction", false);
JCheckBox mult = new JCheckBox("Multiplication", false);
JCheckBox div = new JCheckBox("Division", false);
JPanel inputPanel = new JPanel();
JLabel probNumbers = new JLabel("Input number of problems: ");
JTextField probInput = new JTextField("10", 10);
JButton start = new JButton("Start");
public SimpleFlashCardsGUI()
{
super("SimpleFlashCards");
setSize(400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
help.addActionListener(evtListener);
about.addActionListener(evtListener);
add.addItemListener(evtListener);
sub.addItemListener(evtListener);
mult.addItemListener(evtListener);
div.addItemListener(evtListener);
start.addActionListener(evtListener);
setLayout(new BorderLayout());
helpMenu.add(help);
helpMenu.add(about);
flashCardsBar.add(helpMenu);
cardType.add(add);
cardType.add(sub);
cardType.add(mult);
cardType.add(div);
checkPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
checkPanel.add(add);
checkPanel.add(sub);
checkPanel.add(mult);
checkPanel.add(div);
inputPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
inputPanel.add(probNumbers);
inputPanel.add(probInput);
inputPanel.add(start);
setJMenuBar(flashCardsBar);
add(checkPanel, BorderLayout.NORTH);
add(inputPanel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] arguments)
{
SimpleFlashCardsGUI groovyJukebox = new SimpleFlashCardsGUI();
}
}
Well that's about it. I have attached all of the files to this post in a .zip file if you want to download as well as the executable .jar if you want to test it. As always, feedback is appreciated so let me know what you guys think!


Sign In
Create Account



Back to top









