Algebraic fraction answer requiring simplification
Download file: AlgebraicFractionAnswer.pg
DOCUMENT(); loadMacros( 'PGstandard.pl', 'PGML.pl', 'niceTables.pl', 'parserMultiAnswer.pl', 'PGcourse.pl' );
Preamble
We include the macros file niceTables.pl
to be able to display the answer boxes on top of each other (as a fraction).
Context()->variables->are(y => 'Real'); Context()->{error}{msg}{"Operands of '*' can't be words"} = ' '; do { $a = random(2, 8, 2); $b = random(3, 9, 2); $c = random(1, 9, 1); } until ($a*$c != $b); $fraction = "\frac{$a y}{y-$c} + \frac{$b}{$c - y} "; $num = Formula("$a y - $b"); $den = Formula("y - $c"); $numbogus = Formula("$a*y+$b"); $denbogus = Formula("(y-$c)*($c-y)"); $multians = MultiAnswer($num, $den)->with( singleResult => 0, allowBlankAnswers => 1, checker => sub { my ($correct, $student, $self) = @_; my ($f1stu, $f2stu) = @{$student}; my ($f1, $f2) = @{$correct}; if (($f1 == $f1stu && $f2 == $f2stu) || (-$f1 == $f1stu && -$f2 == $f2stu)) { return [ 1, 1 ]; } elsif ($f1 == $f1stu || -$f1 == $f1stu) { return [ 1, 0 ]; } elsif (($numbogus == $f1stu || -$numbogus == $f1stu) || ($denbogus == $f2stu || -$denbogus == $f2stu)) { $self->setMessage(1, "Find a common denominator first"); $self->setMessage(2, "Find a common denominator first"); return [ 0, 0 ]; } elsif ($f2 == $f2stu || -$f2 == $f2stu) { return [ 0, 1 ]; } elsif ($f1 * $f2stu == $f1stu * $f2) { $self->setMessage(1, "Simplify your answer further"); $self->setMessage(2, "Simplify your answer further"); return [ 0, 0 ]; } else { return [ 0, 0 ]; } } ); # Display the fraction and answer blanks nicely # Context()->texStrings; # if ($displayMode eq 'TeX') { # $showfraction = # "\[ $fraction = " # . $multians->ans_rule(10) # . $multians->ans_rule(10) . " \]"; # } else { # $showfraction = ColumnTable( # "\( \displaystyle $fraction = \)", # $multians->ans_rule(20) . $BR . $HR . $multians->ans_rule(20), # separation => 10, # ); # } # Context()->normalStrings; $frac = LayoutTable([[[ans_rule(10), rowbottom => 1]],[ans_rule(10)]], center => 0, allcellcss => {padding => '4pt'});
Setup
We define a string $fraction
that will be displayed in TeX mode. We define MathObjects formulas $num
and $den
that are the correct numerator and denominator for the answer, as well as some bogus answers $numbogus
and $denbogus
that result from not finding a common denominator. We use MultiAnswer to manipulate both student answers at the same time. In $multians
we allow for answers to be left blank, which requires one of two things: either we disable the error message or do type checking on the students input by using ref($f1) eq ref($f1stu)
to see if the correct numerator $f1
and the student numerator $f1stu
have the same type. We used the code Context()->{error}{msg}{"Operands of '*' can't be words"} = ' ';
to disable the error message because this method allows the “Simplify your answer” feature to work more reliably. We also allow for the student to enter the fraction as either (6y-3)/(y-2)
or (3-6y)/(2-y)
, since both are correct and it is not clear that one is preferable to the other, which requires that we check $f1 == $f1stu || -$f1 == $f1stu
. Here ||
is perl’s “or” operator. We provide some custom answer hints by testing for bogus numerators and denominators and displaying answer messages via $self->setMessage(1, "Simplify your answer further");
, where the 1 stands for the first answer blank.
The fraction answer is created using a LayoutTable
from niceTables.pl
. This is just a one-column table with the first row with a horizontal line. The padding is changed to improve the look of the fraction.
BEGIN_PGML Perform the indicated operations. Express your answer in reduced form. [``[$fraction]=``][$frac]*** END_PGML
Statement
Everything is as usual. Insert the fraction and answer blanks using $showfraction
.
ANS($multians->cmp()); # ANS($multians->cmp());
Answer
It is necessary to install the answer evaluator with ANS
, since ans_rule
was used to produce answer blanks.
BEGIN_PGML_SOLUTION Solution explanation goes here. END_PGML_SOLUTION ENDDOCUMENT();
Solution
A solution should be provided here.