import java.applet.*; import java.awt.*; import java.util.*; // A drill on completing the square public class Csq extends Applet { int a,b,c; //Coefficients of sample problem int LastError[]={0,0,0,0,0,0,0,0,0,0}; int NumberErrorsInARow[]={0,0,0,0,0,0,0,0,0,0}; TextField banswer; Random r; int numerator, denominator; int rightnumerator, rightdenominator; int n1, d1; int state; String usercomm; private Font f; public void setnumbers(){ // Select coefficients randomly. a = 0; while(Math.abs(a) < 2){ a = Math.abs(r.nextInt()) % 20 - 10; } b = 0; while(Math.abs(b) == 0){ b = Math.abs(r.nextInt()) % 10; } c = 0; while(Math.abs(c) == 0){ c = Math.abs(r.nextInt()) % 10; } // Expected answer in state 1 is b/a rightnumerator = b; rightdenominator = a; if(rightdenominator < 0){ rightdenominator = - rightdenominator; rightnumerator = - rightnumerator; } int g = gcd(rightdenominator, rightnumerator); rightnumerator /= g; rightdenominator /= g; } int gcd(int x, int y){ int remainder; x = Math.abs(x); y = Math.abs(y); remainder = x % y; while( remainder != 0){ x = y; y = remainder; remainder = x % y; } return y; } public void init() { f = new Font("TimesRoman", Font.PLAIN, 14); setBackground(Color.cyan); r = new Random(); setnumbers(); banswer = new TextField(20); banswer.setBackground(Color.yellow); add(banswer); state = 1; usercomm = "Enter the value after factoring."; banswer.requestFocus(); } public void paint(Graphics g){ if(state == 1 || state == 2){ paintstate1(g); } else if(state == 3 || state == 4){ paintstate3(g); } else if(state == 5 || state == 6){ paintstate5(g); } else if(state == 7 || state == 8){ paintstate7(g); } else if(state == 9 || state == 10){ paintstate9(g); } } public void paintstate3(Graphics g){ String part1 = a + "x"; String part2; g.setFont( f ); g.drawString(part1, 10, 28); FontMetrics fm = g.getFontMetrics(); int newPosition = fm.stringWidth(part1) + 10; g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); if(b > 0){ part2 = " + " + b + "x"; } else { part2 = " - " + Math.abs(b) + "x"; } if(c > 0){ part2 = part2 + " + " + c; } else { part2 = part2 + " - " + Math.abs(c); } part2 = part2 + " = " + a + " ( x"; g.drawString(part2, newPosition, 28); newPosition += fm.stringWidth(part2); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part3 = " + "; if(rightnumerator > 0){ part3 = " + " + rightnumerator; } else { part3 = " - " + Math.abs(rightnumerator); } if(rightdenominator != 1){ part3 = part3 + "/" + rightdenominator; } part3 = part3 + " x + "; g.drawString(part3, newPosition, 28) ; newPosition += fm.stringWidth(part3); banswer.reshape(newPosition, 12, 48 ,26); newPosition += 48 + 4; String part4; part4 = " ) "; if(c > 0){ part4 = part4 + " + " + c; } else { part4 = part4 + " - " + Math.abs(c); } part4 = part4 + " - [a number to be entered later]"; g.drawString(part4, newPosition, 28) ; g.drawString(usercomm, 10, 58); if(state == 3){ switch(LastError[3]){ case 0: break; case 1: g.drawString("Last time through you got the sign wrong.", 10, 88); break; case 2: g.drawString("On the previous problem, you were off by a factor of 4.", 10, 88); break; case 3: g.drawString("On the previous problem, you were off by a factor of 4 and the sign was wrong.", 10, 88); break; case 4: g.drawString("On the previous problem, you were off by a factor of 2.", 10, 88); break; case 5: g.drawString("On the previous problem, you were off by a factor of 2 and the sign was wrong.", 10, 88); break; case 6: g.drawString("Last time through you did something wrong, but I couldn't figure out what.", 10, 88); break; } } if(state == 4 && NumberErrorsInARow[3] > 1){ String should = "You should take half of the coefficient of x, which is b/(2a) = " + n1; if(d1 != 1) should = should + "/" + d1; should = should + ", and square it, to get " + n1*n1; if(d1 != 1) should = should + "/" + d1*d1; should = should + "."; g.drawString(should, 10, 118); } } public void paintstate1(Graphics g){ String part1 = a + "x"; String part2; g.setFont( f ); g.drawString(part1, 10, 28); FontMetrics fm = g.getFontMetrics(); int newPosition = fm.stringWidth(part1) + 10; g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); if(b > 0){ part2 = " + " + b + "x"; } else { part2 = " - " + Math.abs(b) + "x"; } if(c > 0){ part2 = part2 + " + " + c; } else { part2 = part2 + " - " + Math.abs(c); } part2 = part2 + " = " + a + " ( x"; g.drawString(part2, newPosition, 28); newPosition += fm.stringWidth(part2); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part3 = " + "; g.drawString(part3, newPosition, 28) ; newPosition += fm.stringWidth(part3); banswer.reshape(newPosition, 12, 48 ,26); newPosition += 48 + 4; String part4; part4 = " x ) "; if(c > 0){ part4 = part4 + " + " + c; } else { part4 = part4 + " - " + Math.abs(c); } g.drawString(part4, newPosition, 28) ; g.drawString(usercomm, 10, 58); if(state == 1){ switch(LastError[1]){ case 0: break; case 1: g.drawString("Last time through you got the sign wrong.", 10, 88); break; case 2: g.drawString("Last time through you forgot to divide by a", 10, 88); break; case 3: g.drawString("Last time through your answer was upside down.", 10, 88); break; case 4: g.drawString("Last time through you did something wrong, but I couldn't figure out what.", 10, 88); break; } } if(state == 2 && NumberErrorsInARow[1] > 1){ g.drawString("When you factor " + a + " out, the coefficient " + b + " of x has to be divided by " + a + ", so you should get " + b + "/" + a + ".", 10, 118); } } public void paintstate5(Graphics g){ String part1 = a + "x"; String part2; g.setFont( f ); g.drawString(part1, 10, 28); FontMetrics fm = g.getFontMetrics(); int newPosition = fm.stringWidth(part1) + 10; g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); if(b > 0){ part2 = " + " + b + "x"; } else { part2 = " - " + Math.abs(b) + "x"; } if(c > 0){ part2 = part2 + " + " + c; } else { part2 = part2 + " - " + Math.abs(c); } part2 = part2 + " = " + a + " ( x"; g.drawString(part2, newPosition, 28); newPosition += fm.stringWidth(part2); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part3 = " + "; if(rightnumerator > 0){ part3 = " + " + rightnumerator; } else { part3 = " - " + Math.abs(rightnumerator); } if(rightdenominator != 1){ part3 = part3 + "/" + rightdenominator; } part3 = part3 + " x + " + n1*n1 +"/" + d1*d1 + " ) "; if(c > 0){ part3 = part3 + " + " + c + " + "; } else { part3 = part3 + " - " + Math.abs(c) + " + "; } g.drawString(part3, newPosition, 28) ; newPosition += fm.stringWidth(part3); banswer.reshape(newPosition, 12, 48 ,26); newPosition += 48 + 4; g.drawString(usercomm, 10, 58); if(state == 5){ switch(LastError[5]){ case 0: break; case 1: g.drawString("Last time through you got the sign wrong.", 10, 88); break; case 2: g.drawString("On the previous problem, you were off by a factor of 4.", 10, 88); break; case 3: g.drawString("On the previous problem, you were off by a factor of 4 and the sign was wrong.", 10, 88); break; case 4: g.drawString("On the previous problem, you were off by a factor of 2.", 10, 88); break; case 5: g.drawString("On the previous problem, you were off by a factor of 2 and the sign was wrong.", 10, 88); break; case 6: g.drawString("Last time through, you forgot the factor a.", 10, 88); break; case 7: g.drawString("Last time through, you forgot the factor a and the sign was wrong.", 10, 88); break; case 8: g.drawString("Last time through you did something wrong, but I couldn't figure out what.", 10, 88); break; } } if(state == 6 && NumberErrorsInARow[5] > 1){ String should = "Within the parentheses you added " + n1*n1; if(d1*d1 != 1) should = should + "/" + d1*d1; should = should + ". Since the expression in parentheses is multiplied by " + a +","; g.drawString(should, 10, 118); int rnum = a * n1 * n1; int rdenom = d1 * d1; int g1 = gcd(rnum, rdenom); rnum = rnum/g1; rdenom = rdenom / g1; if(rdenom < 0){ rnum = -rnum; rdenom = -rdenom; } should = "what was really added was " + rnum; if(rdenom != 1) should = should + "/" + rdenom; should = should + ", so now you should subtract that."; g.drawString(should, 10, 148); } } public void paintstate7(Graphics g){ String part1 = a + "x"; String part2; g.setFont( f ); g.drawString(part1, 10, 28); FontMetrics fm = g.getFontMetrics(); int newPosition = fm.stringWidth(part1) + 10; g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); if(b > 0){ part2 = " + " + b + "x"; } else { part2 = " - " + Math.abs(b) + "x"; } if(c > 0){ part2 = part2 + " + " + c; } else { part2 = part2 + " - " + Math.abs(c); } part2 = part2 + " = " + a + " ( x"; g.drawString(part2, newPosition, 28); newPosition += fm.stringWidth(part2); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part3 = " + "; if(rightnumerator > 0){ part3 = " + " + rightnumerator; } else { part3 = " - " + Math.abs(rightnumerator); } if(rightdenominator != 1){ part3 = part3 + "/" + rightdenominator; } part3 = part3 + " x + " + n1*n1; if(d1*d1 != 1) part3 = part3 +"/" + d1*d1; part3 = part3 + " ) "; if(c > 0){ part3 = part3 + " + " + c ; } else { part3 = part3 + " - " + Math.abs(c) ; } int gc1 = gcd(b*b, 4*a); int rnum = -b*b/gc1; int rdenom = 4*a/gc1; if(rdenom < 0){ rdenom = -rdenom; rnum = -rnum; } if(rnum > 0){ part3 = part3 + " + " + rnum; } else { part3 = part3 + " - " + (-rnum); } part3 = part3; if(rdenom != 1) part3 = part3 + "/" + rdenom; part3 = part3 + " = " + a + "(x"; g.drawString(part3, newPosition, 28) ; newPosition += fm.stringWidth(part3); String part4 = " + "; g.drawString(part4, newPosition, 28); newPosition += fm.stringWidth(part4); banswer.reshape(newPosition, 12, 48 ,26); newPosition += 48 + 4; String part5 = " )"; g.drawString(part5,newPosition, 28); newPosition += fm.stringWidth(part5); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part6 = ""; if(c > 0){ part6 = part6 + " + " + c ; } else { part6 = part6 + " - " + Math.abs(c) ; } if(rnum > 0){ part6 = part6 + " + " + rnum; } else { part6 = part6 + " - " + (-rnum); } if(rdenom != 1) part6 = part6 + "/" + rdenom; g.drawString(part6, newPosition, 28); g.drawString(usercomm, 10, 58); if(state == 7){ switch(LastError[7]){ case 0: break; case 1: g.drawString("Last time through you got the sign wrong.", 10, 88); break; case 2: g.drawString("On the previous problem, you were off by a factor of 2.", 10, 88); break; case 3: g.drawString("On the previous problem, you were off by a factor of 2 and the sign was wrong.", 10, 88); break; case 4: g.drawString("The last time through, you should have had b/(2a) but instead you squared it.", 10, 88); break; case 5: g.drawString("Last time through you did something wrong, but I couldn't figure out what.", 10, 88); break; } } if(state == 8 && NumberErrorsInARow[7] > 1){ String should = "This should be the number you squared previously to complete the square, b/(2a) = " + n1; if(d1 != 1) should = should + "/" + d1; should = should + "."; g.drawString(should, 10, 148); } } // public void paintstate9(Graphics g){ String part1 = a + "x"; String part2; g.setFont( f ); g.drawString(part1, 10, 28); FontMetrics fm = g.getFontMetrics(); int newPosition = fm.stringWidth(part1) + 10; g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); if(b > 0){ part2 = " + " + b + "x"; } else { part2 = " - " + Math.abs(b) + "x"; } if(c > 0){ part2 = part2 + " + " + c; } else { part2 = part2 + " - " + Math.abs(c); } part2 = part2 + " = " + a + " ( x"; g.drawString(part2, newPosition, 28); newPosition += fm.stringWidth(part2); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part3 = " + "; if(rightnumerator > 0){ part3 = " + " + rightnumerator; } else { part3 = " - " + Math.abs(rightnumerator); } if(rightdenominator != 1){ part3 = part3 + "/" + rightdenominator; } part3 = part3 + " x + " + n1*n1; if(d1*d1 != 1) part3 = part3 +"/" + d1*d1; part3 = part3 + " ) "; if(c > 0){ part3 = part3 + " + " + c ; } else { part3 = part3 + " - " + Math.abs(c) ; } int gc1 = gcd(b*b, 4*a); int rnum = -b*b/gc1; int rdenom = 4*a/gc1; if(rdenom < 0){ rdenom = -rdenom; rnum = -rnum; } if(rnum > 0){ part3 = part3 + " + " + rnum; } else { part3 = part3 + " - " + (-rnum); } part3 = part3; if(rdenom != 1) part3 = part3 + "/" + rdenom; part3 = part3 + " = " + a + "(x"; g.drawString(part3, newPosition, 28) ; newPosition += fm.stringWidth(part3); String part4 = " + " ; if(n1 > 0){ part4 = part4 + " + " + n1; } else { part4 = part4 + " - " + Math.abs(n1); } if(d1 != 1) part4 = part4 + "/" + d1; part4 = part4 + " )"; g.drawString(part4, newPosition, 28); newPosition += fm.stringWidth(part4); g.drawString("2", newPosition, 20); newPosition += fm.stringWidth("2"); String part5 = " + "; g.drawString(part5, newPosition, 28); newPosition += fm.stringWidth(part5); banswer.reshape(newPosition, 12, 48 ,26); g.drawString(usercomm, 10, 58); if(state == 9){ switch(LastError[9]){ case 0: break; case 1: g.drawString("Last time through you got the sign wrong.", 10, 88); break; case 2: g.drawString("On the previous problem, it looked like you didn't multiply c by the common denominator.", 10, 88); break; case 3: g.drawString("On the previous problem, it appears that you didn't use the correct sign on the second term.",10,88); break; case 4: g.drawString("Last time through you did something wrong, but I couldn't figure out what.", 10, 88); break; } } if(state == 10 && NumberErrorsInARow[9] > 1){ int pnum = -b*b; int pdenom = 4 * a; int g1 = gcd(pnum, pdenom); pnum = pnum/g1; pdenom = pdenom/g1; if(pdenom < 0){ pdenom = - pdenom; pnum = -pnum; } String should; if(pdenom == 1){ should = "Just add " + c; if(pnum > 0) should = should + " + " + pnum; else should = should + " - " + Math.abs(pnum); g.drawString(should, 10, 118); } else{ should = "To add " + c; if(pnum > 0) should = should + " + " + pnum; else should = should + " - " + Math.abs(pnum); should = should + "/" + pdenom + ", note that the common denominator is " + pdenom + "."; g.drawString(should, 10, 118); should = "So you should get (" + c + " times " + pdenom; if(pnum > 0) should = should + ") + " + pnum; else should = should + ") - " + Math.abs(pnum); should = should + ", and divide that by " + pdenom + "."; g.drawString(should, 10, 148); } } } int intParse(String s){ // If s represents an integer, return the integer; otherwise, throw an exception. Integer val; s = s.trim(); if(s.indexOf('+') == 0) s = s.substring(1, s.length()); if(s.equals("-")) throw new NumberFormatException(); try{ val = new Integer(s); } catch(NumberFormatException nef){ throw nef; } return val.intValue(); } void getRationalFrom( String s){ // If s represents a rational number, break out the numerator and denominator int where; s = s.trim(); if((where = s.indexOf('/')) < 0){ denominator = 1; try{ numerator = intParse(s); } catch(NumberFormatException nef){ throw nef; } } else{ try{ numerator = intParse(s.substring(0,where)); denominator = intParse(s.substring(where + 1, s.length())); } catch(NumberFormatException nef){ throw nef; } } } // State descriptions // In state 1, we have just posed a problem, and we are waiting // for input for factoring the coefficient of a x-squared out of // the x terms.If that input produces a number // format exception, we stay in state 1. Otherwise we analyze // the input and go to state 2. // // In state 2, we give the student the good or bad news on the first // field, and then wait for a mouse click. This gives the student // time to read our analysis. On the mouse click, we move to state 3. // // In state 3, we wait for the input that completes the square // on x squared + (b/a)x. On a number format exception, stay in // state 3; otherwise move to state 4. // // In state 4, we wait for the student to read our analysis of // the state 3 input and click the mouse, whereupon we move to // state 5. // // States 5 and 6 are paired like the above states, and handle // the subtraction of what was added in state 3. // // States 7 and 8 are paired, and handle the factoring of the perfect // square term. // // States 9 and 10 are paired, to handle the finishing of the // arithmetic from the state 5 subtraction. On the click in // state 10, we pose a new problem and return to state 1. // public boolean action(Event e, Object o){ if( (e.target == banswer) && (state == 1 )){ try{ getRationalFrom(e.arg.toString()); } catch(NumberFormatException nef){ usercomm = "Your answer isn't correctly typed. Enter the first field again."; repaint(); return(true); } usercomm = "You entered " + e.arg.toString() + " . "; analyzeState1(); repaint(); return(true); } else if( (e.target == banswer) && (state == 3)){ try{ getRationalFrom(e.arg.toString()); } catch(NumberFormatException nef){ usercomm = "Your answer isn't correctly typed. Enter the first field again."; repaint(); return(true); } usercomm = "You entered " + e.arg.toString() + " . "; analyzeState3(); repaint(); return(true); } else if( (e.target == banswer) && (state == 5)){ try{ getRationalFrom(e.arg.toString()); } catch(NumberFormatException nef){ usercomm = "Your answer isn't correctly typed. Enter the first field again."; repaint(); return(true); } usercomm = "You entered " + e.arg.toString() + " . "; analyzeState5(); repaint(); return(true); } else if( (e.target == banswer) && (state == 7)){ try{ getRationalFrom(e.arg.toString()); } catch(NumberFormatException nef){ usercomm = "Your answer isn't correctly typed. Enter the first field again."; repaint(); return(true); } usercomm = "You entered " + e.arg.toString() + " . "; analyzeState7(); repaint(); return(true); } else if( (e.target == banswer) && (state == 9)){ try{ getRationalFrom(e.arg.toString()); } catch(NumberFormatException nef){ usercomm = "Your answer isn't correctly typed. Enter the first field again."; repaint(); return(true); } usercomm = "You entered " + e.arg.toString() + " . "; analyzeState9(); repaint(); return(true); } return(false); } public boolean mouseUp(Event e, int x, int y){ if(state == 2){ state = 3; banswer.setText(""); banswer.requestFocus(); usercomm = "Enter a value to make the factor a perfect square."; repaint(); } else if (state == 4){ state = 5; banswer.setText(""); banswer.requestFocus(); usercomm = "Enter the number that makes the equality correct." ; repaint(); } else if (state == 6){ state = 7; banswer.setText(""); banswer.requestFocus(); usercomm = "Enter the number that makes the equality correct."; repaint(); } else if (state == 8){ state = 9; banswer.setText(""); banswer.requestFocus(); usercomm = "Enter the number that makes the equality correct."; repaint(); } else if (state == 10){ setnumbers(); state = 1; usercomm = "Enter the value after factoring."; banswer.setText(""); banswer.requestFocus(); repaint(); } return true; } void analyzeState1(){ if(numerator * a == denominator * b){ usercomm = usercomm + "That's right. "; LastError[1] = 0; } else if(numerator * a == - denominator * b){ usercomm = usercomm + "The sign is incorrect. "; LastError[1] = 1; } else if(numerator == denominator * b) { usercomm = usercomm + "You forgot to divide by " + a + ". "; LastError[1] = 2; } else if(denominator * a == numerator * b){ usercomm = usercomm + "Your answer is upside down. "; LastError[1] = 3; } else { usercomm = usercomm + "I can't figure out what you did. "; LastError[1] = 4; } if(LastError[1] > 0) NumberErrorsInARow[1]++; else NumberErrorsInARow[1] = 0; usercomm = usercomm + "Click to go on."; String rightanswer = "" + rightnumerator; if(rightdenominator != 1) rightanswer = rightanswer + "/" + rightdenominator; banswer.setText(rightanswer); state = 2; } void analyzeState3(){ int g = gcd(b, 2*a); n1 = b / g; d1 = (2 * a) / g; if(d1 < 0){ d1 = -d1; n1 = -n1; } int rdenom = d1 * d1;; int rnum = n1 * n1; if(numerator * rdenom == denominator * rnum){ usercomm = usercomm + "That's right. Click for next part."; LastError[3] = 0; } else if(numerator * rdenom == - denominator * rnum){ usercomm = usercomm + "Ok, except the sign should be positive. Click."; LastError[3] = 1; } else if(numerator * rdenom == 4 * denominator * rnum){ usercomm = usercomm + "You're off by a factor of 4. Square half the coefficient of x, " + n1 + "/" + d1 + ". Click."; LastError[3] = 2; } else if(numerator * rdenom == -4 * denominator * rnum){ usercomm = usercomm + "The sign is wrong, and you should square half the coefficient of x, " + n1 + "/" + d1 + ". Click."; LastError[3] = 3; } else if(numerator * rdenom == 2 * denominator * rnum){ usercomm = usercomm + "You're off by a factor of 2. Square half the coefficient of x, " + n1 + "/" + d1 + ". Click."; LastError[3] = 4; } else if(numerator * rdenom == -2 * denominator * rnum){ usercomm = usercomm + "The sign is wrong, and you should square half the coefficient of x, " + n1 + "/" + d1 + ". Click."; LastError[3] = 5; } else { usercomm = usercomm + "I don't know what you did. Square half the coefficient of x, " + n1 + "/" + d1 + ". Click for next part."; LastError[3] = 6; } if(LastError[3] > 0) NumberErrorsInARow[3]++; else NumberErrorsInARow[3] = 0; state = 4; String rightanswer = "" + rnum; if(rdenom!=1) rightanswer = rightanswer + "/" + rdenom; banswer.setText(rightanswer); } void analyzeState5(){ // Yes, the state 3 analyzer did this already, but better safe // than sorry. int g = gcd(b, 2*a); n1 = b / g; d1 = (2 * a) / g; if(d1 < 0){ d1 = -d1; n1 = -n1; } // Compute the right answer, which is -a times what // we added within the parentheses. int rnum = -a * n1 * n1; int rdenom = d1 * d1; g = gcd(rnum, rdenom); rnum = rnum/g; rdenom = rdenom / g; if(rdenom < 0){ rnum = -rnum; rdenom = -rdenom; } if(numerator * rdenom == denominator * rnum){ usercomm = usercomm + "That's right. Click for next part."; LastError[5] = 0; } else if(numerator * rdenom == - denominator * rnum){ usercomm = usercomm + "Ok, except for sign. You added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click to go on."; LastError[5] = 1; } else if(numerator * rdenom == 4 * denominator * rnum){ usercomm = usercomm + "You're off by 4. You added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click to go on."; LastError[5] = 2; } else if(numerator * rdenom == -4 * denominator * rnum){ usercomm = usercomm + "The sign is wrong, and you added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click for next."; LastError[5] = 3; } else if(numerator * rdenom == 2 * denominator * rnum){ usercomm = usercomm + "You're off by 2. You added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click for next part."; LastError[5] = 4; } else if(numerator * rdenom == -2 * denominator * rnum){ usercomm = usercomm + "The sign is wrong, and you added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click for next."; LastError[5] = 5; } else if(a * numerator * rdenom == denominator * rnum){ usercomm = usercomm + "You forgot the factor " + a + ". Click to go on." ; LastError[5] = 6; } else if(a * numerator * rdenom == -denominator * rnum){ usercomm = usercomm + "You forgot the factor " + a + ", and you may have sign problems. Click to go on." ; LastError[5] = 7; } else { usercomm = "I don't know how you got that. You added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click for next part."; LastError[5] = 8; } if(LastError[5] > 0) NumberErrorsInARow[5]++; else NumberErrorsInARow[5] = 0; state = 6; String rightanswer = "" + rnum; if(rdenom !=1) rightanswer = rightanswer + "/" + rdenom; banswer.setText(rightanswer); } void analyzeState7(){ int g = gcd(b, 2*a); n1 = b / g; d1 = (2 * a) / g; if(d1 < 0){ d1 = -d1; n1 = -n1; } if(numerator * d1 == denominator * n1){ usercomm = usercomm + "That's right. Click for next part."; LastError[7] = 0; } else if(numerator * d1 == - denominator * n1){ usercomm = usercomm + "The sign is incorrect. Click for next part "; LastError[7] = 1; } else if(numerator * d1 == 2 * denominator * n1){ usercomm = usercomm + "You're off by 2. This should be " + b + "/(2 times " + a +"), which you squared before." ; LastError[7] = 2; } else if(numerator * d1 == -2 * denominator * n1){ usercomm = usercomm + "You're off by 2 and a sign. This should be " + b + "/(2 times " + a +"), which you squared before." ; LastError[7] = 3; } else if(numerator * d1 * d1 == denominator * n1 * n1){ usercomm = usercomm + "You should have just " + b + "/(2 times " + a +"), not its square.Click for next part."; LastError[7] = 4; } else { usercomm = "I don't know how you got that. You added " + a + " times " + n1*n1 + "/" + d1*d1 + " so subtract it. Click for next part."; LastError[7] = 5; } if(LastError[7] > 0) NumberErrorsInARow[7]++; else NumberErrorsInARow[7] = 0; state = 8; String rightanswer = "" + n1; if(d1 != 1) rightanswer = rightanswer + "/" + d1; banswer.setText(rightanswer); } void analyzeState9(){ // Compute the right answer for c - b*b/(4*a) int rnum = 4*a*c - b*b; int rdenom = 4 * a; int g = gcd(rnum,rdenom); rnum = rnum/g; rdenom = rdenom/g; if(rdenom < 0){ rdenom = -rdenom; rnum = -rnum; } int pnum = b*b; int pdenom = 4 * a; g = gcd(pnum, pdenom); pnum = pnum/g; pdenom = pdenom/g; if(pdenom < 0){ pdenom = - pdenom; pnum = -pnum; } if(numerator * rdenom == denominator * rnum){ usercomm = usercomm + "That's right. Click for another problem."; LastError[9] = 0; } else if(numerator * rdenom == - denominator * rnum){ usercomm = usercomm + "The sign is incorrect. Click for another problem."; LastError[9] = 1; } else if(numerator * pdenom == denominator * (c - pnum)){ usercomm = usercomm + "It looks like you didn't multiply " + c + " by the common denominator " + pdenom; LastError[9] = 2; } else if(numerator * pdenom == denominator * (c*pdenom + pnum)){ usercomm = usercomm + "It looks like you added the second term instead of subtracting."; LastError[9] = 3; } else { usercomm = usercomm + "I don't know what you did. Click for another problem."; LastError[9] = 4; } if(LastError[9] > 0) NumberErrorsInARow[9]++; else NumberErrorsInARow[9] = 0; state = 10; String rightanswer = "" + rnum; if(rdenom != 1) rightanswer = rightanswer + "/" + rdenom; banswer.setText(rightanswer); } }