220 likes | 712 Views
Greenfoot. Chapter 2 The First Program Little Crab. The Little Crab Scenario. The hierarchy indicates an is-a relationship (also called inheritance) The crab is an animal and an animal is an actor thus a crab is an actor. Making the Crab Move. public void act()
E N D
Greenfoot Chapter 2The First Program Little Crab From Introduction to Programming with Greenfoot by Michael Kölling Used with permission
The Little Crab Scenario From Introduction to Programming with Greenfoot by Michael Kölling Used with permission The hierarchy indicates an is-a relationship (also called inheritance) The crab is an animal and an animal is anactor thus a crab is an actor
Making the Crab Move From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • public void act() • act - empty method – no code • Animals have no default action. • Overridesact in class greenfoot.Actor • Open editor – standard Java class definition • Replace comment // … with the move(); method • Method is an action than an object (crab) knows how to do • Method call is an instruction telling the crab to do it
Making the Crab Move cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Exercise 2.2: Change the act method in your crab class to include the move() instruction. Compile and place a crab into the world and click the Act and Run buttons. Exercise 2.3: Place multiple crabs into the world. Run the scenario. What do you observe?
Turning From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • public void turn(int angle) • 'angle' is degrees towards the right (clockwise) • 0 to 359 degrees • Value passed is called a parameter • Exercise 2.4: Replace move()instruction with turn(5). Try other values. Must re-compile every time change the code. • Exercise 2.5: How can you make the crab turn left?
Multiple Instructions in Method From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Multiple instructions executed in sequence (one after the other in ordered written) Exercise 2.6 Use both a move() and turn(N) instruction. Use different values for N. Move expects no parameters – just executes Turn requires additional information – an Integer value
Syntax Errors From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Compiler checks for errors • Question mark button displays to the right of the error message • Clicking button displays additional information about error • Highlighted line may be the line after the error as in a “; expected” message • Exercise 2.7: Remove semicolon after move() and re-compile • Exercise 2.8: Make various changes to cause different error messages. Find at least five different error messages.
Dealing with Screen Edges From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Actors can’t leave the world and fall off it edge • Subclass (i.e. crab) inherits all the abilities (methods) from its superclass (i.e. animal) • Called inheritance • Exercise 2.9: Open Animalclass. Switch to Documentation view. How many methods doesthis class have?
Non-void Return Type From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Calling a method with a void return type issues a command • Calling a method with a non-void return type asks a question • boolean atWorldEdge() – method will return either true (Yes!) or false (No!) • Exercise 2.10: Create a crab. Call method. What does it return? • Exercise 2.11: Let crab run to edge of the screen or move it there. What does method return?
if Statement From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • if statement can be used to write instructions that are executed only when a certain condition is true • Syntax (form of if statement) if (condition) { instruction; instruction; } • condition is any expression that is either true or false i.e. atWorldEdge() method call • instructions executed only if condition is true • instructions skipped if condition is false – execution continues below curly bracket i.e. }
if Statement cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Exercise 2.12: Make your crabs turn at the edge of the screen Exercise 2.13: Experiment with different values for the parameter to the turn method Exercise 2.14: Place the move() statement inside the if-statement rather than after it. Explain the behavior you observe. Fix the problem.
Indentation From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Tip: Greenfoot will mark matching curly bracket when cursor is placed behind a curly bracket Use 4 spaces for one level of indentation (code after curly bracket) or the Tab key. Reduces errors i.e. missing curly brackets Makes code much easier to read
Summary of Programming Techniques From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Called methods with or without parameters • Identified the body of act method • Encountered error messages • Encountered a first glimpse of inheritance • Seen how to make decisions • Used if statement for conditional execution • Used boolean type (a true or false value)
Concept Summary From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • A method call is an instruction that tells an object to perform an action. The action is defined by a method of the object. • Additional information can be passed to some methods within the parentheses. The value passed is called a parameter. • Multiple instructions are executed in sequence, one after the other, in the order in which they are written. • When a class is compiled, the compiler checks to see whether there are any errors. If an error is found, an error message is displayed.
Concept Summary cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • A subclass inherits all the methods from its superclass. That means that it has and can use, all methods that its superclass defines. • Calling a method with a void return type issues a command. Calling a method with a non-void return type asks a question. • An if statement can be used to write instructions that are executed only when a certain condition is true.