The applet below asks a student to find a vertex of a given parabola. This problem was chosen for two reasons. First, the vertex of f(x) = ax^2 + bx + c occurs at the point (-b/2a, f(-b/2a)), a calculation that requires fraction arithmetic, so this problem highlights the arithmetic operations in our custom Fraction class. Second, the problem of finding the vertex of a parabola has an obvious graphical component, so we have an opportunity to use our SimpleGraph class in this applet.
The Code
We use the custom Fraction class and the SimpleGraph class from the flashandmath.as3.tools package, both available in the zip file download linked at the bottom of this page. This means that the file Fraction.as and the flashandmath folder must be in the same directory as the fla file under development.
import Fraction;
import flashandmath.as3.tools.SimpleGraph;
We restrict input so that only fractions can be entered, and we declare variables.
txtX.restrict = "0123456789/\\-";
txtY.restrict = "0123456789/\\-";
var a:int;
var b:int;
var c:int;
var md:int;
var answerX:Fraction = new Fraction();
var answerY:Fraction = new Fraction();
Set up the SimpleGraph object on the stage.
var gr:SimpleGraph = new SimpleGraph(250,250);
gr.x = 250;
gr.y = 125;
addChild(gr);
The newProblem function picks random integers a, b and c and forms the parabola "y = ax^2 + bx + c" to be used. We set the Fraction objects answerX, answerY to be the coordinates of the vertex of this parabola..
function newProblem():void {
var s:String;
var xmin:Number;
var xmax:Number;
var ymin:Number;
var ymax:Number;
a = Math.round(10*Math.random());
while (a<2) {
a = Math.round(10*Math.random());
}
b = 2 + Math.round(10*Math.random());
c = 5 + Math.round(10*Math.random());
s = String(a) + "x^2" + " + " + String(b) + "x + " + String(c);
answerX.setFraction(-b,2*a);
// Make answerY = c + b*answerX + a*answerX^2
answerY.setFraction(b,1);
answerY = answerY.fracAdd(answerX.fracMul(a));
answerY = answerY.fracMul(answerX);
answerY = answerY.fracAdd(c);
xmin = Math.round(answerX.toValue() - 5);
xmax = Math.round(answerX.toValue() + 5);
ymin = Math.round(answerY.toValue() - 5);
ymax = Math.round(answerY.toValue() + 5);
gr.setWindow(String(xmin),String(xmax),String(ymin),String(ymax));
gr.board.drawAxes();
gr.board.drawGrid();
gr.graphRectangular(String(a) + "*x^2" + " + " + String(b) + "*x + " + String(c),"x",1,2,0);
txtEquation.text = s;
txtX.text = "";
txtY.text = "";
txtMessage.text = ""
stage.focus = txtX;
md = 0;
}
The checkAnswer function sets up a Fraction object based on the user input and compares it to the answerX and answerY fractions. If the answer is correct, set mode (md) to 1 so that the next time the user hits the "enter" key will cause the newProblem function to be called.
function checkAnswer():void {
var fX:Fraction = new Fraction(txtX.text);
var fY:Fraction = new Fraction(txtY.text);
if (fX.hasError() || fY.hasError()) {
txtMessage.text = "ERROR!";
return;
}
if (fX.isEqual(answerX) && fY.isEqual(answerY)) {
txtMessage.text = "RIGHT!";
md = 1;
}
else {
txtMessage.text = "WRONG!";
}
}
When the "enter" key is pressed, either checkAnswer or newProblem is called, depending on the current mode (md).
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(kevt:KeyboardEvent):void {
if (kevt.keyCode == Keyboard.ENTER) {
if (md == 0) {
checkAnswer();
}
else {
newProblem();
}
}
}
The showPressed handler puts the correct answer on the screen. Remove this code and the "Show Me" button on stage to disable this capability.
btnShow.addEventListener(MouseEvent.CLICK, showPressed);
function showPressed(mevt:MouseEvent):void {
txtX.text = answerX.toString();
txtY.text = answerY.toString();
stage.focus = txtX;
md = 1;
}
Finally, the following function call initiates the first problem.
newProblem();
Download
Download all three fla files as well as the Fraction.as class file in the following compressed folder.