You are on page 1of 26

Big Idea:

Jeroo is a text-based coding


environment designed to help
novices master the basic mechanics
of object oriented programming. It
features a limited syntax to
emphasize basic computational
thinking skills like problem
decomposition (ie, breaking things
down into subtasks), the semantics
of control structures like loops and
ifs, and use of objects and state to
solve problems.
Solving problems involves writing a
program that helps one or more
kangaroo-like objects (ie, Jeroos)
hop around an island while avoiding
the nets set by animal collectors to
achieve an objectiv
Lesson 1: Introduction to Jeroo

Santong island is an uninhabited speck of land in the South Pacific Ocean. In the late 1980's, naturalist
Jessica Tong discovered two previously unknown species that are unique to this island. One is the jeroo, a
rare mammal similar to the wallabies of Australia. The other is the large winsum flower that is the jeroo's
primary food source.
Like its distant Australian relative, the jeroo moves by hopping, but its movements are unique in all of
nature. Each jeroo only moves in one of the four main compass directions, and only turns in 90 degree
increments. This unusual behavior continues to mystify Jessica and the few other researchers who have
observed jeroos in the wild. Some believe that this behavior is somehow related to geomagnetic forces,
but others think that this is just a bizarre learned behavior.
Every jeroo has a pouch, but the male's pouch is somewhat smaller. While the female uses her pouch to
protect and nuture her offspring, both sexes use their pouches to carry winsum flowers that they have
picked.
During the rainy season, January through December, sudden storms can create temporary lakes and
rivers, or even alter the shape of the island itself. Despite living on Santong island for uncounted
millennia, the jeroos are extremely poor swimmers. While they are keenly aware of their immediate
surroundings, jeroos can be careless. If one happens to hop into the ocean, its fur becomes waterlogged
and it must rest in the sun for a few hours before resuming its normal activity. In what can only be
described as a telepathic connection, all other jeroos also cease their activities until the wet jeroo has
recovered.
Until recently, the jeroos were safe from human interference because they were only known to a handful
of researchers and because Santong island is very small, very remote, and missing from most maps. The
jeroo's idyllic world was interrupted in 2001, when Brian arrived at the island. Brian is a renowned
collector who was hired to capture a jeroo for the extreme animal exhibit at Missouri's C. A. Baret zoo.
Having studied the jeroos' unique patterns of movement, Brian periodically sets nets in the locations that
a jeroo is likely to visit. Fortunately, the sensitive jeroos can detect his nets, and have learned that tossing
a winsum flower onto a net will disable the net and make that location safe for future travel. Brian can
only catch a careless jeroo that leaps before it looks.
What is Jeroo?

Jeroo is a text-based coding environment designed to help novices master the basic mechanics of object
oriented programming. It features a limited syntax to emphasize basic computational thinking skills like
problem decomposition (ie, breaking things down into subtasks), the semantics of control structures like
loops and ifs, and use of objects and state to solve problems.

Solving problems involves writing a program that helps one or more kangaroo-like objects (ie, Jeroos) hop
around an island while avoiding the nets set by animal collectors to achieve an objective. Along the way,
Jeroos can pick up flowers growing on the island and use them to disarm nets. Jeroos must be careful to
also stay out of the water, since they've never developed an ability to swim.

Lesson 2: Form the first Jeroo Program

Methods are the primary components of a Jeroo program. Every Jeroo program musthave
exactly one main method, and it may contain any number of programmer-defined Jeroo
methods.

The main method describes how to use one or more Jeroos to solve a specific problem. Theform of
the main method is shown in figure 4.1. Everything that appears between the braces should be
indented 2 or 3 spaces.
Empty
parenthesesare

method main()

Body of the method

Declare one or more Jeroos.


Braces mark the
beginning and
endof the body. Write a sequence of statements that
describe how to use the Jeroos to solve a
specific problem.

} //===== end of method main() =====

Form of the main section


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0
1
2 Cell (4,5)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
The Model for Santong Island
Lesson 3&4: Declaring and Instantiating Jeroos

