The computer, however, is not as smart as you are. It cannot understand the same language you use (English perhaps) when you communicate with other people. So, instead of communicating with the computer as if it was a person, you need to communicate with the computer as if it was a machine (imagine that). A computer language!
There are many different computer lanuages. Instead of the phrase "tell the computer what to do", most people use the phrase "program the computer". From this phrase comes the term "programming language", which refers a specific computer language you are using to "program the computer". In this book, we will say "programming language" instead of "computer language". JJ is a programming language. When we refer to the JJ programming language, we will usually say the "JJ language" knowing that you now understand that the JJ language is a language that you use to tell the computer what to do.
The JJ language consist of about three dozen instructions you can use that the computer will understand. You need to decide which instructions (and in what order to arrange these instructions) if you want to tell the computer to do something. Then, you need to enter these instructions into the computer. ("Entering instructions into the computer" usually means "pressing the keys on a keyboard" to form the instructions.) The computer will follow your instructions, in the order in which you present them. Don't make a mistake, because the computer is going to do exactly what you tell it to do.
This is a good thing. "The computer is going to do exactly what you tell it to do." If you tell the computer to turn left, it will turn left. If you tell the computer to turn right, it will turn right...even if the result is turning right and falling over a cliff.
Notice this difference between computers and people. If
you tell the driver of a car to "turn left", but you
are
in the rightmost lane, and
the upcoming street is a oneway going to the right, and
the driver is a good friend and knows that you sometimes say "left" when you actually mean "right"
the driver (person) is very likely going to figure out that you meant
to say "turn right" instead of "turn left".
This is a good thing. People do what they thinks you meant, which is not necessarily what you said. Imagine what the world would be like if people had to be exactly correct all the time. It would be practically impossible to write a book like this, not to mention have a normal conversation with another person.
How do you learn more about the set of JJ instructions you can use to tell the computer what to do? One way it to read this book and follow my instructions exactly (following instructions exactly is sometimes difficult for people to do). At times, you will need to think and create on your own (thinking may or may not be difficult depending on who you are and if you want to; creating is something you were born to do).
What can a computer do? So far, you have learned that if you use a certain set of instructions, the computer is going to do what you tell it to do. What you do not know yet is specifically what the instructions are and specifically what these instructions can have the computer do. You can, for example, have the computer compute (or calculate) arithmetic like a calculator or you can have the computer play a game. You cannot, however, have the computer tie your shoes for you (because the computer you are using probably does not have any fingers). Since the computer you are using is hooked into a monitor (also known as the screen or the display) and has a keyboard (for you to press keys on) and a mouse (for you to point and click with), you are going to learn how to have the computer display stuff onto the monitor screen. Sometimes, what you tell the computer to display will depend on what somebody else typed into the computer or what they pointed and clicked on. Since you are a beginner, there are only about a dozen instructions in the JJ language that this book will tell you about. If you like this book and writing JJ instructions, there is second book that describes the other instructions in the JJ language. The more instructions you learn, the more you can have the computer do for you. The first step, however, is to see if you enjoy having the computer do what you tell it to do when you only have about a dozen instructions in your JJ vocabulary.
With the first JJ instructions you learn, you will be able to tell the computer to display (output) information.
With a few more JJ instructions, your could tell the computer to display (output) different information depending on supplied (input) information. For example, when the computer is running the instructions you gave it, the computer can ask the person sitting in front of the computer to enter (input) the day of the week. In this book, the method to supply input will be pressing keys on the keyboard. With a little more study, you can learn how to use JJ instructions that use a mouse as the method of input.
One example that uses input to display certain output is a
daily reminder ...
If today is Sunday, you can have the computer display a reminder to go back to sleep.
If today is Monday, you can have the computer display a reminder of things to do today.
Here is another example, suggesting directions from one location to another ...
Given two street addresses--a current location and a destination location
--you can have the computer display directions of how to get from
the currently location to the destination location.
You could even have the computer take into account what day and
time it is, since the day and time could play a factor in determining
the directions one would want to travel.
Other examples include numerous games ...
Given certain cards (or dice or random numbers) you can have the computer play
a game.
Yet one final example, an adding machine ...
Given numbers, you can have the computer add the numbers.
Essentially, when the computer is following your instructions, it can do whatever you tell it to do with the input information in order to calculate and display some output information.
INPUT OUTPUT ----- ------ day of the week reminder 2 street addresses directions 2 street addresses, day and time directions cards game score numbers sum total
Output "hello, world"Once you have typed this in, click the CheckIt button to make sure you did not make a typing error--a typo. If you do not have a typo, click the CheckItOut button as the first step to running your instructions, (in this case, we only gave the computer one instruction to run). If you wait a few of seconds, the computer will allow you to start running your instruction by clicking on a Start button.
When the computer starts running your instructions, did you see "hello, world" (without the quotes) displayed?
The output instruction always starts with the word Output. This word is followed with a message that you want to computer to output (display).
Enter the following incorrect instruction
Outout "hello, world"When you click on CheckIt, you will be warned that you have a typo. With certain typos, like the one above, it is easy for the computer to tell you what your mistake is. The word "Outout" is so close to "Output" that the computer is able to help you with more specific information than if you made the following typo
Oyuet "hello, world"or
oouuttppuutt "hello, world"or
Guessing "hello, world"or
Yuck "hello, world"Some typos are even harder for the computer to report accurately than these typos because these typos were all on the first word. There are only a handful of words that belong as the first word, so if the computer is given an incorrect first word it is relatively easy to report accurately.
If you make a typo on the first word of an instruction (even
if you write the word
The funny thing about typos is that sometimes the computer
has no idea that you have made a mistake. Sometimes, as far
as the computer can tell, you have given it correct
instructions...no typos. See what happens, for example, if
you forgot the 'd' in "world"
Of course. If you wanted to have the computer display the
message "hello, worl" instead of "hello, world", this is
exactly how you would tell the computer to do so
These sorts of errors are errors that
the computer cannot help you with. They are called "logic
errors" which means that you told the computer what to do and
the computer did it, but what the computer did is unfortunately not what you thought you had told it to do.
In other words, what you told the computer to do is not logically what you wanted to tell it to
do (though it could be valid instructions that somebody else may want to tell the computer to follow).
Since you did not break any of the rules
of the JJ language (the word Output is indeed followed by a quoted phrase
to display) the computer accepts your instruction and is
prepared to run it should you decide to click on the
CheckItOut and Start buttons. Unfortunately for you, you have an error in
the logic in your JJ instructions.
The sorts of typos that actually break the rules of
the JJ language are called "syntax errors", meaning
that you gave instructions to the computer that do not follow
the syntax rules of the instructions in the JJ language.
Syntax errors are easier to find and fix than logic errors because,
for one, the computer will report that you have a syntax error.
Also, the computer can usually tell you the approximate location
of your syntax error.
On the other hand,
syntax errors can be frustrating as they are a constant reminder
that you need to follow the rules of the JJ language *exactly* if
you want the computer to understand your instructions. If you
find syntax errors frustrating, logic errors are often ten times
as annoying. Logic errors are incredibly time consuming because
unlike syntax erros, the computer will not automatically help you
find your error. The worst part about spending a whole
day finding and fixing a logic error is realizing that the computer
did indeed do exactly what you told it to do instead of what
you wanted to tell it to do. You may want to blame the computer, until
the truth is eventually realized that you made a small mistake.
Now is as good a time as any to warn you that computer programming can
be a humbling experience. And, for those of you that read this
sentence, I can let you in on a little secret: the humbling, once
accepted, makes you a better computer programmer.
Enough about errors (for now). Let's get back to learning
the JJ instructions.
There is a problem with the logic of these two instructions--
the output displays the message
There are many ways to fix this.
The lastest program you wrote, from the final exercise, does more than simply say "hello, world". It
actually comes close to doing something useful. It invites your neighbors to
come over for dinner.
If your name is Wilma or Fred, you can
use your last program to invite your neighbors to dinner
if their names are Betty and Barney.
Ah, you may be asking, What if your name is not Wilma and Fred or what if
your neighbors names are not Betty and Barney?
A much more valuable program would be a program that invites
the neighbors over to dinner regardless what your name is
and what your neighbor's names are.
Since you wrote the program, you could simply change the names
Wilma and Fred to your name and change the names Betty and
Barney to the name of your neighbors. What happens, however,
when somebody else wants to use your "Dinner Invitation" program?
The problem is not
Here is another way to look at this. You will not see software programs
in the store that says, "If your names are Wilma and Fred and
your neighbors names are Betty and Barney, this Dinner Invitation
Program is perfect for you". Or, you will not hear about a popular
web site called www.wilmaandfred.com that runs software programs for you
if your name is Wilma or Fred. Instead, you see programs (both
for sale and for free) that work for anybody, regardless of the
customer's name.
How do software programs work for anybody, not just Wilma and Fred?
There are many approaches to writing software programs that work
for anybody. The approach you are about to learn simply
asks the person running the program for their name.
By asking people for their name and then saving their name
(so you don't have to ask them again), the program can work
for anybody.
How do you write a program that finds out the name of
the person using your program? First,
ask them to enter their name into the computer, using
an Outputln instruction. This part you already know how to do
The idea of storing data in a box (the word "variable" is the
common term instead of the word "box", but we will always use
the word "box" in this book) is a key concept in
computer programming. Boxes are so important that you rarely
need only one box. Soon after the next program, you will
be writing JJ programs that use many boxes.
The next example will use a box and
input from the keyboard to store
the name of the person who is using your program.
The example after that will use two boxes so that the person
running your program can
also input their neighbor's name (in order to
ask them to dinner).
Almost every example in this book will require boxes.
Recall that without input and without boxes we were able
to say "Hello from Fred", but this is not a very valuable program
unless the person running the program happens to be Fred.
At first, you will be the only person running your program.
Soon, however, other students or your instructor will be running
your programs. Eventually, anyone anywhere in the world will be
running your programs on the Internet so it is important for you
to learn how to write a program that anybody in the world can use.
PERSONALIZED GREETING
Here is an example that uses an Input instruction to get the name
of the person running the program and store this name into a box.
First, a Box instruction is needed so that the computer knows that
you have a box, knows what sort of information you are going to put
into the box, and knows what you are calling this box. Since you
will most often have more than one box and both you and the computer
need to keep track of which box is which, you need to give every box
in your program a name.
Later, we will take a closer look at the Box instruction. For now,
keep in mind that there are two pieces of information you need to
tell the computer about any boxes you want to use in your program
After the
Input instruction, this box is used twiced, once in an Output instruction
and once in an Outputln instruction. So, it should makes sense to you that the computer
waited on the Input instruction for the entered input before continuing with
the next instruction. If the computer did not wait, it would not know what
to display as the value of the aBoxForThePersonsName box just two instructions later
in the Output instruction
Notice the difference between the two instructions
This is the whole point. As a programmer, you know that you
need to store a value, but you have no idea what the value
is going to be. Instead, you rely on the computer to store a
value in the box (in this case, with the Input instruction) and sometime later
(sometimes during the very next instruction and sometimes during an instruction
running seconds or minutes later) to retrieve the
value being stored in the box (in this case, when the name of the box is used
in an Output instruction).
Be careful that you did not accidentally tell the computer to
EXERCISE: Using the above "PERSONALIZED GREETING" program as a guide,
write a program that will invite the neighbors to dinner. Make sure the
neighbors know who invited them to dinner. This will require asking
the customer of your program twice for input--once for their name (or names)
and once for the name (or names) of their neighbor(s). You will need
more than one Box instruction and more than one Input instruction.
An example of the final display, after asking for names, is
If you were able to complete the above exercise, you have a
pretty good understanding of the Output, Outputln and Input
instructions. In all these instructions, there is only one
thing to remember (besides the first word of the instruction).
In the Output and Outputln instructions, the word Output or Outputln
is followed with a message to display.
As seen in an above example, the message can be either a
quoted string, or the name of a box if the boc has a value stored in it.
Of the Output and Outputln instructions in the above example, two of
them display the contents of a box named aBoxForThePersonsName, and
the others output a quoted string message:
Although boxes that hold strings are handy for storing practically any information
you would need to store, there are some things that strings are not particularly
good for. For example, strings are not good for doing arithmetic. Arithmetic
is typically done with numbers, not strings. You cannot,
for example, multiply the value of two strings. Even though you see
an example of a string value above that has only numbers in it ("4088675309")
you also see that strings can contain characters other than numbers ("(408)867-5309").
If you (the programmer) knew for sure that a box that stored a string only had numbers in it,
it is reasonable to ask the computer to do some arithmetic for you using that value.
Unfortunately, you (the programmer) cannot easily be certain that the person using your
program is going to input only numbers from the keyboard. Even if you ask somebody
for their phone number and tell them to use
only numbers, somebody someday may enter a '-' or place
'(' and ')' around their area code.
You may be asking, "Why would I want to do arithmetic on a phone number?" which is
a very good question. You probably do not want to do multiplication on
a phone number. There are other sorts of data, however, that you would want to do
arithmetic on. For example, to find the average of a list of numbers, the numbers
are added together and then the sum is divided by the number of numbers. Finding
an average is not only done in math class, it is done in baseball and most sports,
in the stock market, in calculating a students grade point average, and many
other places.
Given that there are situations where a programmer would want to store numbers (possibly for
arithmetic) and other situations where a programmer would want to store strings (possibly for
dinner invitations), programming languages usually ask the programmer to specify what
sort of values are going to be stored in a box, numbers or strings.
So far, we have not been storing numbers in boxes or doing any arithmetic; we have only been
storing strings and displaying these strings with the Output and Outputln instructions.
Given that the word "computer" has as much to do with computing arithmetic as it does with shopping
for concert tickets, you had better learn a little about storing numbers. As it turns out, most
beginning programming books start with examples that use numbers instead of strings, and most
of the programs in the remainder of this book use boxes that store numbers.
There are many differences between using numbers and strings in JJ instructions.
The first difference to learn about is how to get a box that can store numbers instead of a strings.
Recall the typcial Box instruction to get a box that stores a string looks like
By changing the phrase "ofClass Str" to "ofType int", the computer now expects numbers
instead of strings. The word "ofClass" was changed to "ofType" because Str is the
name of a class, and int is a type. The difference between classes and types will
be explained later ... much later. You will soon learn more about ints, and about
other types. It will be a while, however, before you learn more about classes other
than the class named Str.
Your boss at the Penguin's Igloo Ice Cream Parlor (PIICP) asks you to write a program that adds the total
number of chocolate scoops, vanilla scoops and strawberry scoops (PIICP only sells three flavors). You boss
wants the computer to do this simple calculation because there was once a mistake adding these three numbers
and this resulted in accidently underpaying the employees. If you did not
understand the above lesson on boxes and the Input instruction, you might take the wrong approach
to writing the program.
Here is one way to take the wrong approach. Ask your boss how many scoops of
each flavor were sold. After getting a strange look from your boss, you are told
Continuing with this example of taking the wrong approach to writing the program for your boss, you
might not understand why your boss wants you to write a program to add 34, 43 and 26 since you already
said that the total was 103.
Thinking that your boss is a nut, but wanting to show your computer skills, you quickly write the following
JJ program and run it for your boss.
Although this example may seem somewhat contrived, miscommunication between humans can
cause many more wasted hours than what you would expect from this example. Imagine working
many months, even years, on a program that turns out not to be what it was expected to be.
Should you decide to continue with computer programming, you may find that misunderstanding
another human will cost far more time than tracking down syntax and logic errors in your programs.
Now, let us continue with the ice cream example.
Without an answer for your boss, you return to this book, first reviewing the previous section on boxes
and keyboard input, then taking a look at the following example
Some of the arithmetic you can do with numbers and boxes that store numbers (that is, boxes of type int)
include
Parenthesis are used to show which arithmetic operations you want to do first.
For example
Recall that you are still learning about the Box instruction, and have now learned
about strings and integers. An important aspect of boxes that has not yet been
discussed is choosing the correct names for your boxes.
The above names for the boxes--totalChocolateScoops,
totalVanillaScoops and totalStawberryScoops--are quite description, but your fingers
may get tired entering these names over and over in the instructions (unless you are
a good typer or know how to use copy-and-paste). You are allowed to name boxes
whatever you want, as long as you start with a letter ('a' through 'z' or 'A' through 'Z')
and use only letters, digits ('0' through '9') and underscores ('_'), and the final
character is not an underscore. (Notice that you
cannot use blanks in the name of a box.)
What sorts of names should you use for your boxes? It depends on the situation. The
above program is something that your boss at the Penguin's Igloo Ice Cream Parlor could probably read and
almost understand (because the names of your boxes are so descriptive).
Another strategy is to use short names for your boxes, sometimes as short as one letter long.
Here is the same program written above, but using different names for the boxes.
There is a strategy that allows the best of both worlds (a long
description for box names and short box names that are easier
to write and read). The JJ language
allows the programmer (you) to write additional information for the
reader (sometimes you, sometimes your instructor or another student
or somebody you work with)
to help them understand what your instructions are doing. This additional
information can be placed at the end of an instruction, or on a line
all by itself. Whether on their own line or at the end of an instruction,
this additional information is referred to as a "comment". Comments
begin with the two characters '--'.
Here is a third version of the above program
Why not use completely descriptive names? Maybe after you have learned how
to program, you will prefer to use long names. For now, the problem with
long names is they increase the likelyhood of a typo, and as a beginner you
have enough to learn without this extra distraction.
Misspelling a box name could be a syntax error (you broke the
rules of the JJ language), but it could also be a logical error (you
did not break the rules of the JJ language, but the computer did
not do what you wanted it to do). If, for example, you had many
box names that looked the same
Consider phone numbers. In about 20 years, there may be a need for longer
phone numbers. In America, area codes are only three digits, which means there can
only be 999 area codes. Since they cannot start with a zero or a one, there
are even less. Considering there used to be only one area code in Los Angeles
and there are now 12, and that seeing people with phones in their car used to be
rare and now it is quite common, it is only a matter of time before America's
phone number system (three digits for the area code and seven digits for the
phone number) has to change. At that time, many computer programs will need
to be examined and possibly rewritten.
Compared to
Yuck when you meant to write
the word
Output hello, world
You see that the reported message is not as helpful as when you tried
your first typo (spelling Outout instead of Output). This is
because the computer can no longer tell what your typo really is.
Once you learn about the Box instruction you will try this
example again and see what "the computer thought" you
were trying to tell it to do.
Output "hello, worl"
Clicking the CheckIt button, you see that the computer
thinks that everything is ok. Then, clicking the CheckItOut button,
you see that the computer is going to do exactly what you told
it to do--it outputs the message "hello, worl"!
Output "hello, worl"
There is absolutely no way for the computer to know that
you have made a typo. If the computer did not accept this
instruction, there may be no way to have the computer
output the message "hello, worl".
A SEQUENCE OF INSTRUCTIONS
You give instructions to the computer and if there
are no typos the computer can run these instructions. So far
we have only given one instruction to the computer. Let's
give two instructions to the computer and see what happens
Output "hello, world"
Output "from Fred"
If you did not make any syntax errors (typos), when you start running
these instructions (by clicking on the CheckItOut and Start buttons) you see
that the computer runs them from top to bottom. In this case, it runs the first
Output instruction, displaying the message "hello, world",
then it runs the second instruction, displaying the
message "from Fred". Take a close look at the result. Something is wrong.
hello, worldfrom Fred
instead of the more desirable message
hello, world from Fred
EXERCISE: Fix this logical error. You need to somehow
put a blank between the word "world" and the word "from".
END EXERCISE
Now that you have the correct display of "hello, world from Fred",
some of you may be wondering how you would tell the computer to
display this message on two lines instead of one. That is, it
may be nice if instead of the display message looking like
hello, world from Fred
it would look better as
hello, world
from Fred
This is accomplished using a different instruction, an instruction
that outputs a line as well as the message--the Outputln instruction.
Outputln "hello, world"
Outputln "from Fred"
Comparing the syntax rules for the Output and Outputln instructions, the
only difference is the two characters 'ln' at the end of the word "Output".
To output a line after your message, use the word Outputln instead of the word Output.
(The 'ln' is an abbreviation for the word "line".)
The Output instruction outputs a message but stays on the same line and
the Outputln instruction outputs a message and moves down to the next line.
EXERCISE: Add instructions to the one instruction
Output "Dear"
so that the computer displays the message
Dear Betty and Barney,
We invite you to join us for dinner.
Your neighbors,
Wilma and Fred
Part of this exercise is that you are not allowed to
modify the first instruction
Output "Dear"
HINT: You need to add at least four more instructions.
EXTRA: If you make any errors, keep track of each error
and determine if it was a syntax error or a logical error.
END EXERCISE
Before completing this first section, there is another term for
you to learn. When the computer is running the instructions you
gave it, the term used for "the instructions" is "the program".
Whether you give the computer only one instruction or many instructions,
these instructions are referred to as a "program". In other words,
a set of instructions makeup a program, and the process of
creating these instructions is called "programming". If you have
written a program that has any syntax errors, the computer will
report to you (as accurately as possible) what and where your
syntax errors are. Once your program is free of syntax errors,
you can run your program. When your program is running, the computer
is doing exactly what you told it to do, so if there are any logic
errors in your program it is up to you to find and fix these errors.
SUMMARY
So far, you have written six programs. The output, from the
first program to the sixth program is
Each of these programs consist of a set of JJ instructions.
In the first four programs, there is only one instruction in the
program, the Output instruction. In the fifth and sixth programs, there are
more than one instruction in the program.
from Fred
We invite you to join us for dinner.
Your neighbors,
Wilma and Fred
TIME FOR A BREAK
If you have not yet taken a break, now is a good time. You have learned
two JJ instructions, Output and Outputln, and you have learned a little
about typos (syntax errors) and a little about logic errors (when the computer is
doing what you told it to do, but this is not what you thought you had
told the computer to do). Next, we are going to have the computer
compute some output based on some input.
READING INPUT AND STORING INPUT IN A BOX
So far, we have learned how to tell the computer to
display a message. Pretty easy, is it not? Simply write
the word Output or Outputln(exactly) followed by a message
inside of quotes, such as ...
Output "hello, world"
There is a reason, however, why
programming is not thought of as being easy. You are
about to see why programming is difficult for other people.
This is why you were told to take a break at the end of the
previous section, so you would be rested and ready for the next step.
How do you, the programmer, make sure that your name is
accurately used in the program?
Instead, the problem is
How do you, the programmer, write a program that anybody can
use (without having to ask you to rewrite the program so it
has their specific name in it)?
You see, the average person does not know how to program a computer.
Instead of writing their own programs, they use somebody
elses programs. The average computer user will
either reply on
the old method of purchasing a program from somebody else or
they will (save money and) use
the more modern method of using a free program found on
the Internet. The important point is that people use
programs that do not contain instructions which have specific
names in them such as "Wilma and Fred" or "Betty and Barney".
Outputln "Enter your name:"
Then, use an instruction that will
read in the data (their name) that they enter as input--
the Input instruction. The Input instruction will read in
entered input and store it into a box.
Why do you need to store it in a box? The reason you need to
store the input from the user into a box is
so you can use their name again and again without having to ask
them again and again to enter their name.
The below program uses only one box, referred to as aBoxForThePersonsName,
and the sort of data you can put into the box is ofClass Str
which will be explained soon
Box aBoxForThePersonsName ofClass Str
Outputln "Enter your name:"
Input aBoxForThePersonsName
Output "Hello "
Output aBoxForThePersonsName
Outputln "."
Outputln "Quite a pleasure to meet you."
Output "Stick around because this program "
Outputln "will get a lot more interesting."
Output "Goodbye "
Outputln aBoxForThePersonsName
Run this program. The first instruction tells the computer about
the box you want to use. The second instruction, the
Outputln instruction, prompts the person
running the program (currently the person is you, but it could be somebody
else) for their name. The next instruction the
computer runs is the Input instruction. The
computer will wait here until some data is input.
This is very important, the computer will wait here until
some data is input.
Once some data is input, the computer knows how to
put it into the box named aBoxForThePersonsName since that is
the name of the box you placed in the above Input instruction
Input aBoxForThePersonsName
Output aBoxForThePersonsName
Output "Hello "
and
Output aBoxForThePersonsName
The latter instruction refers to a box
instead of supplying a quoted string. The computer
will display a quoted string (in the above example, "Hello ")
exactly as you, the programmer, tells it to. When the
computer displays the value in a box (in the above example,
the name of the person running your program), the computer
knows what the value in the box is. Ironically, the programmer
usually does not know what the value in the box is. Of course,
if people all around the world are going to use the program you wrote,
there is no way for you to predict the names of all the people who
may use your program.
Output "aBoxForThePersonsName"
instead of
Output aBoxForThePersonsName
The first of these Output instructions put the name of the box
in a quoted message, and we know very well what the computer is
going to do with a quoted message in an Output or Outputln instruction.
It will output the message
aBoxForThePersonsName
and the second of these Output instructions will output the appropriate message
??? (we cannot tell ... whatever value happens to be in the box)
Now that you have seen a box and input, you are ready to write the
improved Dinner Invitation Program; one that does not require the customers
name to be Fred, Wilma, Betty or Barney. You know you are not on the right track
if any of these names ("Fred", "Wilma", "Betty" or "Barney") appear in your instructions.
Dear _____________,
I invite you to join me for dinner.
Your neighbor,
_______________
HINTS: The sort of value for both of your boxes is ofClass Str.
So, your box instructions will look similar to the one above
Box aBoxForThePersonsName ofClass Str
The names of the two boxes need to be different, for example
you may want to use the two box names myName and neighborsName,
or name1 and name2
EXTRA: If you make any errors, write each one down and
determine if it was a syntax error or a logical error.
END EXERCISE
Outputln "Enter your name:"
Output "Hello "
Output aBoxForThePersonsName
Outputln "."
Outputln "Quite a pleasure to meet you."
Output "Stick around because this program "
Outputln "will get a lot more interesting."
Output "Goodbye "
Outputln aBoxForThePersonsName
In the Input instruction, the word Input is followed with the
name of a box. In the one Input instruction from the above
example, the input value supplied by the person running the
program will be stored into a box named aBoxForThePersonsName.
Input aBoxForThePersonsName
The box instruction is not as simple as the Output, Outputln and Input
instructions. After the word Box, the box instruction requires two
pieces of information. The next section is dedicated to explaining
the Box instruction.
BOXES AND THEIR VALUES
So far, our Box instructions have had the following phrase 'ofClass Str'; a phrase that
has not yet been explained. The three letters 'Str' is an abbreviation
for the word "string". We first used the word string above when referring to
the quoted string "Hello " (notice that there is a blank after the word Hello).
Now, we are again using the term string, but shortening it to Str.
Recall that there are two pieces of information the computer needs to know about
every box:
The first piece of information, the name of the box, you place immediately after
the word Box. The second piece of information, the sort of data that the box is
going to store, follows the name of the box.
For example:
Box myName ofClass Str
Box neighborsName ofClass Str
Box neighborsAddress ofClass Str
Box neighborsPhoneNumber ofClass Str
Box neighborsNickname ofClass Str
In each of these five Box instructions, the box will store a string (ofClass Str).
Notice that the value
placed in the box named neighborsAddress is likely to have blanks
in it since most addresses consist of a street number, a street name, and
possibly an apartment number. For example:
"123 Main Street"
"59732 2nd Str #103"
If you have been reading this book from the beginning, this is not the
first time you are seeing the need for a blank in a string.
The box neighborsPhoneNumber will have
numbers in it, and could have other characters such as '(' and ')'
and '-'. For example
"4088675309"
"(408)867-5309"
Boxes that store strings
are very popular because, as you can see, they may contain
letters ('a' to 'z' and 'A' to 'Z'), numbers ('0' to '9') and
other characters ('#', '.', ',', '(', ')' and '-' to name a few) which are all useful for things
like peoples names, addresses, phone numbers and practically anything.
Whatever can be entered from the keyboard and displays on the computer monitor as a
single character can be placed in a string. An example of a key on the keyboard
that cannot be used in a string (until you read the more advanced book) is the
ENTER key (a.k.a. a carraige return) since it does not display on the computer
monitor as a single character.
Box myName ofClass Str
The name of the box, myName, is used in other insructions to either put a string value
into the box or get the current string value out of the box. To get a box that stores
numbers instead of strings, simply change the last part of the instruction from
ofClass Str to ofType int as in
Box myHeight ofType int
The three letters 'int' is an abbreviation for the word "integer". The word integer
refers to a whole number, possibly negative, zero, or positive.
Recall (again) that there are two pieces of information the computer needs to know about
every box:
In the above Box instruction, the name of the box is myHeight and the sort of data
you can put in this box is integers (whole numbers).
BOXES FOR NUMBERS
To learn more about boxes that store numbers, an example scenario is presented.
34 chocolate scoops
43 vanilla scoops
26 strawberry scoops
You might tell your boss that the total number of scoops is 103. Your boss thanks you, but after a second
strange look, asks you to write a program to do this calculation because humans make errors and computers
are built to compute numbers. Since you are learning about computers, you are supposed to be able to
help your boss by writing this program.
Output 34+43+26
You already knew about the Output instruction and by skipping ahead in this book you saw that numbers
can be added using the plus sign ('+'). Expecting some positive feedback, your boss surprises you and
says sarcastically how nice it is that your program magically added the scoops without having to ask
how many of each flavor were sold. What happens, you are asked, if tomorrow the sales figures are
37 chocolate scoops
39 vanilla scoops
25 strawberry scoops
and, if the next day the programmer is not at work, and the sales figures are
38 chocolate scoops
33 vanilla scoops
29 strawberry scoops
If the same program is run tomorrow, it will still report the total as 103, but the total has
changed to 101.
Box age ofType int
Outputln "Enter your age ..."
Input age
Output "If you survive, next year you will be "
Outputln age+1
Now you begin to understand the idea of boxes and input a little more. This program will work for
anybody, no matter what their age is. No new JJ instructions had to be explained. The only new
features are
As mentioned above, an int (short for integer) refers to whole numbers such as 37, 25, 143597, 0, -1, -55, etc.
Integers are used when you are counting things, or when you want to do arithmetic.
You cannot do arithmetic on strings.
addition with '+'
subtraction with '-'
multiplication with '*', and
division with '/'.
For example,
Output 3 + 4 - 5
will display the value 2, and
Output 4 - 5
will display the value -1, and
Output 3 * 4 * 5 / 6
will display the value 10
Output (3 + 4) * 5
will do the addition(+) before the multiplication(*) and display the value 35, and
Output 3 + (4 * 5)
will do the multiplication(*) before the addition(+) and display the value 23.
EXERCISE
Modify the above program that asked for an age so it displays:
(1) how old the person running your program was last year and
(2) approximately how old they will be when they die if they have lived one
quarter of their life so far.
ENDEXERCISE
Returning to the ice cream scoops adding machine, to
write a program that adds the number of Chocolate, Vanilla
and Strawberry scoops, you need to
Here is one solution
Box totalChocolateScoops ofType int
Box totalVanillaScoops ofType int
Box totalStawberryScoops ofType int
Output "Enter the number of chocolate scoops"
Input totalChocolateScoops
Output "Enter the number of vanilla scoops"
Input totalVanillaScoops
Output "Enter the number of strawberry scoops"
Input totalStawberryScoops
Output "The total number of scoops is "
Outputln totalChocolateScoops + totalVanillaScoops + totalStawberryScoops
Notice that blank lines are used in between the instructions in places where it
helps the readability of the program. A blank line is used after the Box instructions and before the
the Input and Output instructions (that
gather the required information from the person running the program),
after the Input and Output instructions and before
the Output and Outputln instructions (that display the result).
Box c ofType int
Box v ofType int
Box s ofType int
Output "Enter the number of chocolate scoops"
Input c
Output "Enter the number of vanilla scoops"
Input v
Output "Enter the number of strawberry scoops"
Input s
Output "The total number of scoops is "
Outputln c + v + s
Which program do you think would be easier for your boss to read?
Some would argue that the first example is more understandable.
The problem with the first example is that you are more likely
to make a typo when the box names are so long. The second
example is surely shorter and easier to type. The problem with
the second example, however, is that the first 3 instructions and
the last instruction supply little information to the reader about
what c, v and s refer to.
Box c ofType int -- c is for chocolate
Box v ofType int -- v is for vanilla
Box s ofType int -- s if for strawberry
-- gather the input
Output "Enter the number of chocolate scoops"
Input c
Output "Enter the number of vanilla scoops"
Input v
Output "Enter the number of strawberry scoops"
Input s
-- report the total number of scoops
Output "The total number of scoops is "
Outputln c + v + s
Notice how comments, blanks lines and short names for boxes are used to
help both the programmer and the reader.
This may look like our best strategy, but this particular program is quite short--eleven
instructions. If there were hundreds of instructions and dozens of boxes,
one letter box names would not be the best strategy. The final instruction
in the above example
Outputln c + v + s
is understandable because you can see the comments on the Box instructions
just a few lines above it. The letters c, v and s would be vague
if there were many other boxes and many more instructions separating this
final instruction from their Box instructions. You could, of course,
decide to comment this final line, changing it to
Outputln c + v + s -- chocolate, vanilla and strawberry
but then it looks similar in length to the original version of this program
which looked like
Outputln totalChocolateScoops + totalVanillaScoops + totalStawberryScoops
The final advice (for now) on strategy for box names is to use a name
longer than one letter if there are any references to this name more
than one screen away from the Box instruction where the name first appears.
The longer name does not, however, need to be completely descriptive
(as in totalChocolateScoops). It can be an abbreviation, long enough to
act as a reminder of what the box is for (for example, choc, van and straw).
ERROR EXAMPLE
What happens when you press the CheckIt button on a program that has an
instruction which misspells the name of a box? The above describes a strategy
to help minimize this possibility, but misspelling a box name eventually
happens to every programmer, so now is the appropriate time to prepare.
Try the following
incorrect program
Box suchALongNameForABox ofType int
Output "Enter your favorite number:"
Input suchALongNameForABo --missing the final letter ('x')
Output "Your favorite number squared is "
Outputln suchALongNameForaBox * suchALongNameForaBox
You see how the error message may be confusing? It would especially be
confusing if you were too busy thinking about how to solve a problem
to be able to stop, read and understand the error message.
This example only had one box so that the error would be obvious.
Remember that most of your future programs will have many boxes
and many instructions so the error, a misspelled box name, may not be
so obvious.
Box totalCScoops ofType int -- C is for chocolate
Box totalVScoops ofType int -- V = vanilla
Box totalSScoops ofType int -- S = strawberry
and then you misspelled one of them, accidently typing a C instead
of a V (they happen to be right next to each other on most keyboards),
the computer cannot help you with any error messages. For example,
consider the below typo, something the computer cannot detect
Output "The total number of scoops is "
Outputln totalCScoops + totalCScoops + totalSScoops
Instead, the displayed total is usually not correct. You could probably figure
this out if you test your program before giving it to your boss. If
you happen to test it with a poorly chosen example, such as
33 chocolate scoops
33 vanilla scoops
33 strawberry scoops
you will not discover your misspelled box error. Even worse, the first
day your boss runs your program, if the input is
27 chocolate scoops
27 vanilla scoops
22 strawberry scoops
your boss may think that your program is completely accurate. Afterall,
your boss is not the one taking the computer class and if the program
works the first time it must work all the time. Wrong. Some programs that have been
working for over thirty years are actually incorrect. On January, 1st, 2000, programs
that used only two digits to store the year (such as 73 for 1973 and 99
for 1999) may or may not have worked incorrectly. This particular bug was so
predictable, that it was given a name--the Y2K bug.
ASIDE
The Y2K bug is only the first of many famous computer bugs that will set
certain amounts of fear and panic through the world. Can you think of other
examples where programs that have worked for many years will no longer work?
BACK TO THE UNCAUGHT TYPO
You boss will truly be mad once it is discovered that your program is not
accurately adding the total number of scoops. In the above scenario, this
might happen on the second day your program is used.
Output "The total number of scoops is "
Outputln totalCScoops + totalCScoops + totalSScoops
would you have been able to see your typing error if it looked like
Output "The total number of scoops is "
Outputln c + c + s
How about
Output "The total number of scoops is "
Outputln choc + choc + straw
Again, it looks like the short-but-not-too-short strategy for naming boxes
is the best strategy.
Below, are some notes that David wants to use to continue from here ...
SUMMARY
Use description names for boxes, but not so long that it is difficult to
read and difficult to find errors should you need to read your own program
years after you wrote it.
MORE ON BOX NAMES
Did you notice that you can call your box names whatever you want?
Obviously, the best names make the most sense to anybody that may read your program
while not, at the same time, making it difficult to read your instructions because
your box names are too long.
VERY BAD NAMES
Box lateForDinner
Box tillTheCowsComeHome
Box toeJam
show example where the names mean absolutely nothing
another that shows misspells
Box totol
On to REALS and getting the AVERAGE
THE COMPUTER CAN CHECK FOR YOU (try putting a string into an int box, vice-versa)
NEED INT
(later, need REAL)