Download presentation
Presentation is loading. Please wait.
Published byAda Williams Modified over 9 years ago
1
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department
2
For Tuesday `Read Savitch 1.4 `Practice problems 22-27
3
Questions? `Any questions about last class or material covered on Tuesday?
4
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.
5
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
6
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;
7
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 */
8
Instructions `hop - move forward one unit `left - turn left 90 degrees `right - turn right 90 degrees `pick - pick a flower `toss - toss a flower forward one unit; any net there is disabled; the flower disappears whether there’s a net or not
9
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. `But `We often want to write a more generic program `A program consisting of just the five basic instructions can be very long
10
Control Structures `To solve this problem, programming languages (including Jessica) provide different control structures. `We use three basic control structures: `sequence `selection `repetition
11
Sequence `A sequence is a set of instructions in order: `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.
12
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)
13
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 }
14
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.
15
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
16
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
17
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
18
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
19
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
20
Negating a condition if (!water_ahead) { hop; } ` NOT:!simple condition opposite of simple condition while (!water_ahead) { hop; }
21
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
22
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.
23
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
24
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 Jessica `There is a net somewhere north of the flower `Jessica is to use the flower to destroy the net
25
Task List `Face north `Pick the flower `Go to the flower `Go to the net `Destroy the net
26
Implementation `Once we’ve considered what Jessica needs to do, writing the program consists of: `Putting the tasks in the correct order `Writing the Jessica code to accomplish each task `Let’s work through implementing this program together.
27
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.
28
Modularization `What do we mean by this term? `What is a module? `Why modularize programs?
29
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
30
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
31
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.
32
Write a Task List `Find the wall `Find the door `Go outside `Find the flower `Pick the flower `Return to the house
33
Structure Chart Example: Jessica Paper Program Morning Paper Find Wall Back Home Find Door Pick Paper
34
Program Design `What do we mean by the term “program design”? `Why design programs?
35
Design Tools `What is the first step in designing a program?
36
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
37
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 Jessica `There is a net somewhere north of the flower `Jessica is to use the flower to destroy the net
38
Task List `Face north `Pick the flower `Go to the flower `Go to the net `Destroy the net
39
Implementation `Once we’ve considered what Jessica needs to do, writing the program consists of: `Putting the tasks in the correct order `Writing the Jessica code to accomplish each task `Let’s work through implementing this program together.
40
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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.