Calculator

(ToC)


Video Introduction (15 minutes) <<--- Watch this video first, then

English IDE <<--- Click this link and write your calculator program here.

When your calculator runs, Raise your hand in Zoom to continue.

If you didn't watch the video, or if you didn't pay much attention, and if you cannot figure out why your program does not run properly, you might reconsider: Watch the Video.

Or, if you are like me and prefer to read at your own pace instead of whatever pace set in some video, You can read the transcript (and probably come out ahead of the non-readers).

Video Transcript: 4. Calculator

Today we begin a serious program -- not a graphical toy like PBJ, but a real program like people get paid for. YOU will write a 4-function calculator something like this one.

First let's think about exactly what a calculator does. Obviously it calculates, but that's not good enough for the stupid computer, which must be told every tiny little step. When I did this stuff (and got paid for it!) I had to write code for every operation -- even long multiplication and division. Do they teach that stuff in school any more? They did when I went to school, but you-all just buy a $2 calculator at the Dollar store. Which is OK, both Java and Kitchen English know how to multiply and divide.

But we still need to tell it what steps to do in what order, broken down into the Five Concepts you already know about -- and nothing else. So here they are, to remind us this is what we are aiming for:

5 Concepts
 Sequence
 Iteration
 Conditional
 Input/Output
 Variables


Before we can do that, we must understand (in our own heads) exactly what a calculator does when we push the buttons. It turns out to be exactly what we do when we do the same calculation in our heads (or on paper: I do a lot on paper). If you do not want to know how things work, you cannot tell the computer how things work. You cannot tell the computer what is not in your head. Not even the AI people do that.

OK, here's the schoolkid version

1 + 2 + 3 + 4 = ?


Let's do the pencil-and-paper version, one step at a time. You are not allowed to use a calculator for this (you are writing the calculator program, so you must assume it does not yet exist), and you must compute the sum. What do you do first? If you just write down the answer, you can't be a programmer, The computer needs to do it One. Step. At. A. Time.

So what do you do first? Add 1+2 and get an intermediate result three.

3 + 3 + 4 = ?


Now the problem looks exactly like it did at the beginning, except there is one less number to add. Do it again 3+3 is six.

6 + 4 = ?
It got shorter again. Do you see where this is going? We have an Iteration, the same thing over and over, we are going to add the next number to the intermediate result, and that will be the new intermediate result. That intermediate result sounds like a Variable. OK, finish out the calculation: 6+4 equals ten. All in pencil and paper, no calculator needed.
6 + 4 = 10


Remember, you are always smarter than the computer you are programming. If you can't do it without the computer, you can't tell the computer to do it. Unless somebody else already programmed some of it, you can build on their work, like a carpenter frames a house on top of the concrete foundation somebody else laid. Like a PBJ is built on top of bread your Mom bought at the store. Her great-grandmother made her own bread. Somebody has to bake the bread. Somebody has to tell the computer, Every.  Thing.  It.  Does.

OK, let's watch the calculator and the pencil version together. When the calculator is turned on (the app starts) the initial working result is zero (like the image above). The first thing we do is replace that working result with the first number in the sum, in this case one.  Let me write that down here, so I don't forget what happened:

Put initial value into result


The next step is to see what operator is coming. This is a four-function calculator, it could be add, subtract, multiply, or divide, but we are doing addition, so the user presses the Plus key (or in our case, types a plus on the keyboard) and the calculator sees that button press and remembers it. Your program needs to see the same keypress and remember it -- at least until you have both numbers to add.

How does a computer see when somebody types something on the keyboard? That's one of the Five Concepts we are looking for, Input. How does it remember it? In a Variable. The only way a computer ever remembers anything at all is in a variable. A variable is a place in the computer memory that has a name and we can put some kind of value there. Let's call this variable "operator" and it will remember what operator (character, '+' or '-' or whatever) the user typed, and we can combine it with the Input command. How do I know that? If I were writing this as a program in the IDE, there at the bottom in the Reference section, one of the commands you can use in this computer is "Input" (follow the link, then come back) and it is followed by a variable name, where the input value goes. So I'm going to write that down, so I don't forget.

Put initial value into result
Input operator


Speaking of remembering, we already decided that the result will also be a variable. Remember what happened when the user pressed the "1" button on the calculator -- that would be the numeral one key on your keyboard -- and that value went into the result? This is basically the same idea, input to a variable, so lemme fix it:

Input result
Input operator


Where were we? Oh yes, we just did the Plus operator, now we are ready for the value to be added to the result. In our pencil-and-paper version we looked at three things, the current result (one), the active operator (Plus) and the new value to be added (two). Step. By. Step, what happens first? The calculator just sits there until the user presses the '2' key. When that happens, it's another Input, lemme write that down:

Input result
Input operator
Input number


Now we have all three parts, the result we want to add to, an operator that tells us to add, and a number to add to the result. IF the operator is a Plus -- that's a Conditional -- then add the number we just got to the running result. Do you believe that? It's exactly what we did in the pencil-and-paper version.

Input result
Input operator
Input number
If the operation is "+" then
  let result = result + number


The other thing we saw in the pencil version is all these repetitive Plus-number, plus-number, plus-number, which looked like an iteration. We could just as easily have seen it as "number-operator..." or "operator-number...". The program comes out slightly different in structure, but basically works just the same. If you want to do this professionally, it would be a good exercise to try it both ways.

However, since we started out with the operator first, we need to build our iteration around the two inputs followed by the conditional. As you recall (or you can review the Reference section of the IDE page) the correct form of the English Computer iteration is the command "Repeat" in front of whatever is to be repeated, and "Next" after it, like this:

Input result
Repeat {some}
  Input operator
  Input number
  If the operation is "+" then
    let result = result + number
  Next iteration


You know, what started out as a few comments about how we understood the calculator functionality has turned into a good English description of what our calculator program should do. Do you think it will run? What's missing? If you look over at the list of the "5 Concepts," what's in that list but not in our English calculator description? We have seven lines of sequence, three variables, one iteration and one conditional, and three Inputs, but no Outputs. Look at the calculator above; do you see output there? What is on display?

Right. One line, after the calculation, but before it goes back for more input, to display the calculated result:

Input result
Repeat {some}
  Input operator
  Input number
  If the operation is "+" then
    let result = result + number
  Display the result
  Next iteration


OK, Let's copy this program [Select all eight lines & Copy, then go to the English IDE] and paste it into the program panel of the IDE, then click on the Run button and see what happens.

"Your Mission, should you choose to accept it," is to stay there in the English IDE and (erase, then) rethink this calculator, and write your own. I left out subtract, multiply, and divide, but you can put them in. If you watched the video, you can see that the unlabelled numbers on the screen are pretty confusing, I suggest you put a label on each of your outputs to cure that.

You will find it works better if you use commands from the Reference section, especially if you've had prior experience programming: "English" is English, not some variant of Python or JavaScript, so if your program doesn't read like good English, it might not run correctly.

Let us see your work!
 

[2021 August 14]