Greenfoot Chapter 4 Finishing the Crab GameName ___Key______

Use as a checklist for the activities in the chapter.

Read the textbook and following the exercise instructions. Whenever you are asked to try record results use this worksheet as your record of that work. You will turn this in at the end of each chapter. Some activities you need to show your instructor.

Section 4.1 Adding objects automatically (Start with little-crab 4 for this chapter)

Copy the current constructor from the CrabWorld here

A constructor is different than a method because

1. __no return type ______

2. __the name is always the same as the name of the class____

This constructor is automatically executed whenever a Crab world is created.

Change your constructor as shown on page 54. Compile your code.

3. What happens? _A crab is automatically added to the work at location 260, 200.

Section 4.2 Creating new Objects

In Java, the keyword ‘new’ is used to create a new instance of an object.

When you create a new object you must do something with it.

Circle in the code to the right where a new Crab was created.

Section 4.3 Variables

Variables must be declared with a type before they can be used in Java.

A variable declaration includes a type and the name of the variable.

int age;

Crab myCrab;

The type can be any data type or an object. Java has a small subset of primitive types such as int, double and boolean. See Appendix D for a list and description of each.

Section 4.4 Assignment

Storing a value to a variable is accomplished using an assignment statement.

Assignment works from right to left. The new crab() was created and then it was assigned to the variable myCrab.

age = 12 would read as “12 is stored into the variable age”

4. Copy the general form for assignment from your text (page 56) here.

____variable = expression; ______

Assignments replace any previous values.

Assignments can be combined with a declaration statement such as: int age = 12;

Section 4.5 Object Variables

This line of code does three things Crab myCrab = new Crab();

It creates an object variable called myCrab.

It creates a crab object using the new command.

It assigns the crab object to the myCrab variable.

□Exercise 4.1 ___int score; ______

□Exercise 4.2 ____booleanisHungry = true; ______

□Exercise 4.3 ____int year = 2014;

□Exercise 4.3b __ year = 2015; ______// change the value assigned to year to 2015

□Exercise 4.4 ____Crab littleCrab = new Crab();______

□Exercise 4.5 ____Control inputButton = new Button (): ______

□Exercise 4.6 ___’int’ should be ‘Crab’ since it is a new crab object that is being created.

  • Correction: Crab myCrab = new Crab();

Section 4.6 Using variables – Read your text

Most math operators work as you would expect in Java. For multiplication you need to use an asterisk(*) and use a forward slash (/) for division. There are some other special operators and notes concerning division but we will cover those later as they are needed.

Complete Exercises 4.7 to 4.9

□Exercise 4.7_int children; ______

__children = daughters + sons. ______

□Exercise 4.8 _int area; ______

__area = width * length; ______

□Exercise 4.9 Note: you may need more lines

_ int x; x = 23; or use a single statement int x = 23;

__int y; y = 17 ; or use a single statement int y = 17;

Swap: int temp = x;

y = x;

x = temp;

Section 4.7 Adding objects to the world

Note the addObject(myCrab, 250, 200) statement

in the CrabWorld constructor.

The GreenfootaddObject() method is one you will use frequently. Copy its signature below (page 59).

voidaddObject(Actor object, int x, int y)

5. Does the method return a value ? __no______

6. Circle the word that tells you this.

7. Underline the name of the method.

8. Draw a box around the parameter list.

9. What are the types of the parameters in the order they appear? Actor ,int and _int______

10. What is the purpose of the last 2 parameters. For example in addObject(myCrab, 250, 200) what purpose do the numbers 250 and 200 serve? Draw a picture if you prefer.

_They set the location of the crab starting with (0,0) in the upperleft corner of the screen and x going to the right 250 and y going down 200

You may have noticed the first parameter is of type Actor but we are using a Crab when we add myCrab to the world. This is permissible because a Crab “is-a” Actor. You can see this from the Class hierarchy on the right side of your screen. Actor is what we call a superclass and Crab, Lobster and Worm are all subclasses. If you are not sure if a class is a subclass of another try the “is-a” test.

A Crab ‘is-a’ Actor so it is a subclass of Actor.

A Lobster ‘is-a’ Actor so it is a subclass of Actor.

A Worm ‘is-a’ Actor so it is a subclass of Actor.

Therefore we could use any of these actors in the addObject method.

