Greenfoot Chapter 4 Finishing the Crab GameName ______

Use as a checklist for the activities in the chapter. Take notes and record page numbers as needed. It is expected that you are reading the textbook and following the exercise instructions. Whenever you are asked to try something or record something 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 Constructor from the CrabWorld here

A constructor is different than a method because

1. ______

2. ______

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? ______

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.

______

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 ______

□Exercise 4.2 ______

□Exercise 4.3 ______

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

□Exercise 4.4 ______

□Exercise 4.5 ______

□Exercise 4.6 ______

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 ______

______

□Exercise 4.8 ______

______

□Exercise 4.9 Note: you may need more lines

______

______

______

______

Section 4.7 Adding objects to the world

Note the addObject(myCrab, 250, 200) statement

in the CrabWorld constructor.

The addObject() method is one you will use frequently. It is a built in Greenfoot method. Copy its signature below (page 59).

5. ______

6. What is its return type? ______

7. What is its name? ______

8. How many parameters does it have? ______

9. What are the types of the parameters in the order they appear? ______, ______and ______

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 server? ______

______

______

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.

□Exercise 4.11 – 4.12. Complete and show your instructor or your team leader.

Section 4.8 Save the World

Read your text. and do

□Exercises 4.13 – 4.14

What is the purpose of the prepare method? ______

We have 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: ______Argument(s)______

What is the return type?: ______

□Method: ______Argument(s): ______

What is the return type?: ______

11. Why Is it better to separate the code that create the image files and that sets the images? ______

Section 4.11 Instance variables (fields)

12. One difference between local variables and instance variables is that local variables are declared ______while an instance variable are declared ______

______.

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)

______

□Exercise 4.16 What are the variables for a crab object when you choose to Inspect the crab’s pop up menu? ______

□Exercise 4.17 ______

______

□Exercise 4.18 Follow text instructions.

□Exercise 4.19 Variables after inspection? ______

______

The images have not actually been created – only the space to store them has been created. Give one example of a line of code that will create an actual image. ______

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.

□Exercise 4.21 Variables after inspection? ______

______

Section 4.13 Alternating the images

Read your text.

13. What is pseudocode? ______

14. What is the purpose of the == symbol? ______

15. Whatimage does getImage() capture? ______

Section 4.14 The If/else statement

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

The flowchart representation for in 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 get initialized to automatically? ______

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. Submit your final version for a lab grade.

If time permits complete Exercises 4.35 to 4.39. Submit for a lab grade. 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

Comp Science Intro Greenfoot CH 4 7/1/2015 Page 1

Exercises are from the Introduciton to Proramming with Greenfoot book by Michael Kolling, second edition, Pearson Higher Ed