I'm working with math 7 students and teaching integer addition and subtraction. The "rules" are easy, but the idea in many school districts (valid I think) is to have students experiment and develop their own rules based on observations. I just found one demonstration at the demonstration site that samples this idea. It's important and I wanted to see if I could get anything to work. The following are a few ideas. I'd welcome ANY input on improving the code, making it more general, and any other thoughts or ideas on teaching this idea. My last demonstration, especially, is a huge mess. It does what I want, but it's a complete hack...

If these are in any way useful, or get modified to be useful, I'd be happy to post these at the demonstrations site and credit contributions.

ADDING INTEGERS, two arrows on a number line, start at zero

 Manipulate[Module[{string1, string2, string3, ans},
ans = num1 + num2;
string1 =
"(" <> If[num1 < 0, "-", "+"] <> ToString[Abs[num1]] <> ")";
string2 =
"(" <> If[num2 < 0, "-", "+"] <> ToString[Round[Abs[num2]]] <>
")";
string3 =
"(" <> Which[ans < 0, "-", ans == 0, "", ans > 0, "+"] <>
ToString[Round[Abs[ans]]] <> ")", " "];

Graphics[{
{Arrowheads[.02], If[num1 < 0, Red, Yellow],
Arrow[{{0, 1}, {num1, 1}}], If[num2 < 0, Red, Yellow],
Arrow[{{num1, 2}, {num1 + num2, 2}}]},
Line[{{num1, 0}, {num1, 2}}],
Line[{{num1 + num2, 0}, {num1 + num2, 2}}],
Text[Row[{

Style[string1, If[num1 < 0, Red, Yellow]],
" + ",
Style[string2, If[num2 < 0, Red, Yellow]],
"= ",
Style[string3,
Which[ans < 0, Red, ans == 0, Black, ans > 0,
Yellow]]}], {0, 2.5}, BaseStyle -> Large]},
Axes -> {True, False},
PlotRange -> {{-20, 20}, {-3, 3.5}},
ImageSize -> 1200,
AspectRatio -> 0.2,
Ticks -> {Table[{i, If[IntegerQ[i/5], i, ""],
If[IntegerQ[i/5], 0.01, 0.005]}, {i, -20, 20, 1}], None},
Background -> Gray]],
{{num1, 5, "Number 1"}, -10, 10, 1},
{{num2, 5, "Number 2"}, -20 - num1, 20 - num1, 1},

]


Adding integers, start at first number and use one arrow

Manipulate[Module[{string1, string2, string3, ans},
ans = num1 + num2;
string1 =
"(" <> If[num1 < 0, "-", "+"] <> ToString[Abs[num1]] <> ")";
string2 =
"(" <> If[num2 < 0, "-", "+"] <> ToString[Round[Abs[num2]]] <>
")";
string3 =
"(" <> Which[ans < 0, "-", ans == 0, "", ans > 0, "+"] <>
ToString[Round[Abs[ans]]] <> ")", " "];

Graphics[{
{Arrowheads[.02], If[num2 < 0, Red, Yellow],
Arrow[{{num1, 1}, {num1 + num2, 1}}]},
Text[Row[{

Style[string1, If[num1 < 0, Red, Yellow]],
" + ",
Style[string2, If[num2 < 0, Red, Yellow]],
"= ",
Style[string3,
Which[ans < 0, Red, ans == 0, Black, ans > 0,
Yellow]]}], {0, 2.5}, BaseStyle -> Large]},
Axes -> {True, False},
PlotRange -> {{-20, 20}, {-3, 3.5}},
ImageSize -> 1200,
AspectRatio -> 0.2,
Ticks -> {Table[{i, If[IntegerQ[i/5], i, ""],
If[IntegerQ[i/5], 0.01, 0.005]}, {i, -20, 20, 1}], None},
Background -> Gray]],
{{num1, 5, "Number 1"}, -10, 10, 1},
{{num2, 5, "Number 2"}, -20 - num1, 20 - num1, 1},
]


Addition, using counters and the idea of "zero pairs"

