Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 2 Programming in Alice & Java 1. Day 1: Learning to Program  Objective: Understand the need for step-by-step logic; Basic working of a computer.

Similar presentations


Presentation on theme: "Unit 2 Programming in Alice & Java 1. Day 1: Learning to Program  Objective: Understand the need for step-by-step logic; Basic working of a computer."— Presentation transcript:

1 Unit 2 Programming in Alice & Java 1

2 Day 1: Learning to Program  Objective: Understand the need for step-by-step logic; Basic working of a computer  Essential Skill: Gather and organize information and data 2

3 What is a computer program?  A program is a sequence of instructions that  tell the computer what to do  tell another human being what you want the computer to do  Programs are written for various kinds of tasks (not mutually exclusive)  solve a problem  simulation  game  animation 3

4 What are statements?  A statement is a written instruction  Three basic kinds of statements  Action to be performed (executes when the program is run)  Control (determines the sequence in which actions are performed)  Comment (documentation of who wrote the program and what the program is intended to do)  The methods panel contains tiles for creating program statements 4

5 What are methods?  Actions that are placed in a specific order, generally inside of a control of some type.  Class or object methods  things like move, say, turn, roll  Scene level methods  fog density, background color, etc… 5

6 More on methods  Alice has two types of methods:  Procedures: methods that perform an action  Functions: methods that ask a question  Calling a method: bennyBunny.move(10);  When you write this line of code you are calling the “built-in” procedure “move”. It is a method of the Hare class, which it inherits from the Object class.  10 is an argument or parameter which tells the Hare how far to move 6

7 Procedural methods  All objects in Alice have built-in procedures  Procedures are methods that perform actions  Move, turn, roll, turnToFace, etc…..  Some procedures are built-in -- part of the Alice 3 system when you install it.  You (as the programmer) can write your own procedural methods. We say these methods are user-defined. 7

8 Let’s write a simple program  Create a world using the grass template  Add an instance of the Hare (in the Biped folder)  Position the hare so he is in the middle of the screen and standing on the grass  Let’s make the hare jump up and down 8

9 A Little More Complicated Example 9

10 Explanation of components in card program:  Control Elements:  Do in order-perform actions in sequence  Do together- perform actions at same time  Analogy: sports track  Do in order—one runner on track  Do together—adds other runners on separate tracks 10

11 Explanation of components in card program:  Comments are used to document:  your program  Identify the author  Describe the problem/task  Methods (procedures and functions) that you write  Simple description of what the procedure/function does (10-15 words)  Special considerations for use of the method  Required comments for ALL projects for the remainder of the semester:  Name  Description of problem/task (10-15 words)  Brief description in any user-defined methods (10 – 15 words) 11

12 Classwork  Add another object to your scene with the hare  Write some code to make the two objects perform actions together and separately  You might want to try having subparts perform an action (like raising an arm or kicking a leg) 12

13 Days 2-3: Writing a program  Objective: Understand the need for step-by-step logic; Basic working of a computer  Essential Skill: DM1- Use technology to advance creative thinking and construct knowledge 13

14 Programming Problems  Examples presented in this course and assigned in projects are problems, worded like this:  "Write a program to create an animation, where a …“  The problem is: how to write a program “to create an animation, where a...”  Example: Create an animation for the following short film:  A penguin has discovered a bowling ball and a pin in a crate that washed up on the arctic ice. The animation you are to create will show the penguin learning to bowl. The penguin moves forward to the bowl ball. Then, the penguin swaggers left and right, as he considers how to make the ball roll toward the pin. He decides to try pushing the bowling ball toward the pin. When the ball moves forward, it successfully knocks the pin over. Surprisingly, the pin stands up, shakes itself off and pushes the bowling ball back at the penguin. Then the penguin is knocked over! 14

15 Step 1: Understand the Problem Problem Statement Understand the problem Implement the design Test the Code Design a solution Identify the objects (characters / props) Identify the actions 15

16 Step 1: Understand the Problem  Need to understand  What characters and props (nouns) are needed?  The penguin moves forward to the bowl ball. Then, the penguin swaggers left and right, as he considers how to make the ball roll toward the pin. He decides to try pushing the bowling ball toward the pin.  What actions (verbs) are performed?  The penguin moves forward to the bowl ball. Then, the penguin swaggers left and right, as he considers how to make the ball roll toward the pin. He decides to try pushing the bowling ball toward the pin. 16

17 Step 2: Design Problem Statement Understand the problem Implement the design Test the code Design a solution Develop an algorithm (storyboard) Set up the scene 17

