Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied...

Post on 16-Dec-2015

212 views 2 download

Transcript of Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied...

Programming in Jessica

ByJoaquin Vila

Prepared by Shirley WhiteIllinois State University

Applied Computer Science Department

For Tuesday Read Savitch 1.4 Practice problems 22-27

Questions? Any questions about last class or

material covered on Tuesday?

What is Jessica? Jessica is a simple programming

language developed by Dr. Mary Elaine Califf, an instructor in the ACS department here at ISU.

It is an easy to learn language that will allow us to jump into programming with very little instruction.

Who is Jessica? Jessica is something not entirely unlike a

kangaroo She lives on the island of Santong, a rather

strange perfectly square island Jessica cannot swim--if she jumps off the

island, she’ll drown Jessica’s home is in the far northwest corner

of the island The island has two objects on it besides

Jessica: nets and flowers If Jessica moves onto a net, she is caught

and presumably killed

What Can Jessica Do? She can hop forward -- hop; She can turn left exactly 90 degrees -- left; She can turn right exactly 90 degrees --

right; She can pick a flower she’s standing on top

of -- pick; She can toss a flower into the space in front

of her (done to destroy a net) -- toss;

Jessica Programs/* Always begin every program with a

comment including your name */void main() /* no spaces between n and ( */{ /* brace is required */

/* instructions go here */hop;right;hop;

} /* end program -- brace is required */

Instructions

hop - move forward one unitleft - turn left 90 degreesright - turn right 90 degreespick - pick a flowertoss - toss a flower forward one unit; any

net there is disabled; the flower disappears whether there’s a net or not

Controlling Jessica If we know EXACTLY what Jessica’s

environment looks like and exactly what we want her to do, we can easily write a program to make her do it.

ButWe often want to write a more generic

programA program consisting of just the five basic

instructions can be very long

Control Structures To solve this problem, programming

languages (including Jessica) provide different control structures.

We use three basic control structures:sequenceselectionrepetition

Sequence A sequence is a set of instructions in order:

hop;hop; right;pick;

We can give Jessica instructions in a specific order, and Jessica will follow those instructions, one at a time, in the order specified.

Selection Selection is the control structure that

allows Jessica to make choices. For example, we might want her to

pick a flower only if she’s standing on one (to keep that annoying message from popping up) or to toss a flower only if there’s a net in front of her (to avoid wasting them)

Selection in Jessica The selection statement in Jessica

looks like this: if (condition) {

statements to do if condition is true} else {

statements to do if condition is false}

Repetition The repetition control structure allows

Jessica to repeat the same action(s) more than once.

She stops when some condition in her environment that you specify changes.

For example, if you want Jessica to hop until there is water in front of her, you can use repetition, or a loop, to do that.

Repetition in Jessica The repetition statement in Jessica looks

like this: while (condition) {

statements to repeat }

If condition is false to start with, the statements will never be executed

The condition must be something that will be changed when the statements are executed

Conditions In both if statements and while loops,

we need conditions which can be true or false.

In Jessica, there are 10 conditions that we can test.

These are the 10 things Jessica knows about her environment

What Does Jessica Know?

•Simple Conditions:

• at_flower• out_of_flowers• net_ahead• net_on_left• net_on_right

• water_ahead• facing_north• facing_south• facing_east• facing_west

Example of Selection

if (at_flower) {

pick;

toss;

} else {

hop;

}

Notice the punctuation

• semicolon (;) after each statement in instruction block

• no semicolon after if (at_flower) or else

Example of Repetition

while (at_flower) {

pick;

hop;

}

Notice the punctuation

• semicolon (;) after each statement in instruction block

• no semicolon after while (at_flower)

•braces enclosing instruction block

Negating a condition

if (!water_ahead) {

hop;

}

NOT: !simple condition opposite of simple condition

while (!water_ahead) { hop;}

Programming Practice Write code to make Jessica:

Hop one space ahead only if there is a net on her left

Pick a flower that is an unknown number of spaces ahead of her

Check for a net in front of her, and, if there is one, disable it by tossing a flower

Face north when you do not know which way she is facing

More Practice Write code to make Jessica:

Hop one space ahead. If there is a net on the right she, she should turn left; otherwise she should hop an addition space forward.

Disable the net in front of her if she has a flower in her pouch, but go around the net if she does not. Assume Jessica and the net are not adjacent to the water.

Task Lists A list of the things Jessica is supposed

to do Does not have to be in order Should be written in plain, clear

English

Example We want to write a fairly simple Jessica

program. The environment setup is:

Jessica is facing in an unknown direction with no flowers in her pouch

There is a flower somewhere north of JessicaThere is a net somewhere north of the flower

Jessica is to use the flower to destroy the net

Task List Face north Pick the flower Go to the flower Go to the net Destroy the net

Implementation Once we’ve considered what Jessica

needs to do, writing the program consists of:Putting the tasks in the correct orderWriting the Jessica code to accomplish

each task Let’s work through implementing this

program together.

Task List Practice Environment:

There is a rectangle made of nets.Along an inside wall of the rectangle is a

flower.Jessica is inside the rectangle, with a net

on her left.Jessica has no flowers in her pouch.

Task: Jessica is to leave the rectangle.

Modularization What do we mean by this term? What is a module? Why modularize programs?

Why Modularize? Solving small problems is easy Solving large, complex problems is hard The goal of modularization is to break a

problem into subproblems that are as independent of one another as possible

If you do this, you only have to solve one of these subproblems at a time

Steps in Modularization Develop the task list Group the tasks into modules Construct structure chart Consider order of processing Create logic of the mainline with calls

to the major processing modules Write the modules, working from top to

bottom

Paper Program Jessica is inside a rectangular house. There is a single door located on one

wall. (Jessica is not facing the door.) Outside the door is a paper (flower). Help Jessica locate the door, collect

the paper, and return to her house.

Write a Task List Find the wall Find the door Go outside Find the flower Pick the flower Return to the house

Structure Chart Example: Jessica Paper Program

Morning PaperMorning Paper

Find WallFind Wall Back HomeBack HomeFind DoorFind Door Pick PaperPick Paper

Program Design What do we mean by the term

“program design”? Why design programs?

Design Tools What is the first step in designing a

program?

Notes on Task Lists Just a list of the things Jessica is

supposed to do Does not have to be in order Should be written in plain, clear

English

Example We want to write a fairly simple Jessica

program. The environment setup is:

Jessica is facing in an unknown direction with no flowers in her pouch

There is a flower somewhere north of JessicaThere is a net somewhere north of the flower

Jessica is to use the flower to destroy the net

Task List Face north Pick the flower Go to the flower Go to the net Destroy the net

Implementation Once we’ve considered what Jessica

needs to do, writing the program consists of:Putting the tasks in the correct orderWriting the Jessica code to accomplish

each task Let’s work through implementing this

program together.

Task List Practice

Environment:There is a rectangle made of nets.Along an inside wall of the rectangle is a

flower.Jessica is inside the rectangle, with a net on

her left.Jessica has no flowers in her pouch.

Task: Jessica is to leave the rectangle.