Manipulate[
Module[{string1, string2, string3, ans},
ans = num1 + num2;
string1 =
"(" <> If[num1 < 0, "-", "+"] <> ToString[Abs[num1]] <> ")";
string2 =
"(" <> If[num2 < 0, "-", "+"] <> ToString[Round[Abs[num2]]] <>
")";
string3 =
"(" <> Which[ans < 0, "-", ans == 0, "", ans > 0, "+"] <>
ToString[Round[Abs[ans]]] <> ")", " "];

Graphics[{
{If[num1 < 0, Red, Yellow],
Table[Disk[{i, 1}, 0.5], {i, 0, Abs[num1] - 1}]},
Table[Circle[{i, 1}, 0.5], {i, 0, Abs[num1] - 1}],
{If[num2 < 0, Red, Yellow],
Table[Disk[{i, -1}, 0.5], {i, 0, Abs[num2] - 1}]},
Table[Circle[{i, -1}, 0.5], {i, 0, Abs[num2] - 1}],

If[num1 < 0,
Table[Text[Style["-", Large], {i, 1}],  {i, 0, Abs[num1] - 1}],
Table[Text[Style["+", Large], {i, 1}],  {i, 0, Abs[num1] - 1}]],

If[num2 < 0,
Table[Text[Style["-", Large], {i, -1}],  {i, 0, Abs[num2] - 1}],
Table[
Text[Style["+", Large], {i, -1}],  {i, 0, Abs[num2] - 1}]],

Text[Row[{

Style[string1, If[num1 < 0, Red, Yellow]],
" + ",
Style[string2, If[num2 < 0, Red, Yellow]],
"= ",
Style[string3,
Which[ans < 0, Red, ans == 0, Black, ans > 0,
Yellow]]}], {0, 2.5}, BaseStyle -> Large],

Table[Line[{{-0.5 + 5 i, -2}, {-0.5 + 5 i, 2}}], {i, 0, 4}]
}, Background -> Gray,
PlotRange -> {{-5, 25}, {-3, 3}},
ImageSize -> {1200, 300}]],
{{num1, 5, "Number 1"}, -20, 20, 1},
{{num2, 5, "Number 2"}, -20, 20, 1},

]


Subtraction, one arrow Saw this in our textbook... the idea is like counting back change. Start at the second number and point to the first number. Direction of arrow indicates sign, length is the answer. This is a "tough" model, for some students probably harder than simply being given the algorithm

Manipulate[Module[{string1, string2, string3, ans},
ans = num1 - num2;
string1 =
"(" <> If[num1 < 0, "-", "+"] <> ToString[Abs[num1]] <> ")";
string2 =
"(" <> If[num2 < 0, "-", "+"] <> ToString[Round[Abs[num2]]] <>
")";
string3 =
"(" <> Which[ans < 0, "-", ans == 0, "", ans > 0, "+"] <>
ToString[Round[Abs[ans]]] <> ")", " "];

Graphics[{
{Arrowheads[.02], If[ans < 0, Red, Yellow],
Arrow[{{num2, 1}, {num1, 1}}]},
Text[Row[{

Style[string1, If[num1 < 0, Red, Yellow]],
" - ",
Style[string2, If[num2 < 0, Red, Yellow]],
"= ",
Style[string3,
Which[ans < 0, Red, ans == 0, Black, ans > 0,
Yellow]]}], {0, 2.5}, BaseStyle -> Large]},
Axes -> {True, False},
PlotRange -> {{-20, 20}, {-3, 3.5}},
ImageSize -> 1200,
AspectRatio -> 0.2,
Ticks -> {Table[{i, If[IntegerQ[i/5], i, ""],
If[IntegerQ[i/5], 0.01, 0.005]}, {i, -20, 20, 1}], None},
Background -> Gray]],
{{num1, 5, "Number 1"}, -10, 10, 1},
{{num2, 15, "Number 2"}, -20 - num1, 20 - num1, 1},

]


Finally, the big mess. This is using counters. Start with the first number. Sometimes you can subtract, so no problem. Sometimes, in order to have the needed counters, you need to introduce zero pairs, and then subtract. It's a nice idea and works for some students, but I'm not sure if this implementation "works". Tested on one person but that's hardly a basis....

