What You Will Learn |
---|
|
This section provides an introduction to the process of writing a Jeroo program. We introduce the form of a program, the possible ways to instantiate (create) a Jeroo, and the basic actions that every Jeroo understands.
At one level, a Jeroo program is just a text file, but one with identifiable parts. The largest parts are called methods. The body of each method consists of comments, blank lines, action statements, and control structures.
Jeroo's Java style programming language is case-sensitive. This means that upper-case and lower-case letters are considered to be different from one another. We need to be careful and consistent whenever we type the source code for a Jeroo program.
It's not enough to write a program that solves the intended problem. The program's logic must be well designed, and it must be easy for someone to read and understand the program's source code. Whitespace, comments, and descriptive identifiers all contribute to making a program easier to read.
Whitespace consists of blank lines, spaces, tabs, and newline (end-of-line characters). Blank lines should be used to organize the parts of the source code into groups that correspond to the logic of the algorithm. Indentations using tabs or spaces should be used to show the extent of methods and control structures. Spaces should be used to help a reader see the important parts of a line of source code. A newline (press Enter or Return on the keyboard) should be used at the end of every statement, to break up long statements into smaller parts, and to help show the major parts of the code. The recommended way to use whitespace will be demonstrated in the examples, and discussed as necessary.
Comments are phrases that a programmer inserts into a program to provide additional information for anyone who reads the program. Comments are ignored when the program is executed (run). A comment in Jeroo's Java style language begins with the digraph // and continues until the end of the line.
Methods are the primary components of a Jeroo program. Every Jeroo program must have exactly one main method, and it may contain any number of programmer-defined Jeroo methods. Programmer-defined Jeroo methods are discussed in later unit.
The main method describes how to use one or more Jeroos to solve a specific problem. The form of the main method is shown in the figure below. Everything that appears between the braces should be indented 2 or 3 spaces.
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 be used by the program. The syntax of this statement is shown below. There are two parts to the statement, the declaration and the instantiation
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 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 below.
Attribute | Default |
---|---|
Location |
(0,0) |
Direction |
EAST |
Flowers |
0 |
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 below.
Examples | Attributes |
---|---|
//---Accept all defaults --- Jeroo jessica = new Jeroo(); |
Name: jessica Location: (0,0) Direction: EAST Flowers: 0 |
//--- Specify just the flowers --- Jeroo jessica = new Jeroo(8); |
Name: jessica Location: (0,0) Direction: EAST Flowers: 8 |
//--- Specify just the location --- Jeroo jessica = new Jeroo(3,4); |
Name: jessica Location: (3,4) Direction: EAST Flowers: 0 |
//--- Specify location and direction --- Jeroo jessica = new Jeroo(3,4,WEST); |
Name: jessica Location: (3,4) Direction: WEST Flowers: 0 |
//--- Specify location and flowers --- Jeroo jessica = new Jeroo(3,4,8); |
Name: jessica Location: (3,4) Direction: EAST Flowers: 8 |
//--- Specify all attributes --- Jeroo jessica = new Jeroo(3,4,SOUTH,8); |
Name: jessica Location: (3,4) Direction: SOUTH Flowers: 8 |
The equal sign between the declaration and instantiation portions indicates that the newly created Jeroo object is to be associated with the identifier in the declaration portion.
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 below.
The Jeroo language includes the seven action methods shown in table below. Three of these, give, turn, and one version of hop require an argument value.
Method | Purpose | Example |
---|---|---|
hop() | Hop one space ahead. 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. | jessica.hop(); |
hop(number) | Hop number times in a row, where number is a positive integer. | jessica.hop(3); jessica.hop(12); |
pick() | Pick a flower from the current location. Nothing happens if there is no flower at the current location. | jessica.pick(); |
plant() | Plant a flower at the current location. Nothing happens if the Jeroo does not have a flower to plant. | jessica.plant(); |
toss() | Toss a flower one space ahead. The tossed flower is lost forever. If the flower lands on a net, the net is disabled. | jessica.toss(); |
turn(relativeDirection) | Turn in the indicated direction [ turn(AHEAD) and turn(HERE) are meaningless ] |
jessica.turn(LEFT); jessica.turn(RIGHT); |
give(relativeDirection) | Give a flower to a Jeroo in a neighboring cell in the indicated direction.
Nothing happens if the giving Jeroo has no flowers or if there is no Jeroo in the indicated direction. [give(HERE) is meaningless ] |
jessica.give(LEFT); jessica.give(RIGHT); |
This section contains an extended example that demonstrates a recommended process for converting an algorithm into completed source code. The problem statement and the detailed algorithm are repeated here for your convenience.
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
A good programmer doesn't write a program all at once. Instead, the programmer will write and test the program in a series of builds. Each build adds to the previous one. The high-level algorithm will guide us in this process.
The recommended first build contains three things.
Once the first build is working correctly, we can proceed to the others. In this case, each build will correspond to one step in the high-level algorithm. It may seem like a lot of work to use four builds for such a simple program, but doing so helps establish habits that will become invaluable as the programs become more complex.
This build adds the logic to "get the flower". The blank lines help show the organization of the logic. The new code is shown in boldface type.
This build adds the logic to "put the flower". The new code is shown in boldface type.
This build adds the logic to "hop East". The new code is shown in boldface type.