Every Jeroo program uses from one to four Jeroo objects to solve a problem. The first part
of the main method must be used to declare and instantiate all the Jeroos that will beused
by the program. The syntax of this statement is shown in figure 4.2. There are two parts to
the statement, the declaration and the instantiation.

Declaration Instantiation

name = Jeroo(...)

Syntax for Declaring and Instantiating a Jeroo

Declaration Portion
The declaration portion indicates that the programmer plans to use a Jeroo to help
solve the problem. The programmer must provide an identifier (or name) for the Jeroo
object. The following box contains the rules for creating identifiers in Jeroo’s
programming language.

 The first character must be a letter or an underscore (_).


 The remaining characters can be letters, digits, or underscores.
 There is no limit on the length of an identifier.

Instantiation Portion
The instantiation portion is a request that the Jeroo object be created. The crucial
part of the instantiation is the constructor, which has the form Jeroo(...). The
parentheses are to be filled in with initial values of the attributes for that Jeroo object.
Every Jeroo object has three attributes: its location, its direction, and the number of
flowers in its pouch. If we choose not to specify an initial value for one or more of the
attributes, the constructor provides default values as shown in table 4.1.

Attribute Default Value


Location (0,0)
Direction EAST
Flowers 0

Default Attributes

As a convenience, the Jeroo language contains six different constructors, all but one of which
allows us to provide values for just some of the attributes. Default values will be provided for
the others. The constructors are summarized in following table.

Example Attributes
#---Accept all defaults --- Name: Jessica
Location: (0,0)
jessica = Jeroo() Direction: EAST
Flowers: 0
#--- Specify just the flowers --- Name: jessica
Location: (0,0)
jessica = Jeroo(8) Direction: EAST
Flowers: 8
#--- Specify just the location --- Name: jessica
Location: (3,4)
jessica = Jeroo(3,4) Direction: EAST
Flowers: 0
#--- Specify location and direction --- Name: jessica
Location: (3,4)
jessica = Jeroo(3,4,WEST) Direction: WEST
Flowers: 0
#--- Specify location and flowers --- Name: jessica
Location: (3,4)
jessica = Jeroo(3,4,8) Direction: EAST
Flowers: 8
#--- Specify all attributes --- Name: jessica
Location: (3,4)
jessica = Jeroo(3,4,SOUTH,8) Direction: SOUTH
Flowers: 8

Declaration and Instantiation Examples

Lesson 5&6: Jeroo Action Methods

An action statement is a request that a Jeroo perform a specific task. That task can be either
one of the basic action methods that are part of the Jeroo language or a Jeroo method that
has been written by the programmer. The syntax of an action statement is shown in figure
Period Parenthese

jeroo_identifier.method_name(...)

Which Jeroo? What Argument value

Syntax of an Action Statement


The Jeroo language includes the seven action methods shown in the following table .Three of
these, give, turn, and one version of hop require an argument value.

Method Purpose Example


hop() Hop one space ahead. jessica.hop()
The program terminates with an error
condition if the hopping Jeroo lands
in the water, lands on another Jeroo,
or hops onto a net.
A Jeroo can hop onto a flower.
hop(number) Hop number times in a row, where jessica.hop(3)
number is a positive integer. jessica.hop(12)
pick() Pick a flower from the current jessica.pick()
location. Nothing happens if there is
no flower at the current location.
plant() Plant a flower at the current location. jessica.plant()
Nothing happens if the jeroo does not
have a flower to plant.
toss() Toss a flower one space ahead. jessica.toss()
The tossed flower is lost forever.
If the flower lands on a net, the net is
disabled.
turn(relativeDirection) Turn in the indicated direction jessica.turn(LEFT);
[ turn(AHEAD) and turn(HERE) are jessica.turn(RIGHT);
meaningless ]

give(relativeDirection) Give a flower to a neighboring Jeroo jessica.give(LEFT);


in the indicated direction. Nothing jessica.give(RIGHT);
happens if the giving Jeroo has no jessica.give(AHEAD);
flowers, or if there is no neighboring
Jeroo in the indicated direction.
[ give(HERE) is meaningless ]