Manipulate[
Module[{string1, string2, string3, ans, index1},
ans = num1 - num2;
string1 =
"(" <> If[num1 < 0, "-", "+"] <> ToString[Abs[num1]] <> ")";
string2 =
"(" <> If[num2 < 0, "-", "+"] <> ToString[Round[Abs[num2]]] <>
")";
string3 =
"(" <> Which[ans < 0, "-", ans == 0, "", ans > 0, "+"] <>
ToString[Round[Abs[ans]]] <> ")", " "];

Graphics[{
(*case 1*)

If[num1 > 0 && num2 >= 0 && num1 > num2,
{Yellow,
Table[Disk[{i, 1}, 0.5], {i, 0, Abs[num1] - 1}],
Black, Table[Circle[{i, 1}, 0.5], {i, 0, Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i, 0, Abs[num1] - 1}]},
Black],

If[num1 > 0 && num2 >= 0 && num1 > num2 && subtract,
{Thickness[0.005], Line[{{-0.5, 1}, {-0.5 + num2, 1}}]},
Black],

(*case 2*)

If[num1 < 0 && num2 < 0 && num2 > num1,
{Red,
Table[Disk[{i, -1}, 0.5], {i, 0, Abs[num1] - 1}],
Black, Table[Circle[{i, -1}, 0.5], {i, 0, Abs[num1] - 1}],
Table[Text[Style["-", Large], {i, -1}],  {i, 0, Abs[num1] - 1}]},
Black],

If[num1 < 0 && num2 < 0 && num2 > num1 && subtract,
{Thickness[0.005], Line[{{-0.5, -1}, {-0.5 + Abs[num2], -1}}]},
Black],

(*case 3*)

If[num1 > 0 && num2 > 0 && num2 > num1,
{Yellow,
Table[Disk[{i, 1}, 0.5], {i, 0,  Abs[num1] - 1}],
Black,
Table[Circle[{i, 1}, 0.5], {i, 0, Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i, 0, Abs[num1] - 1}]},
Black],

If[num1 > 0 && num2 > 0 && num2 > num1 && zero,
{Yellow,
Table[
Disk[{i, 1}, 0.5], {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Red,
Table[
Disk[{i, -1}, 0.5], {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Black,
Table[
Circle[{i, 1}, 0.5], {i, 0, (num2 - num1) + Abs[num1] - 1}],
Table[
Circle[{i, -1}, 0.5], {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i,
0, (num2 - num1) + Abs[num1] - 1}],
Table[
Text[Style["-", Large], {i, -1}],  {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}]},
Black],

If[num1 > 0 && num2 > 0 && num2 > num1 && zero && subtract,
{Yellow,
Table[
Disk[{i, 1}, 0.5], {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Red,
Table[
Disk[{i, -1}, 0.5], {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Black,
Table[
Circle[{i, 1}, 0.5], {i, 0, (num2 - num1) + Abs[num1] - 1}],
Table[
Circle[{i, -1}, 0.5], {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i,
0, (num2 - num1) + Abs[num1] - 1}],
Table[
Text[Style["-", Large], {i, -1}],  {i,
Abs[num1], (num2 - num1) + Abs[num1] - 1}],
Thickness[0.005],
Line[{{-0.5, 1}, {-0.5 + (num2 - num1) + Abs[num1], 1}}]},
Black],

(*case 4*)

If[num1 > 0 && num2 < 0,
{Yellow,
Table[Disk[{i, 1}, 0.5], {i, 0,  Abs[num1] - 1}],
Black,
Table[Circle[{i, 1}, 0.5], {i, 0, Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i, 0, Abs[num1] - 1}]},
Black],

If[num1 > 0 && num2 < 0 && zero,
{Yellow,
Table[
Disk[{i, 1}, 0.5], {i, Abs[num1], Abs[num2] + Abs[num1] - 1}],
Red,
Table[
Disk[{i, -1}, 0.5], {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}],
Black,
Table[
Circle[{i, 1}, 0.5], {i, Abs[num1], Abs[num2] + Abs[num1] - 1}],
Table[
Circle[{i, -1}, 0.5], {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}],
Table[
Text[Style["-", Large], {i, -1}],  {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}]},
Black],

If[num1 > 0 && num2 < 0 && subtract,
{Thickness[0.005],
Line[{{-0.5 + num1, -1}, {-0.5 + num1 - num2, -1}}]},
Black],

(*case 5*)

If[num1 < 0 && num2 > 0,
{Red,
Table[Disk[{i, -1}, 0.5], {i, 0,  Abs[num1] - 1}],
Black,
Table[Circle[{i, -1}, 0.5], {i, 0, Abs[num1] - 1}],
Table[Text[Style["-", Large], {i, -1}],  {i, 0, Abs[num1] - 1}]},
Black],

If[num1 < 0 && num2 > 0 && zero,
{Yellow,
Table[Disk[{i, 1}, 0.5], {i, Abs[num1], Abs[num1] + num2 - 1}],
Red,
Table[
Disk[{i, -1}, 0.5], {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}],
Black,
Table[
Circle[{i, 1}, 0.5], {i, Abs[num1], Abs[num2] + Abs[num1] - 1}],
Table[
Circle[{i, -1}, 0.5], {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}],
Table[
Text[Style["-", Large], {i, -1}],  {i, Abs[num1],
Abs[num2] + Abs[num1] - 1}]},
Black],

If[num1 < 0 && num2 > 0 && subtract,
{Thickness[0.005],
Line[{{-0.5 + Abs[num1], 1}, {-0.5 + Abs[num1] + num2, 1}}]},
Black],

(*case 6*)

If[num1 < 0 && num2 < 0 && num2 < num1,
{Red,
Table[Disk[{i, -1}, 0.5], {i, 0,  Abs[num1] - 1}],
Black,
Table[Circle[{i, -1}, 0.5], {i, 0, Abs[num1] - 1}],
Table[Text[Style["-", Large], {i, -1}],  {i, 0, Abs[num1] - 1}]},
Black],

If[num1 < 0 && num2 < 0 && num2 < num1 && zero,
{Yellow,
Table[Disk[{i, 1}, 0.5], {i, Abs[num1], Abs[num2] - 1}],
Red,
Table[Disk[{i, -1}, 0.5], {i, Abs[num1], Abs[num2] - 1}],
Black,
Table[Circle[{i, 1}, 0.5], {i, Abs[num1], Abs[num2] - 1}],
Table[Circle[{i, -1}, 0.5], {i, Abs[num1], Abs[num2] - 1}],
Table[
Text[Style["+", Large], {i, 1}],  {i, Abs[num1],
Abs[num2] - 1}],
Table[
Text[Style["-", Large], {i, -1}],  {i, Abs[num1],
Abs[num2] - 1}]},
Black],

If[num1 < 0 && num2 < 0 && num2 < num1 && subtract,
{Thickness[0.005], Line[{{-0.5 , -1}, {-0.5 + Abs[num2], -1}}]},
Black],

(*the subtraction statement*)
Text[Row[{
Style[string1, If[num1 < 0, Red, Yellow]],
" - ",
Style[string2, If[num2 < 0, Red, Yellow]],
"= ",
Style[string3,
Which[ans < 0, Red, ans == 0, Black, ans > 0,
Yellow]]}], {0, 2.5}, BaseStyle -> Large],
Table[Line[{{-0.5 + 5 i, -2}, {-0.5 + 5 i, 2}}], {i, 0, 4}]

}, Background -> Gray,
PlotRange -> {{-5, 25}, {-3, 3}},
ImageSize -> {1200, 300}]],
{{num1, 7, "Number 1"}, -10, 10, 1, Appearance -> "Labeled"},
{{num2, 5, "Number 2"}, -10, 10, 1, Appearance -> "Labeled"},
{{zero, False, "zero pairs"},
{False, True},
Enabled ->
If[num1 > 0 && num2 >= 0 && num1 > num2 ||
num1 < 0 && num2 < 0 && num2 > num1, False, True]},
{{subtract, False, "subtract?"}, {False, True},
Enabled ->
If[num1 > 0 && num2 >= 0 && num1 > num2 ||
num1 < 0 && num2 < 0 && num2 > num1, True, zero]},
]


Thanks for reading, as I said, I'm open to improvements, suggestions for using Mathematica to help teach this important concept. Seems so "simple" when you have it, but yeah, having simply taught the "rules" for years, I know LOTS of students just don't get it (and I'm not saying this will help! I'd like it to....)

Tom

-
I'm not sure this question is a great fit for SE, it seems you are mostly looking for code review. Do you have a specific question? –  s0rce Feb 28 at 21:08
Why don't use constrained locators instead of sliders? –  belisarius Feb 28 at 21:16
The idea making the arithmetic operations take on geometric (as well as algebraic) meaning makes a lot of sense. One of the great challenges is to help students move from counting number (whole numbers and later integers) to measuring number (rationals). The real line can play a useful role in introducing number as positions, lengths, and gradually as translations as your examples suggest. I would avoid using a model that mixes counts with measures. If you want to embody numbers as amounts in a model of the real line, line segments (or strips) may be a better way to go. –  David Carraher Feb 28 at 22:07