Algebraic Fraction Answer

Algebraic fraction answer requiring simplification

Complete Code

Download file: AlgebraicFractionAnswer.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'parserMultiAnswer.pl', 'PGcourse.pl');

Preamble

Load the parserMultiAnswer.pl macro to be able to consider two answer rules together (the numerator and denominator) in the answer checker. Note that the niceTables.pl macro is implicitly used, and is automatically loaded by the PGML.pl macro.

Context()->variables->are(y => 'Real');

do {
    $a = random(2, 8, 2);
    $b = random(3, 9, 2);
    $c = random(1, 9, 1);
} until ($a * $c != $b);

$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(
    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 (Value::classMatch($f1stu, 'Formula')
            && Value::classMatch($f2stu, 'Formula')
            && $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 ];
        }
    }
);

Setup

Define MathObject formulas $num and $den which are the correct numerator and denominator for the answer, as well as the bogus answers $numbogus and $denbogus that result from not finding a common denominator (used in the custom answer checker). A MultiAnswer is used to check the numerator and denominator together.

The allowBlankAnswers => 1 option for the MultiAnswer object is set which allows the answers to be left blank, so that partial credit can be given if the student has the numerator or denominator correct but does not enter both. This requires that the type of the students input be checked before using those values in computations or warnings will be issued (in this case the warning “Operands of ’*’ can’t be words” is issued if $f1 * $f2stu == $f1stu * $f2 is computed). This is done for the numerator, for example, with Value::classMatch($f1stu, 'Formula').

The student is also allowed 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. For this the check $f1 == $f1stu || -$f1 == $f1stu is used. Note that || is perl’s “or” operator.

Custom answer messages can be displayed by calling the setMessage method of the MultiAnswer object that $self refers to. For example, with $self->setMessage(1, "Simplify your answer further"), where 1 means to set the message for the first answer blank.

BEGIN_PGML
Perform the indicated operations. Express your answer in reduced form.

[#
    [. [``\frac{[$a]y}{y - [$c]} + \frac{[$b]}{[$c] - y} =``] .]
    [.
        [#
            [. [_]{$multians} .]*{ bottom => 1 }
            [. [_]{$multians} .]
        #]*{ padding => [ 0.5, 0 ] }
    .]
#]*{ padding => [ 0, 0.5 ], valign => 'middle' }
END_PGML

Statement

The fraction answer is created using a LayoutTable from niceTables.pl via its PGML syntax. A LayoutTable is started with [# and is ended with #]*. Options for the table are set in braces after the ending #]*. Cells of the table are started wtih [. and ended with .]. Options for a cell (some of which apply to the row as a whole) are set in braces after the cell’s ending .]. Rows of the table are ended by a starred cell. For example [. ... .]*. Note that the second cell of this table contains a nested LayoutTable.

The outer LayoutTable has a single row with the mathematical expression and then another LayoutTable with two rows that formats the fraction with a bottom horizontal line under the first row. The padding is changed to improve the look of the fraction.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.