Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit cba8190e4e796e74edf4b4f6783518ec5dbd54b6 0 parents
unknown authored
6 .classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="classes"/>
+</classpath>
2  .gitignore
@@ -0,0 +1,2 @@
+classes/**
+.svn
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>mathfacts</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
10 build.properties
@@ -0,0 +1,10 @@
+src.dir=src
+build.dir=build
+classes.dir=classes
+common.build=../builds
+lib.dir=../jars
+major.revision=0.9
+revision=0
+manifest.title=MathFacts
+manifest.vendor=Newellista
+manifest.main-class=org.newell.mathfacts.MathFactsPopupFrame
42 build.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project name="mathfacts" basedir="." default="build">
+ <property file="build.properties" />
+
+ <target name="clean">
+ <delete dir="${build.dir}" />
+ <delete dir="${classes.dir}" />
+ </target>
+
+ <target name="compile" >
+ <mkdir dir="${classes.dir}" />
+ <javac srcdir ="${src.dir}"
+ destdir="${classes.dir}"
+ compiler="javac1.5" >
+ </javac>
+ </target>
+
+ <target name="build" depends="compile">
+ <mkdir dir="${build.dir}"/>
+ <tstamp>
+ <format property="TODAY" pattern="yyyy-MM-dd hh:mm:ss" />
+ </tstamp>
+ <jar destfile="${build.dir}/${ant.project.name}-${major.revision}.${revision}.jar" basedir="${classes.dir}" >
+ <manifest>
+ <attribute name="Built-By" value="${user.name}"/>
+ <attribute name="Implementation-Title" value="${manifest.title}"/>
+ <attribute name="Implementation-Version" value="${major.revision}.${revision} ${TODAY}"/>
+ <attribute name="Implementation-Vendor" value="${manifest.vendor}" />
+ <attribute name="Main-Class" value="${manifest.main-class}" />
+ </manifest>
+ </jar>
+ <copy file="${build.dir}/${ant.project.name}-${major.revision}.${revision}.jar" tofile="${lib.dir}/${ant.project.name}.jar" />
+ <copy file="${build.dir}/${ant.project.name}-${major.revision}.${revision}.jar" todir="${common.build}" />
+ </target>
+
+ <target name="install" depends="build" >
+ <copy file="${build.dir}/${ant.project.name}-${major.revision}.${revision}.jar" tofile="${common.build}/${ant.project.name}.jar" />
+ </target>
+
+ <target name="clean-build" depends="clean,build" />
+</project>
BIN  build/mathfacts-0.9.0.jar
Binary file not shown
21 profiles.ini
@@ -0,0 +1,21 @@
+#
+#Thu Mar 29 08:17:54 MDT 2007
+Operation.1=2
+Operation.0=1
+SecondUpperBound.1=20
+SecondUpperBound.0=20
+NumberOfProfiles=2
+FirstLowerBound.1=1
+FirstLowerBound.0=1
+NumberOfProblems.1=50
+NumberOfProblems.0=50
+SecondLowerBound.1=1
+FirstUpperBound.1=20
+SecondLowerBound.0=1
+FirstUpperBound.0=20
+FontSize.1=18
+CarryFlag.1=1
+Name.1=Subtraction
+Name.0=Addition
+CarryFlag.0=1
+FontSize.0=18
42 src/org/newell/mathfacts/AdditionProblem.java
@@ -0,0 +1,42 @@
+package org.newell.mathfacts;
+
+
+public class AdditionProblem extends MathProblem
+{
+ private static final String OPERATION = "+ ";
+
+ public AdditionProblem( ProblemConstraints problemConstraints )
+ {
+ super( problemConstraints );
+ }
+
+ protected void generateProblem()
+ {
+ firstNumber = getRandomInteger( getProblemConstraints().getFirstLowerBound(), getProblemConstraints().getFirstUpperBound() );
+ secondNumber = getRandomInteger( getProblemConstraints().getSecondLowerBound(), getProblemConstraints().getSecondUpperBound() );
+
+ if( secondNumber > firstNumber )
+ {
+ int tmp = firstNumber;
+ firstNumber = secondNumber;
+ secondNumber = tmp;
+ }
+ }
+
+ public boolean isCorrect()
+ {
+ return false;
+ }
+
+ @Override
+ public int evaluate()
+ {
+ return firstNumber + secondNumber;
+ }
+
+ @Override
+ public String getOperation()
+ {
+ return OPERATION;
+ }
+}
270 src/org/newell/mathfacts/MathFactsFrame.java
@@ -0,0 +1,270 @@
+package org.newell.mathfacts;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Date;
+
+import javax.swing.Box;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+
+@SuppressWarnings("serial")
+public class MathFactsFrame extends JFrame implements ActionListener
+{
+ protected JPanel mainPanel;
+ protected JPanel buttonPanel;
+ protected JButton checkButton;
+ protected JButton exitButton;
+ private JScrollPane scrollPanel;
+ private JPanel problemsPanel;
+ protected ProblemConstraints constraint;
+ protected ArrayList< MathProblemPanel > problemList;
+ protected int numberOfProblems;
+ private long startTime;
+ private long stopTime;
+
+ public MathFactsFrame( ProblemConstraints constraint )
+ {
+ setConstraint( constraint );
+ initialize();
+ }
+
+ public MathFactsFrame( Profile profile )
+ {
+ setConstraint( profile.getProblemConstraints() );
+ setNumberOfProblems( profile.getNumberOfProblems() );
+
+ initialize();
+ setTitle( profile.getName() );
+ }
+
+ public void setNumberOfProblems( int numberOfProblems )
+ {
+ this.numberOfProblems = numberOfProblems;
+ }
+
+ protected void initialize()
+ {
+ getContentPane().setLayout( new BorderLayout() );
+ getContentPane().add( getMainPanel(), BorderLayout.CENTER );
+
+ getContentPane().add( Box.createRigidArea( new Dimension( 5, 0 ) ), BorderLayout.EAST );
+ getContentPane().add( Box.createRigidArea( new Dimension( 5, 0 ) ), BorderLayout.WEST );
+ getContentPane().add( Box.createRigidArea( new Dimension( 0, 5 ) ), BorderLayout.NORTH );
+
+ getContentPane().add( getButtonPanel(), BorderLayout.SOUTH );
+ setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
+ pack();
+ startTimer();
+ }
+
+ private void startTimer()
+ {
+ startTime = System.currentTimeMillis();
+ }
+
+ private void stopTimer()
+ {
+ stopTime = System.currentTimeMillis();
+ }
+
+ public String getElapsedTime()
+ {
+ int seconds;
+ int minutes;
+
+ long elapsedMSecs = stopTime - startTime;
+
+ seconds = ( int ) ( elapsedMSecs / 1000 );
+
+ minutes = seconds / 60;
+
+ seconds %= 60;
+
+ StringBuilder sb = new StringBuilder();
+
+ sb.append( minutes );
+ sb.append( ":" );
+ sb.append( seconds );
+
+ return sb.toString();
+ }
+ private JPanel getButtonPanel()
+ {
+ if( buttonPanel == null )
+ {
+ buttonPanel = new JPanel();
+ buttonPanel.setLayout( new FlowLayout() );
+
+ buttonPanel.add( getCheckButton() );
+ buttonPanel.add( getExitButton() );
+ }
+
+ return buttonPanel;
+ }
+
+ private JButton getExitButton()
+ {
+ if( exitButton == null )
+ {
+ exitButton = new JButton( "Exit" );
+ exitButton.addActionListener( this );
+ exitButton.setEnabled( false );
+ }
+
+ return exitButton;
+ }
+
+ private JButton getCheckButton()
+ {
+ if( checkButton == null )
+ {
+ checkButton = new JButton( "Check Answers" );
+ checkButton.addActionListener( this );
+ }
+
+ return checkButton;
+ }
+
+ private JPanel getMainPanel()
+ {
+ if( mainPanel == null )
+ {
+ mainPanel = new JPanel();
+ mainPanel.setLayout( new BorderLayout() );
+
+ scrollPanel = new JScrollPane( getProblemsPanel() );
+
+ mainPanel.add( scrollPanel, BorderLayout.CENTER );
+ }
+ return mainPanel;
+ }
+
+ private JPanel getProblemsPanel()
+ {
+ if( problemsPanel == null )
+ {
+ problemsPanel = new JPanel();
+ problemsPanel.setLayout( new GridLayout( 0, 10, 8, 8 ) );
+
+ problemList = generateProblemPanelList();
+
+ for( MathProblemPanel m : problemList )
+ {
+ problemsPanel.add( m );
+ }
+ }
+ return problemsPanel;
+ }
+
+ private ArrayList< MathProblemPanel > generateProblemPanelList()
+ {
+ ArrayList< MathProblemPanel > list = new ArrayList< MathProblemPanel >();
+
+ for( int i = 0; i < getNumberOfProblems(); i++ )
+ {
+ list.add( new MathProblemPanel( MathProblemFactory.generateMathProblem( getConstraint() ) ) );
+ }
+ return list;
+ }
+
+ private int getNumberOfProblems()
+ {
+ return numberOfProblems;
+ }
+
+ public static final void main( String[] args )
+ {
+ ProblemConstraints constraint = new ProblemConstraints( ProblemConstraints.ADDITION_OPERATION, ProblemConstraints.NO_CARRY, 1, 20, 1, 9, 12 );
+
+ MathFactsFrame frame = new MathFactsFrame( constraint );
+
+ frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
+
+ frame.setVisible( true );
+ }
+
+ public ProblemConstraints getConstraint()
+ {
+ return constraint;
+ }
+
+ public void setConstraint( ProblemConstraints constraint )
+ {
+ this.constraint = constraint;
+ }
+
+ public void actionPerformed( ActionEvent event )
+ {
+ if( event.getSource() == getCheckButton() )
+ {
+ checkAnswers();
+ }
+ else if( event.getSource() == getExitButton() )
+ {
+ stopTimer();
+ writeStatistics();
+ dispose();
+ }
+ }
+
+ private void writeStatistics()
+ {
+ int numberOfProblems = problemList.size();
+ StringBuilder sb = new StringBuilder();
+
+ sb.append( "*********** Statistics ***********" );
+ sb.append( '\n' );
+ sb.append( new Date().toString() );
+ sb.append( '\n' );
+ sb.append( "Number of Problems: " );
+ sb.append( numberOfProblems );
+ sb.append( '\n' );
+ sb.append( getConstraint() );
+ sb.append( '\n' );
+ sb.append( "Elapsed time: " );
+ sb.append( getElapsedTime() );
+
+ try
+ {
+ PrintWriter pw = new PrintWriter( new BufferedOutputStream( new FileOutputStream( new File( "statistics.txt" ), true ) ) );
+ pw.print( sb.toString() );
+ pw.close();
+ }
+ catch ( FileNotFoundException e )
+ {
+ e.printStackTrace();
+ }
+ }
+
+ private void checkAnswers()
+ {
+ boolean bAllCorrect =true;
+
+ for( MathProblemPanel p : problemList )
+ {
+ boolean bAnswerCorrect = p.checkAnswer();
+ if( !bAnswerCorrect )
+ {
+ bAllCorrect = false;
+ }
+ }
+
+ if( bAllCorrect )
+ {
+ getExitButton().setEnabled( true );
+ }
+ }
+}
145 src/org/newell/mathfacts/MathFactsPopupFrame.java
@@ -0,0 +1,145 @@
+package org.newell.mathfacts;
+
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+@SuppressWarnings("serial")
+public class MathFactsPopupFrame extends JFrame
+{
+ protected HashMap< String, Pair > buttonMap;
+ private JButton exitButton;
+// private JButton newProfileButton;
+ private JPanel buttonPanel;
+
+ public MathFactsPopupFrame()
+ {
+ initialize();
+ pack();
+ }
+
+ protected void initialize()
+ {
+ JPanel p = new JPanel( new BorderLayout() );
+ p.add( getButtonPanel(), BorderLayout.CENTER );
+
+ getContentPane().add( p );
+ }
+
+ private JPanel getButtonPanel()
+ {
+ if( buttonPanel == null )
+ {
+ buttonPanel = new JPanel( new FlowLayout( FlowLayout.LEFT, 5, 5 ) );
+ ArrayList< Profile > profiles = ProfileManager.getInstance().getProfileList();
+
+ for ( Profile p : profiles )
+ {
+ Pair buttonPair = getNamedButton( p.getName(), p );
+
+ buttonPanel.add( buttonPair.getButton() );
+ }
+
+ buttonPanel.add( getExitButton() );
+ }
+
+ return buttonPanel;
+ }
+
+ private JButton getExitButton()
+ {
+ if( exitButton == null )
+ {
+ exitButton = new JButton( "Exit" );
+ exitButton.addActionListener( new ActionListener()
+ {
+ public void actionPerformed( ActionEvent e )
+ {
+ ProfileManager.getInstance().save();
+ System.exit( 0 );
+ }
+ });
+ }
+ return exitButton;
+ }
+
+ private Pair getNamedButton( String name, Profile p )
+ {
+ Pair pair = getButtonMap().get( name );
+
+ if( pair == null )
+ {
+ pair = new Pair();
+ JButton button = new JButton( name );
+ button.addActionListener( new ProfileButtonActionListener() );
+ pair.setButton( button );
+ pair.setProfile( p );
+ getButtonMap().put( name, pair );
+ }
+
+ return pair;
+ }
+
+ public static void main( String[] args )
+ {
+ MathFactsPopupFrame frame = new MathFactsPopupFrame();
+
+ frame.setVisible( true );
+ }
+
+ class ProfileButtonActionListener implements ActionListener
+ {
+ public void actionPerformed( ActionEvent e )
+ {
+ JButton button = ( JButton ) e.getSource();
+ String name = button.getText();
+ Pair pair = getButtonMap().get( name );
+
+ MathFactsFrame frame = new MathFactsFrame( pair.getProfile() );
+ frame.setVisible( true );
+ }
+ }
+
+ protected HashMap< String, Pair > getButtonMap()
+ {
+ if( buttonMap == null )
+ {
+ buttonMap = new HashMap< String, Pair >();
+ }
+
+ return buttonMap;
+ }
+
+ class Pair
+ {
+ protected JButton button;
+ protected Profile profile;
+
+ public JButton getButton()
+ {
+ return button;
+ }
+
+ public void setButton( JButton button )
+ {
+ this.button = button;
+ }
+
+ public Profile getProfile()
+ {
+ return profile;
+ }
+
+ public void setProfile( Profile profile )
+ {
+ this.profile = profile;
+ }
+ }
+}
104 src/org/newell/mathfacts/MathProblem.java
@@ -0,0 +1,104 @@
+package org.newell.mathfacts;
+
+import java.util.Random;
+
+abstract public class MathProblem
+{
+ protected int firstNumber;
+ protected int secondNumber;
+ protected ProblemConstraints problemConstraints;
+
+ public MathProblem( ProblemConstraints problemConstraints )
+ {
+ setProblemConstraints( problemConstraints );
+ generateProblem();
+ }
+
+ public String getFirstLine( int minLength )
+ {
+ String txt = Integer.toString( firstNumber );
+
+ if ( txt.length() < minLength )
+ {
+ StringBuilder sb = new StringBuilder();
+
+ for ( int i = 0; i < minLength - txt.length(); i++ )
+ {
+ sb.append( " " );
+ }
+ sb.append( txt );
+ txt = sb.toString();
+ }
+ return txt;
+ }
+
+ public String getSecondNumber()
+ {
+ return Integer.toString( secondNumber );
+ }
+
+ public String getSecondLine()
+ {
+ StringBuilder sb = new StringBuilder();
+
+ sb.append( getOperation() );
+ sb.append( " " );
+ sb.append( getSecondNumber() );
+
+ return sb.toString();
+ }
+
+ public int pickNumberInRange( int aLowerLimit, int aUpperLimit )
+ {
+ if ( aLowerLimit >= aUpperLimit )
+ {
+ StringBuilder message = new StringBuilder();
+ message.append( "Lower limit (" );
+ message.append( aLowerLimit );
+ message.append( ") must be lower than Upper limit (" );
+ message.append( aUpperLimit );
+ message.append( ")" );
+ throw new IllegalArgumentException( message.toString() );
+ }
+
+ Random generator = new Random();
+ // get the range, casting to long to avoid overflow problems
+ long range = ( long ) aUpperLimit - ( long ) aLowerLimit + 1;
+ // compute a fraction of the range, 0 <= frac < range
+ long fraction = ( long ) ( range * generator.nextDouble() );
+ return ( int ) ( fraction + aLowerLimit );
+ }
+
+ protected int getRandomInteger( int lowerBound, int upperBound )
+ {
+ if ( lowerBound > upperBound )
+ {
+ int tmp = upperBound;
+ upperBound = lowerBound;
+ lowerBound = tmp;
+ }
+
+ Random generator = new Random();
+ // get the range, casting to long to avoid overflow problems
+ long range = ( long ) upperBound - ( long ) lowerBound + 1;
+ // compute a fraction of the range, 0 <= frac < range
+ long fraction = ( long ) ( range * generator.nextDouble() );
+ return ( int ) ( fraction + lowerBound );
+ }
+
+ public ProblemConstraints getProblemConstraints()
+ {
+ return problemConstraints;
+ }
+
+ public void setProblemConstraints( ProblemConstraints problemConstraints )
+ {
+ this.problemConstraints = problemConstraints;
+ }
+
+ abstract public String getOperation();
+
+ abstract public int evaluate();
+
+ abstract protected void generateProblem();
+}
34 src/org/newell/mathfacts/MathProblemFactory.java
@@ -0,0 +1,34 @@
+package org.newell.mathfacts;
+
+import java.util.ArrayList;
+
+public class MathProblemFactory
+{
+ static MathProblem generateMathProblem( ProblemConstraints problemConstraints )
+ {
+ MathProblem mathProblem = null;
+
+ switch( problemConstraints.getOperation() )
+ {
+ case ProblemConstraints.ADDITION_OPERATION:
+ mathProblem = new AdditionProblem( problemConstraints );
+ break;
+ case ProblemConstraints.SUBTRACTION_OPERATION:
+ mathProblem = new SubtractionProblem( problemConstraints );
+ break;
+ }
+
+ return mathProblem;
+ }
+
+ static ArrayList< MathProblem > generateMathProblemList( int numberOfProblems, ProblemConstraints problemConstraints )
+ {
+ ArrayList< MathProblem > listOfProblems = new ArrayList< MathProblem >();
+
+ for( int i = 0; i < numberOfProblems; i++ )
+ {
+ listOfProblems.add( generateMathProblem( problemConstraints ) );
+ }
+ return listOfProblems;
+ }
+}
162 src/org/newell/mathfacts/MathProblemPanel.java
@@ -0,0 +1,162 @@
+package org.newell.mathfacts;
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.GridLayout;
+
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+@SuppressWarnings("serial")
+public class MathProblemPanel extends JPanel
+{
+ protected ProblemConstraints problemConstraints;
+ protected JLabel equalLineLabel;
+ protected JTextField answerEditBox;
+ protected MathProblem mathProblem;
+ protected JLabel firstLine;
+ protected JLabel secondLine;
+
+ protected Font myFont;
+ private static final Color ERROR_COLOR = Color.red;
+ private static final int ERROR_VALUE = -1;
+ private static final Color CORRECT_COLOR = Color.getColor( "background" );
+
+ public MathProblemPanel( MathProblem mathProblem )
+ {
+ setMathProblem( mathProblem );
+ setProblemConstraints( mathProblem.getProblemConstraints() );
+ initialize();
+ }
+
+ protected void initialize()
+ {
+ myFont = new Font( "Courier", Font.PLAIN, problemConstraints.getFontSize() );
+ setLayout( new GridLayout( 4, 1 ) );
+ String secondLine = mathProblem.getSecondLine();
+
+ add( getFirstLine( secondLine.length() ) );
+ add( getSecondLine() );
+ add( getEqualLine() );
+ add( getAnswerEditBox() );
+ }
+
+ private JTextField getAnswerEditBox()
+ {
+ if( answerEditBox == null )
+ {
+ answerEditBox = new JTextField();
+ Dimension dim = answerEditBox.getPreferredSize();
+
+ answerEditBox.setFont( myFont );
+
+ dim.width = 10;
+
+ answerEditBox.setPreferredSize( dim );
+ answerEditBox.setMaximumSize( dim );
+ answerEditBox.setMinimumSize( dim );
+ }
+ return answerEditBox;
+ }
+
+ private JLabel getEqualLine()
+ {
+ if( equalLineLabel == null )
+ {
+ equalLineLabel = new JLabel( "----" );
+ equalLineLabel.setFont( myFont );
+ }
+
+ return equalLineLabel;
+ }
+
+ public boolean isCorrect()
+ {
+ int enteredValue = ERROR_VALUE;
+ int correctValue = getMathProblem().evaluate();
+ String enteredText = getAnswerEditBox().getText();
+ if( enteredText != null && enteredText.length() != 0 )
+ {
+ enteredValue = Integer.parseInt( getAnswerEditBox().getText() );
+ }
+
+ return ( correctValue == enteredValue );
+ }
+
+ public boolean checkAnswer()
+ {
+ if( isCorrect() == false )
+ {
+ setBackground( ERROR_COLOR );
+ }
+ else
+ {
+ setBackground( CORRECT_COLOR );
+ }
+
+ return isCorrect();
+ }
+
+ protected JLabel getFirstLine(int minLength )
+ {
+ if( firstLine == null )
+ {
+ firstLine = new JLabel( mathProblem.getFirstLine( minLength ) );
+ firstLine.setFont( myFont );
+ }
+
+ return firstLine;
+ }
+
+ protected Component getSecondLine()
+ {
+ if( secondLine == null )
+ {
+ secondLine = new JLabel( mathProblem.getSecondLine() );
+ secondLine.setFont( myFont );
+ }
+ return secondLine;
+ }
+
+ public static void main( String[] args )
+ {
+ JFrame frame = new JFrame();
+
+ ProblemConstraints constraint = new ProblemConstraints( ProblemConstraints.ADDITION_OPERATION, ProblemConstraints.NO_CARRY, 0, 20, 0, 20, 12 );
+
+ MathProblem problem = MathProblemFactory.generateMathProblem( constraint );
+
+ MathProblemPanel panel = new MathProblemPanel( problem );
+
+ frame.getContentPane().add( panel );
+
+ frame.pack();
+
+ frame.setVisible( true );
+
+ }
+
+ public MathProblem getMathProblem()
+ {
+ return mathProblem;
+ }
+
+ public void setMathProblem( MathProblem mathProblem )
+ {
+ this.mathProblem = mathProblem;
+ }
+
+ public ProblemConstraints getProblemConstraints()
+ {
+ return problemConstraints;
+ }
+
+ public void setProblemConstraints( ProblemConstraints problemConstraints )
+ {
+ this.problemConstraints = problemConstraints;
+ }
+}
134 src/org/newell/mathfacts/ProblemConstraints.java
@@ -0,0 +1,134 @@
+package org.newell.mathfacts;
+
+public class ProblemConstraints
+{
+ public static final int ADDITION_OPERATION = 1;
+ public static final int SUBTRACTION_OPERATION = 2;
+ public static final int MULTIPLICATION_OPERATION = 3;
+ public static final int DIVISION_OPERATION = 4;
+
+ public static final int NO_CARRY = 1;
+ public static final int MAY_CARRY = 2;
+ public static final int MUST_CARRY = 3;
+
+ protected int operation;
+
+ protected int firstUpperBound;
+ protected int secondUpperBound;
+
+ protected int firstLowerBound;
+ protected int secondLowerBound;
+
+ protected int carryFlag;
+
+ protected int fontSize;
+
+ public ProblemConstraints( int operation, int flags, int firstLowerBound, int firstUpperBound, int secondLowerBound, int secondUpperBound, int fontSize )
+ {
+ this.operation = operation;
+ this.firstLowerBound = firstLowerBound;
+ this.firstUpperBound = firstUpperBound;
+ this.secondLowerBound = secondLowerBound;
+ this.secondUpperBound = secondUpperBound;
+ this.carryFlag = flags;
+ this.fontSize = fontSize;
+ }
+
+ public int getOperation()
+ {
+ return operation;
+ }
+
+ public void setOperation( int operation )
+ {
+ this.operation = operation;
+ }
+
+ public int getFirstLowerBound()
+ {
+ return firstLowerBound;
+ }
+
+ public void setFirstLowerBound( int firstLowerBound )
+ {
+ this.firstLowerBound = firstLowerBound;
+ }
+
+ public int getFirstUpperBound()
+ {
+ return firstUpperBound;
+ }
+
+ public void setFirstUpperBound( int firstUpperBound )
+ {
+ this.firstUpperBound = firstUpperBound;
+ }
+
+ public int getSecondLowerBound()
+ {
+ return secondLowerBound;
+ }
+
+ public void setSecondLowerBound( int secondLowerBound )
+ {
+ this.secondLowerBound = secondLowerBound;
+ }
+
+ public int getSecondUpperBound()
+ {
+ return secondUpperBound;
+ }
+
+ public void setSecondUpperBound( int secondUpperBound )
+ {
+ this.secondUpperBound = secondUpperBound;
+ }
+
+ public int getCarryFlag()
+ {
+ return carryFlag;
+ }
+
+ public void setCarryFlag( int carryFlag )
+ {
+ this.carryFlag = carryFlag;
+ }
+
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder();
+
+ sb.append( "Operation: " );
+ sb.append( getOperation() );
+ sb.append( "\n" );
+ sb.append( "Carry Flag: " );
+ sb.append( getCarryFlag() );
+ sb.append( "\n" );
+ sb.append( "FontSize: " );
+ sb.append( getFontSize() );
+ sb.append( "\n" );
+ sb.append( "FirstUpperBound: " );
+ sb.append( getFirstUpperBound() );
+ sb.append( "\n" );
+ sb.append( "FirstLowerBound: " );
+ sb.append( getFirstLowerBound() );
+ sb.append( "\n" );
+ sb.append( "SecondUpperBound: " );
+ sb.append( getSecondUpperBound() );
+ sb.append( "\n" );
+ sb.append( "SecondLowerBound: " );
+ sb.append( getSecondLowerBound() );
+
+ return sb.toString();
+ }
+
+ public int getFontSize()
+ {
+ return fontSize;
+ }
+
+ public void setFontSize( int fontSize )
+ {
+ this.fontSize = fontSize;
+ }
+}
46 src/org/newell/mathfacts/Profile.java
@@ -0,0 +1,46 @@
+package org.newell.mathfacts;
+
+public class Profile
+{
+ protected String name;
+ protected int numberOfProblems;
+ protected ProblemConstraints problemConstraints;
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName( String name )
+ {
+ this.name = name;
+ }
+
+ public int getNumberOfProblems()
+ {
+ return numberOfProblems;
+ }
+
+ public void setNumberOfProblems( int numberOfProblems )
+ {
+ this.numberOfProblems = numberOfProblems;
+ }
+
+ public ProblemConstraints getProblemConstraints()
+ {
+ return problemConstraints;
+ }
+
+ public void setProblemConstraints( ProblemConstraints problemConstraints )
+ {
+ this.problemConstraints = problemConstraints;
+ }
+
+ public void setNumberOfProblems( String numberOfProblemsString )
+ {
+ if( numberOfProblemsString != null )
+ {
+ setNumberOfProblems( Integer.parseInt( numberOfProblemsString ) );
+ }
+ }
+}
158 src/org/newell/mathfacts/ProfileManager.java
@@ -0,0 +1,158 @@
+package org.newell.mathfacts;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Properties;
+
+public class ProfileManager
+{
+ protected ArrayList< Profile > profileList;
+ protected static ProfileManager profileManager;
+ private static final String COUNT_KEY = "NumberOfProfiles";
+ private static final String NAME_KEY = "Name.";
+ private static final String NUM_PROBLEMS_KEY = "NumberOfProblems.";
+ private static final String OPERATION_KEY = "Operation.";
+ private static final String FLB_KEY = "FirstLowerBound.";
+ private static final String FUB_KEY = "FirstUpperBound.";
+ private static final String SLB_KEY = "SecondLowerBound.";
+ private static final String SUB_KEY = "SecondUpperBound.";
+ private static final String CARRY_KEY = "CarryFlag.";
+ private static final String FONT_SIZE_KEY = "FontSize.";
+
+ private static final String profileName = "./profiles.ini";
+
+ private ProfileManager()
+ {
+ loadProfiles();
+ }
+
+ private void loadProfiles()
+ {
+ Properties p = new Properties();
+ try
+ {
+ p.load( new FileInputStream( profileName ) );
+ String numProfilesString = p.getProperty( COUNT_KEY );
+
+ if ( numProfilesString != null )
+ {
+ int numProfiles = Integer.parseInt( numProfilesString );
+
+ for ( int i = 0; i < numProfiles; i++ )
+ {
+ Profile profile = new Profile();
+
+ profile.setName( p.getProperty( NAME_KEY + i ) );
+ profile.setNumberOfProblems( p
+ .getProperty( NUM_PROBLEMS_KEY + i ) );
+ String operation = p.getProperty( OPERATION_KEY + i );
+ String flb = p.getProperty( FLB_KEY + i );
+ String fub = p.getProperty( FUB_KEY + i );
+ String slb = p.getProperty( SLB_KEY + i );
+ String sub = p.getProperty( SUB_KEY + i );
+ String carryFlag = p.getProperty( CARRY_KEY + i );
+ String fontSize = p.getProperty( FONT_SIZE_KEY + i );
+
+ if( fontSize == null )
+ {
+ fontSize = "12";
+ }
+
+ ProblemConstraints problemConstraint = new ProblemConstraints(
+ Integer.parseInt( operation ), Integer
+ .parseInt( carryFlag ), Integer
+ .parseInt( flb ), Integer.parseInt( fub ),
+ Integer.parseInt( slb ), Integer.parseInt( sub ), Integer.parseInt( fontSize ) );
+
+ profile.setProblemConstraints( problemConstraint );
+
+ getProfileList().add( profile );
+ }
+ }
+ }
+ catch ( FileNotFoundException e )
+ {
+ e.printStackTrace();
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ }
+ }
+
+ public static ProfileManager getInstance()
+ {
+ if ( profileManager == null )
+ {
+ profileManager = new ProfileManager();
+ }
+
+ return profileManager;
+ }
+
+ public Profile getNamedProfile( String profileName )
+ {
+ Profile foundProfile = null;
+
+ for ( Profile p : getProfileList() )
+ {
+ if ( p.getName().compareToIgnoreCase( profileName ) == 0 )
+ {
+ foundProfile = p;
+ break;
+ }
+ }
+ return foundProfile;
+ }
+
+ public ArrayList< Profile > getProfileList()
+ {
+ if ( profileList == null )
+ {
+ profileList = new ArrayList< Profile >();
+ }
+
+ return profileList;
+ }
+
+ public void save()
+ {
+ Properties p = new Properties();
+
+ p.setProperty( COUNT_KEY, Integer.toString( getProfileList().size() ) );
+
+ for ( int i = 0; i < getProfileList().size(); i++ )
+ {
+ Profile profile = getProfileList().get( i );
+
+ p.setProperty( NAME_KEY + i, profile.getName() );
+ p.setProperty( NUM_PROBLEMS_KEY + i, Integer.toString( profile.getNumberOfProblems() ) );
+
+ ProblemConstraints pc = profile.getProblemConstraints();
+ p.setProperty( OPERATION_KEY + i, Integer.toString( pc.getOperation() ) );
+ p.setProperty( FLB_KEY + i, Integer.toString( pc.getFirstLowerBound() ) );
+ p.setProperty( FUB_KEY + i, Integer.toString( pc.getFirstUpperBound() ) );
+ p.setProperty( SLB_KEY + i, Integer.toString( pc.getSecondLowerBound() ) );
+ p.setProperty( SUB_KEY + i, Integer.toString( pc.getSecondUpperBound() ) );
+ p.setProperty( CARRY_KEY + i, Integer.toString( pc.getCarryFlag() ) );
+ p.setProperty( FONT_SIZE_KEY + i, Integer.toString( pc.getFontSize() ) );
+ }
+ try
+ {
+ p.store( new FileOutputStream( profileName ), "" );
+ }
+ catch ( FileNotFoundException e )
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ catch ( IOException e )
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+}
41 src/org/newell/mathfacts/SubtractionProblem.java
@@ -0,0 +1,41 @@
+package org.newell.mathfacts;
+
+
+public class SubtractionProblem extends MathProblem
+{
+ private static final String OPERATION = "- ";
+ protected int answer;
+
+ public SubtractionProblem( ProblemConstraints problemConstraints )
+ {
+ super( problemConstraints );
+ }
+
+ protected void generateProblem()
+ {
+ do
+ {
+ firstNumber = getRandomInteger( getProblemConstraints().getFirstLowerBound(), getProblemConstraints().getFirstUpperBound() );
+ secondNumber = getRandomInteger( getProblemConstraints().getSecondLowerBound(), getProblemConstraints().getSecondUpperBound() );
+ } while ( firstNumber == secondNumber );
+
+ if( secondNumber > firstNumber )
+ {
+ int tmp = firstNumber;
+ firstNumber = secondNumber;
+ secondNumber = tmp;
+ }
+ }
+
+ @Override
+ public int evaluate()
+ {
+ return firstNumber - secondNumber;
+ }
+
+ @Override
+ public String getOperation()
+ {
+ return OPERATION;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.