18 Step 2: Design  Set up the scene  Create algorithm  Just like architects/engineers create blueprints, animators create storyboards  Storyboard: 18 Penguin pushes the ball Sound: none Text: none Ball strikes the pin Sound: none Text: none Pin stands up Sound: “Thunk” Text: none

19 Step 2: Design an Algorithm  Make a “to-do” list of the storyboard actions to be animated  Programmers call this an algorithm  Usually more detailed than sketches or screen captures 19 Do in order penguin move forward (to the ball) penguin swagger penguin push the bowling ball so it rolls to the pin pin falls over pin responds (stands up and pushes the ball back to penguin) penguin falls over

20 Step 3: Implement the Design 20 Write statements (code) – a few steps at a time, not the entire program at once! Problem Statement Understand the problem Implement the design Test the code Design a solution Decompose into simple steps

21 Step 3: Implement the design  Implementation is the act of translating an algorithm/storyboard into a computer program.  To translate:  Decompose algorithm step by step  Incrementally develop and test program statements  Not all at once, but a few steps at a time!  To create a statement, drag a code tile into the edit space  Lets make the penguin move towards the ball! 21

22 Step 4: Test and Revise, if needed 22 Problem Statement Understand the problem Revise? Implement Revise? Test the code Design a solution Revise? Does it work, no matter what? Try different values (distance, direction, revs)

23 Step 4: Test and Revise  When Run… button is clicked  Alice builds the scene for the runtime window  Executes the program statements, starting with the myFirstMethod procedure (defined in MyScene) 23 Runtime window

24 Creating your own procedures!  We moved towards the ball, now lets make the penguin swagger  But problem—swagger isn’t a tile in the procedures!  Built in procedures are simple actions.  We will need to create our own procedure made up of multiple simple actions  Lets decompose the “swagger” procedure: 24 penguin swagger Do in order // penguin shift side to side penguin roll left 0.125 penguin roll right 0.25 penguin roll left 0.125 //penguin jump up and down penguin move up 0.5 penguin move down 0.5

25 The Class Tab  Select Penguin from Class Menu  Select the Penguin class tab  The class document shows all the methods (procedures & functions) and properties of the class  Can add, edit, or delete procedures, functions, or properties in a class document  Lets add a procedure by clicking on the Add Procedure button in the procedures section 25

26 Procedure Names  Type in a name for the new procedure  Naming rules:  Begin with an alphabetic character, not a digit  A name consists of letters, digits or underscore “_”, no spaces/other symbols  Start with a lower case letter  Use camelCase if more than one word is desired  Two or more classes may have procedural methods with the same name but the actions may be different  Penguin has a swagger side to side  Pin might also have a swagger front to back 26

27 Procedure swagger 27 “this” is a proxy – “stands in for” the name of any penguin object (instance of the Penguin class) in your project scene, whatever penguin calls the procedure

28 Comments for the Procedure  Documentation for procedures  Assumes: Assumptions made regarding conditions prior to calling the procedure  Action: description of the procedure action  Postcondition, if any  In-line comments: More specific comments may be imbedded in the code, to help other programmers understand what the code is intended to do 28

29 Implement swagger  Using our design as a guide, drag tiles into the swagger code editor  Final code: 29 penguin swagger Do in order // penguin shift side to side penguin roll left 0.125 penguin roll right 0.25 penguin roll left 0.125 // penguin jump up and down penguin move up 0.5 penguin move down 0.5

30 Step 4: Test  Return to the Scene Class and Click on the Run button  What happens and why?!? 30

31 We need to call swagger in myFirstMethod  We defined the new procedure, swagger; but, we also need to call it somewhere in our program  Select penny in the instance menu  Drag the swagger tile into myFirstMethod in the editor  Now test again 31

32 Classwork  One final hint: for quick access to the Add Procedure button: 1. Click on the Class Menu button 2. Select Penguin from Class Menu in the Code Editor 3. Select Add Procedure… from the Penguin class submenu  Now finish the penguin and bowling program! 32 Start by clicking here

33 Days 4-5: Project 2.1  Project description and requirements posted on website 33

34 Day 6: Unit 2 Quiz  20 points  Multiple Choice  True/False  Short Answer  Material comes from this PowerPoint and in class discussions 34


Download ppt "Unit 2 Programming in Alice & Java 1. Day 1: Learning to Program  Objective: Understand the need for step-by-step logic; Basic working of a computer."

Similar presentations


Ads by Google