Chapter 4 teacher by vSTnB6

VIEWS: 12 PAGES: 4

									Chapter 4
Finishing the Crab Game

Objective:         Initialize a world, set and animate images

Terms:

      1.   Constructors – a method that initializes an object
      2.   State – what is happening with the object at this time
      3.   Instance variables – var that belong to an object, declared at the beginning
      4.   Assignment – sets the memory location of a variable to a value (=)
      5.   New (creating new objects) – instantiating an object, using it’s constructor


4.1: Adding Objects Automatically
6.         What is the first problem to be addressed in this section?

How to add objects automatically to a world

7.         What is special about the way WORLD instances are treated in Greenfoot?

Greenfoot automatically creates one instance of the world and displays it on the screen.

8.         What statement will always appear at the top of our Greenfoot classes?

import greenfoot.*;

9.         How is a constructor different from a method?

It does not have a return value, it is automatically called when you use the word new.

10.        What does the CrabWorld() constructor do?

Sets the screen size to 560 by 560.

11.        List four things we know from reading the addObject signature.

The method does not return an amount, thane name of the method is addObject, 3 parameters,(object,
int int)

12.        Write code to create a new crab object.

addObject(new Crab(), 100,100);
Chapter 4: Finishing the Crab Game

Ex. 4.1 thru 4.5

Your project should now place the crab, lobsters, and worms into the world automatically, whenever you
compile your scenario.

13.       Show your project to your teacher. You must have the project correct at this point before
          continuing.

Teacher Signature:


4.3: Animating Images
14.       Describe the “trick” to animating the crab.

Using two pictures and changing form on to the other quickly.

15.       What two concepts must be introduced in order to animate the crab?

Variables and greenfoot images


4.4: Greenfoot Images
16.       Write the code to access the crab2.png image.

New GreenfootImage(crab2.png)

Ex. 4.6 (questions 17-20 relate to Exercise 4.6)

17.       What are the two methods that allow us to change an actor’s image?

setImage(image) and getImage()

18.       What are their parameters?

setImage(image) and getImage has none.

19.       What do they return?

Setimage returns none and getImge true/false (Boolean)

20.       What will we use to store our two crab images?

GreenfootImage




Introduction to Programming with Greenfoot: by Michael Kolling (Chapter 4)
Resource: Terri Freeny, Northside High School                                                         2
Chapter 4: Finishing the Crab Game

4.5: Instance Variables (fields)
21.       What is an instance variable?

A variable that ‘belongs’ to an object.

22.       What does the type of variable define?

What kind of information will be put into memory.

23.       Where do we always write code for instance variables?

At the beginning of the class, right under the declaration.


4.6 Assignment
24.       Write an example of an assignment statement.

private int n = 10;


4.7 Using Actor Constructors
25.       List the 3 rules applied to the Crab constructor:

          a) The signature of the constructor does not include a return type

          b) The name of the constructor is the same as the name of the class

          c) The constructor is automatically executed when a crab object is created.

26.       Write the line of code that will set an image.

setImage(“crab.png”);


4.8 Alternating the Images
27.       Write the block of code that will alternate between image1 and image2, creating the animation
          effect.

if(getImage() == image1){
     setImage(image2);
}else{
    setImage(image1);
}

28.       What is the difference between using (=) and (==)?

= is assignment and == is compare

Introduction to Programming with Greenfoot: by Michael Kolling (Chapter 4)
Resource: Terri Freeny, Northside High School                                                             3
Chapter 4: Finishing the Crab Game

4.9 The if/else Statement
29.       Exercises 4.13-4.15 discuss creating a switchImage method to switch crab images. Write your
          code for that method.

Public void switchImage(){

     if(getImage() == image1){
          setImage(image2);
    }else{
         setImage(image1);
    }

}


4.10 Counting Worms
30.       List the 4 things you will need to count worms eaten, and play a winning sound when the crab
          eats 8 worms.

          a) an instance variable to count worms

          b) an assignment that initializes this variable to 0 at the beginning

          c) code to increment our count each time we eat a worm

          d) code that checks whether we have eaten eight worms, and stops the game and plays if we
          have.



31.       Write the code to create the wormsEaten variable.

private int wormsEaten = 0;




Introduction to Programming with Greenfoot: by Michael Kolling (Chapter 4)
Resource: Terri Freeny, Northside High School                                                            4

								
To top