Programming with GreenfootÉtudes — Chapter 2

Method signatures

1. Look at the following method signatures:

public void play();
public void addAmount(int amount);
public boolean hasWings();
public void compare(int x, int y, int z);
public boolean isGreater (int number);

For each of these signatures, answer the fllowing questions:

  1. What is the method name?
  2. Does the method return a value? If yes, what is the type of the return value?
  3. How many parameters does the method have

2. Write a method signature for a method named "go". The method has no parameters, and it does not return a value.

3. Write a method signature for a method named "process". The method has a parameter of type "int" that is called "number", and it returns a value of type "int".

4. On paper, write a method call (note: this is a method call, not a signature) for the "play" method from exercise 1. Write another method call for the "addAmount" method from exercise 1.

All the following exercises are intended to be implemented in the Greenfoot scenario "fatcat". Download the scenario here and open it in Greenfoot before continuing.

Reading documentation

5. Open the editor for class Cat. Make sure that the editor is in Documentation view. How many methods does the class Cat have?

6. How many of the Cat's methods return a value?

7. How many parameters does the "sleep" method have?

Writing method calls (with and without parameters)

8. Try calling some of your cat's methods interactively, by using the cat's popup menu. The interesting methods are all "Inherited from Cat".

9. Is the cat bored? How can you make it not bored?

10. Open the editor for class "MyCat". (This is where you will write the code for all the following exercises.) Make the cat eat when it acts. (That is: In the act method, write a call to the eat method.) Compile. Test by pressing the Act button in the execution controls.

11. Make the cat dance. (Don't do this interactively - write code in the act method to do this. When done, click the Act button in the execution controls.)

12. Make the cat sleep.

13. Make the cat do a routine of your choice, consisting of a number of the available actions in sequence.

If statements

14. Change the act method of your cat so that, when you click Act, if the cat is tired, it sleeps a bit. If it is not tired, it doesn't do anything.

15. Change the act method of your cat so that it dances if it is bored. (But only if it is bored.)

16. Change the act method of your cat so that it eats if it is hungy.

17. Change the act method of your cat to the following: If the cat is tired, it sleeps a bit, and then it shouts hooray. If it is not tired, just shouts hooray. (For testing, make the cat tired by calling some methods interactively. How can you make the cat tired?)

18. Write code in the act method to do the following: If your cat is alone, let it sleep. If it is not alone, make it shout "Hooray". Test by placing a second cat into the world before clocking Act.

 

Back to Études page