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

40
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department

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

Page 1: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Programming in Jessica

ByJoaquin Vila

Prepared by Shirley WhiteIllinois State University

Applied Computer Science Department

Page 2: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

For Tuesday Read Savitch 1.4 Practice problems 22-27

Page 3: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Questions? Any questions about last class or

material covered on Tuesday?

Page 4: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 5: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 6: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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;

Page 7: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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 */

Page 8: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 9: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 10: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Control Structures To solve this problem, programming

languages (including Jessica) provide different control structures.

We use three basic control structures:sequenceselectionrepetition

Page 11: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 12: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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)

Page 13: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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}

Page 14: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 15: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 16: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 17: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 18: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 19: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 20: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Negating a condition

if (!water_ahead) {

hop;

}

NOT: !simple condition opposite of simple condition

while (!water_ahead) { hop;}

Page 21: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 22: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 23: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 24: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 25: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 26: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 27: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 28: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 29: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 30: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 31: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 32: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 33: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Structure Chart Example: Jessica Paper Program

Morning PaperMorning Paper

Find WallFind Wall Back HomeBack HomeFind DoorFind Door Pick PaperPick Paper

Page 34: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Program Design What do we mean by the term

“program design”? Why design programs?

Page 35: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

Design Tools What is the first step in designing a

program?

Page 36: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 37: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 38: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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

Page 39: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.

Page 40: Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.

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.