□Exercise 4.10. We did this back on Question #3 so you may be done.

□Exercise 4.11 – 4.12. Complete and show your instructor or your team leader. (Look for crabs and 2 worms auto generated. ) ______initials

Section 4.8 Save the World : Read your text.

□Do Exercises 4.13 – 4.14 What is the purpose of the prepare method? It is the code that creates new crabs and worms that we placed into the world. The code is autogenerated by the Save As action.

We actually started using this trick for Saving the World in an earlier chapter but now you should have a better understanding of how it works.

Section 4.9 Animating images Read the text.

Section 4.10 Greenfoot Images

□Exercise 4.15

□Method: _setImage(GreenfootImage image)Agument(s)_a GreenfootImage____

What is the return type?: __void______

□Method: _setImage(java.lang.String filename)____Argument(s): a String______

What is the return type?: _void______

11. Why is it better to separate the code that create the image files and that sets the images? _Efficiency is the reason. If we separate the code the image objects are created just once and then we can access them and their images any time we wish.

Section 4.11 Instance variables (fields)

12. One difference between local variables and instance variables is that local variables are declared _inside a method___ while an instance variable are declared inside the class before any methods.

Instance variables also have the key word private. Most importantly is the lifetime of the instance variable. Instance variables belong to the object they are declared in and will be accessible and survive as long as the object exists.

Copy here the general form for declaring an instance field (pg 64)

______private variable – type variable – name; ______

□Exercise 4.16 What are the variables for a crab object when you choose to Inspect the crab’s pop up menu? x, y, rotation, world, image. Note all variables start with lowercase by naming conventions.

□Exercise 4.17 They are variables inherited from Actor since crab “is-a” Actor it inherits all the Actors instance variables (or instance fields).

□Exercise 4.18 Follow text instructions.

□Exercise 4.19 What new variables show up after inspection? image1 and image2 ____

The images have not actually been created – only the space to store them has been created. Find one example of a line of code that will create an actual image. _image1 = new GreenfootImage(“crab.png” );

Section 4.12 Using actor constructors

Declaring image variables(instance fields) has been done but where do you need to assign a value to those fields? The answer is inside the Crab constructor as shown on page 66. This code is executed once when the actor is created so it is still efficient because the action only takes place once.

Read the Pitfall carefully and ask clarification questions.

□Exercise 4.20 Follow text instructions. Add the constructor from Code 4.4.

□Exercise 4.21 Variables after inspection should show an arrow or ‘link’ to the new image objects.

Section 4.13 Alternating the images Read your text.

13. In your own words describe what the pseudocode presented in 4.13 will do when implemented? __The plan is for the code to switch back and forth between 2 images to create the animation.

14. What is the purpose of the == symbol? _tests for equality ______

15. What image does getImage() capture? _the current objects image______

Section 4.14 The If/else statement

Copy the general form for the if/else statement here. (pg 69)

The flowchart representation of the if/else statement is to the right. You can see there are two branches and every time one of them will execute depending upon whether the condition statement (diamond) is true or false. This is sometimes called a dual-selection statement because it has 2 options available.

Look at your text to see how the code for the if (yes branch) and the else (no branch) are enclosed within braces.

Exercise 4.22 – 4.24 Complete and show your instructor or team leader.

Section 4.15 Counting Worms

Using the concepts in this chapter complete

□Exercise 4.25 and 4.26. Show your instructor and be prepared to submit your final version.

16. What value do all integer variables in Java get initialized to automatically? __zero______

Section 4.16 More ideas

If you get done early, try some of the extensions listed in this section.

Drill and Practice.

Complete Exercises 4.28 through 4.34 Crab Scenario Extensions. Comment each exercise as it is finished above the code. Example: // 4.28 Lobsters turn towards the center of the screen.

Submit your final version for a lab grade.

If time permits complete Exercises 4.35 to 4.39. Submit for inspection. If all worksheets have been completed on time we can discuss using this as a replacement grade for earlier work.

Concept Summary and Notes: Use this page to take notes throughout the chapter or as a review.

constructor
The ‘new’ keyword
Variables
Variable declarations
Assignment statement
Primitive types
Object types
A ‘reference’ to an object
Greenfootimage
Instance variables (fields)
Lifetime of instance field (going out of scope)
Lifetime of local variables
Equal operator
if/else statement
pseudocode