Direction
Each Jeroo knows four relative directions and four compass directions as shown in the following table.
Each direction must be written in UPPER CASE (In Java, constant values that never change are usually
written this way). The relative direction HERE refers to the Jeroo's current location, so it isn't useful with
the action methods in the previous section (we'll see other methods later where HERE makes sense).

Relative Directions Compass Directions

LEFT NORTH

RIGHT EAST

AHEAD SOUTH
HERE WEST

Attributes
An attribute is a fact associated with a specific object.

The attributes of a Jeroo object include its location, the number of flowers in its pouch, and the direction
it's facing. When we instantiate a class, the class' constructor is responsible for initializing the new
object's attributes. So when we create a Jeroo object, its constructor is responsible for initializing its
location, the number of flowers in its pouch, and the direction it's facing. If we do not specify a value for
one (or more) of the attributes when we create the jeroo, the constructor provides defaults for us.

As a convenience, the Jeroo class provides multiple constructors so that we can omit (or provide) values
we want for any of these attributes. All but one of the constructors allow us to provide values for just
some of the attributes. The constructors are summarized in the following table:
Lesson 7&8: Creating and Using Jeroo Methods

Defining a Behavior
A behavior is an action that an object can take or a task that it can
perform in response to a request from an external source. A method is a
collection of statements that are written in some programming language to
describe a specific behavior.
These definitions imply that the creation of a method is a two-part process.
First, we need to define and name the new behavior. Second, we need to write
the source code for the method.
Writing a Jeroo Method
A Jeroo method contains the source code that describes what an arbitrary Jeroo
needs to do to carry out the corresponding behavior. The form of a Jeroo method is
shown in figure below,for Jeroo’s Python-style language. The method identifier on the
first line (the header line) is a name that the programmer chooses for the method.
The name should indicate the corresponding behavior.
Form of a Python-Style Jeroo Method

“self” and the colon


are required
def method_identifier(self):

Body of the method


The body and the
Write a sequence of statements that closing comment
describe how an arbitrary Jeroo can must be
carry out the corresponding behavior. indented

#===== End method_identifier() =====

There is one important difference between a Jeroo method and the


main section. Since a Jeroo method defines a behavior that applies to
every Jeroo, we cannot send a message to a specific Jeroo. Instead, we
specify that self is to perform the steps. When the program is running, the
keyword self is translated to be identical to the Jeroo that is performing
the entire method.

Using a Jeroo Method


A Jeroo method is used just like any other method. In the main section,
we send a message to a specific Jeroo object, requesting that Jeroo to
perform the task associated with the method.
Example
Let’s have Jeroo Ali plant two rows of flowers, south and east of (5,5)
method main()
{
Jeroo Ali = new

Jeroo(5,5,8);

Ali.plantRowsOfFour();

} //===== end method main() =====

Preconditions and Postconditions


We should always define a behavior carefully before we write the code
for the corresponding method. A complete definition for a behavior must
include a statement of the preconditions and the postconditions.
A precondition for a method is something that is assumed to be true
before the method is invoked. The portion of the code that invokes the
method is responsible for ensuring that all preconditions are satisfied
before the method is invoked.
A postcondition for a method is something that is true after the
method has been executed. The code within the method is responsible
for ensuring that all postconditions are met.

Precondition Questions Postcondition Questions


Do any of the attributes of the receiving How does this method affect the attributes
object need to have specific values? of the receiving object?
Location Location
Direction Direction
Flowers Flowers
Are the contents of certain island cells Have the contents of any island cells
important? changed?
Lesson 9&10: Control Structures: While Loop

Every repetition structure (or loop) allows a group of statements to be executed several times in
succession. There are two important selection structures: a loop that is controlled by the state of the
objects in the program, and a loop that is controlled by a counter.
WHILE
Condition Action
true Make a trip through body
false Terminate the loop
Repetition Structures in Jeroo
A pretest while loop is the only repetition structure in the Jeroo language. Figure 6.4
shows the syntax of the pretest while loop in Jeroo’s Java/C++/C#-style language.

There are three important things to observe about the syntax.


1. The condition must be in parentheses.
2. There is no semicolon after the parentheses.
3. The while structure is not a method, which means that we do not send
it as amessage to a Jeroo object.

There are three important things to observe about the coding style.
1. Braces are used to define the beginning and end of the body.
2. The braces are aligned with the start of the word while.

while( condition ) No semicolon

{
//statements that comprise the body of the loop
3. The statements between the braces should be indented 2 or 3 spaces.
EXAMPLE (pretest while structure)
Assume that a Jeroo named Kim is not standing on a flower, but there is a line of
flowers ahead. Have Kim pick all of those flowers, and stop as soon as there is no
flower directly ahead. After picking all of the flowers, Kim should turn to the left.

while( Kim.isFlower(AHEAD) )
{

Kim.hop();

Kim.pick();
}
Lesson 11&12: Control Structures: IF condition

The Jeroo languagesupports the basic control structures shown in table below. Control
structures can be nested withinone another in whatever way is suitable for completing a
particular task. Parentheses are required where they are shown.

Control Structure Syntax (short form) Syntax (block form)


if if ( condition ) if ( condition )
single_statement; {
one or more statements
}

if-else if ( condition ) if ( condition )


single_statement; {
else one or more statements
single_statement; }
else
{
one or more statements
}

multi-way branch if ( condition ) if ( condition )


single statement; {
Technically, this is a else if ( condition ) one or more statements
nested if, but this is a single statement; }
common form for defining else if ( condition )
three or more alternatives. more else if branches {
if necessary one or more statements
}
else
single statement; more else if branches
if necessary

else
{
one or more statements
}
while while ( condition ) while ( condition )
single_statement; {
one or more statements
}

An if-then-else structure (sometimes called an if-else structure) defines two different blocks of
statements, only one of which will be executed. An if-else structure consists of three parts, the selection
condition, the true branch, and the false branch. The true branch contains a block of statements that will
be executed whenever the selection condition is true. The false branch contains a block of statements
that will be executed whenever the selection condition is false.
The figure above illustrates the generic if-then-else structure and uses arrows to show the order in which
statements are executed and the condition is checked. The if-then-else structure defines a choice between two
courses of action, only one of which will be executed.

Lesson 13&14: Conditions

Conditions are used to build control structures (if, if-else, cascaded if, and while).There
are two types of conditions in the Jeroo language.

Simple Condition
A simple condition is formed by sending a Boolean message to a specific
Jeroo.Example: jessica.isNet(AHEAD)

Compound Condition
A compound condition is constructed from simple conditions and the Boolean (or
logical)operators shown in table A.6.

Boolean
Operator Meaning Name
! NOT Negation
&& AND Conjunction
|| OR Disjunction
Java's Syntax for the If-then-else Structure

Java's Syntax for the If-then Structure

Sensor Methods
The Jeroo class provides a number of sensor methods that can be used to ask a Jeroo something
about its
immediate surroundings. Each sensor method has either a true or a false result. Any method that
produces
either true or false as a result is called a
boolean method (also called a predicate).

Example (if-then structure)


Have the Jeroo named Jessica check for a net to her right. If there is one, have her disable it
and return to her current state. Whether or not she disables a net, Jessica should hop one space
ahead.
Example (if-then structure)
Have the Jeroo named Jessica check for a net to her right. If there is one, have her disable it
and return to her current state. Whether or not she disables a net, Jessica should hop one
space ahead.
Lesson 15&16: Problem Solving and Algorithms

I have a problem! I need to thank Aunt Kay for the birthday present she sent me. I could send
a thank you note through the mail. I could call her on the telephone. I could send her an email
message. I could drive to her house and thank her in person. In fact, there are many ways I
could thank her, but that’s not the point. The point is that I must decide how I want to solve the
problem, and use the appropriate tool to implement (carry out) my plan. The postal service,
the telephone, the internet, and my automobile are tools that I can use, but none of these
actually solves my problem. In a similar way, a computer does not solve problems, it’s just a
tool that I can use to implement my plan for solving the problem. Knowing that Aunt Kay
appreciates creative and unusual things, I have decided to hire a singing messenger to deliver
my thanks. In this context, the messenger is a tool, but one that needs instructions from me. I
have to tell the messenger where Aunt Kay lives, what time I would like the message to be
delivered, and what lyrics I want sung. A computer program is similar to my instructions to the
messenger. The story of Aunt Kay uses a familiar context to set the stage for a useful point of
view concerning computers and computer programs. The following list summarizes the key
aspects of this point of view.

 A computer is a tool that can be used to implement a plan for solving a problem.
 A computer program is a set of instructions for a computer. These instructions describe the
steps that the computer must follow to implement a plan.
 An algorithm is a plan for solving a problem.
 A person must design an algorithm.
 A person must translate an algorithm into a computer program.

This point of view sets the stage for a process that we will use to develop
solutions to Jeroo problems. The basic process is important because it can be
used to solve a wide variety of problems, including ones where the solution will
be written in some other programming language.

An Algorithm Development Process

Every problem solution starts with a plan. That plan is called an algorithm.

An algorithm is a plan for solving a problem.

There are many ways to write an algorithm. Some are very informal,
some are quite formal and mathematical in nature, and some are quite
graphical. The instructions for connecting a DVD player to a television are
an algorithm. A mathematical formula such as R2 is a special case of an
algorithm. The form is not particularly important as long as it provides a
good way to describe and check the logic of the plan.
The development of an algorithm (a plan) is a key step in solving a problem.
Once we have an algorithm, we can translate it into a computer program in
some programming language. Our algorithm development process consists of
five major steps.

Step 1: Obtain a description of the problem.


Step 2: Analyze the problem.
Step 3: Develop a high-level algorithm
Step 4: Refine the algorithm by adding more detail.
Step 5: Review the algorithm.

Step 1: Obtain a description of the problem.


This step is much more difficult than it appears. In the following
discussion, the word client refers to someone who wants to find a
solution to a problem, and the word developer refers to someone who
finds a way to solve the problem. The developer must create an
algorithm that will solve the client’s problem.

The client is responsible for creating a description of the problem, but this is
often the weakest part of the process. It’s quite common for a problem
description to suffer from one or more of the following types of defects: (1) the
description relies on unstated assumptions, (2) the description is ambiguous,
(3) the description is incomplete, or (4) the description has internal
contradictions. These defects are seldom due to carelessness by the client.
Instead, they are due to the fact that natural languages (English, French,
Korean, etc.) are rather imprecise. Part of the developer’s responsibility is to
identify defects in the description of a problem, and to work with the client to
remedy those defects.

Step 2: Analyze the problem.


The purpose of this step is to determine both the starting and ending points
for solving the problem. This process is analogous to a mathematician
determining what is given and what must be proven. A good problem
description makes it easier to perform this step.

When determining the ending point, we need to describe the characteristics


of a solution. In other words, how will we know when we’re done? Asking the
followingquestions often helps to determine the ending point.
 What new facts will we have?
 What items will have changed?
 What changes will have been made to those items?
 What things will no longer exist?
Step 3: Develop a high-level algorithm.
An algorithm is a plan for solving a problem, but plans come in several levels
of detail. It’s usually better to start with a high-level algorithm that includes
the major part of a solution, but leaves the details until later. We can use an
everyday example to demonstrate a high-level algorithm.

Problem: I need a send a birthday card to my brother, Mark.


Analysis: I don’t have a card. I prefer to buy a card rather than make one
myself. High-level algorithm:
Go to a store that sells greeting
cards Select a card
Purchase a
card Mail the
card

This algorithm is satisfactory for daily use, but it lacks details that would
have to be added were a computer to carry out the solution. These details
include answers to questions such as the following.
“Which store will I visit?”
“How will I get there: walk, drive, ride my bicycle, take the
bus?” “What kind of card does Mark like: humorous,
sentimental, risqué?”
These kinds of details are considered in the next step of our process.

Step 4: Refine the algorithm by adding more detail


A high-level algorithm shows the major steps that need to be followed to
solve a problem. Now we need to add details to these steps, but how much
detail should we add? Unfortunately, the answer to this question depends on
the situation. We have to consider who (or what) is going to implement the
algorithm and how much that person (or thing) already knows how to do. If
someone is going to purchase Mark’s birthday card on my behalf, my
instructions have to be adapted to whether or not that person is familiar with
the stores in the community and how well the purchaser known my brother’s
taste in greeting cards.
When our goal is to develop algorithms that will lead to computer programs, we need to
consider the capabilities of the computer and provide enough detail so that someone else
could use our algorithm to write a computer program that follows the steps in our algorithm. As
with the birthday card problem, we need to adjust the level of detail to match the ability of the
programmer. When in doubt, or when you are learning, it is better to have too much detail than
to have too little.

Most of our examples will move from a high-level to a detailed algorithm in a


single step, but this is not always reasonable. For larger, more complex
problems, it is common to go through this process several times, developing
intermediate level algorithms as we go. Each time, we add more detail to the
previous algorithm, stopping when we see no benefit to further refinement.
This technique of gradually working from a high-level to a detailed algorithm
is often called stepwise refinement.

Stepwise refinement is a process for developing a detailed algorithm


bygradually adding detail to a high-level algorithm.

Step 5: Review the algorithm.


The final step is to review the algorithm. What are we looking for? First, we
need to work through the algorithm step by step to determine whether or not it
will solve the original problem. Once we are satisfied that the algorithm does
provide a solution to the problem, we start to look for other things. The
following questions are typical of ones that should be asked whenever we
review an algorithm. Asking these questions and seeking their answers is a
good way to develop skills that can be applied to the next problem.

 Does this algorithm solve a very specific problem or does it solve a more
general problem? If it solves a very specific problem, should it be
generalized?
For example, an algorithm that computes the area of a circle having
radius 5.2 meters (formula *5.22) solves a very specific problem, but
an algorithm that computes the area of any circle (formula *R2) solves
a more general problem.

 Can this algorithm be simplified?


One formula for computing the perimeter of a rectangle is
length + width + length + width. A simpler formula would be
2.0*(length + width).

 Is this solution similar to the solution to another problem? How


are theyalike? How are they different?
For example, consider the following two formulas.
Rectangle area = length * width
Triangle area = 0.5*base*height
Similarities: Each computes an area. Each multiplies two measurements.
Differences: Different measurements are used. The triangle formula
contains 0.5. Hypothesis: Perhaps every area formula involves multiplying
two measurements.
Example – Pick and Plant
This section contains an extended example that demonstrates the algorithm
development process. To complete the algorithm, we need to know that every Jeroo
can hop forward, turn left and right, pick a flower from its current location, and plant a
flower at its current location. In chapter 4, we will use this algorithm to develop and
test a Jeroo program.

Problem Statement (Step 1)


A Jeroo starts at (0,0) facing East with no flowers in its pouch. There is a flower
at location (0,3). Write a program that directs the Jeroo to pick the flower and plant it
at location (2,3). After planting the flower, the Jeroo should hop one space East and
stop. There are no other nets, flowers, or Jeroos on the island.

Jeroo Flower

0 1 2 3 4 0 1 2 3 4
0 0
1 1
2 2
3 3
4 4
Start Finish

Analysis of the Problem (Step 2)


1. The flower is exactly three spaces ahead of the jeroo.
2. The flower is to be planted exactly two spaces South of its current location.
3. The Jeroo is to finish facing East one space East of the planted flower.
4. There are no nets to worry about.
High-level Algorithm (Step 3)
Let’s name the Jeroo Bobby.
Bobby should do the following:

Get the flower


Put the flower
Hop East

Detailed Algorithm (Step 4)


Let’s name the Jeroo Bobby.
Bobby should do the following
Get the flower
Hop 3 times Pick
the flower
Put the flower Turn
right Hop 2
times Plant a
flower
Hop East
Turn left
Hop once

Review the Algorithm (Step 5)


1. The high-level algorithm partitioned the problem into three rather
easy subproblems. This seems like a good technique.
2. This algorithm solves a very specific problem because the Jeroo and the
flower are in very specific locations.
3. This algorithm is actually a solution to a slightly more general problem in
which the Jeroo starts anywhere, and the flower is 3 spaces directly ahead of
the Jeroo.

You might also like