In the greenfoot ide what does the and operator do
In the greenfoot ide what does the and operator do
Kunci Jawaban All Quiz Oracle Academy Java Fundamental 2017 Part 8
1. In Greenfoot, constructors can be used to create new instances of objects. True or false? Mark for Review
(1) Points
2. Which Greenfoot control operator is used to test if two values are equal? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 8.
3. From your Greenfoot lessons, what is the parameter of the following constructor that creates a new image, and designates it to the Actor class?
setImage (new GreenfootImage(«Bee01.png»)); Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 8.
4. In Greenfoot, the sound file must be saved in the scenario and written in the source code for it to play. True or false? Mark for Review
(1) Points
5. In Greenfoot, which class has methods that allow you to get the status of the mouse? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 7.
6. Greenfoot has tools to record sound. True or false? Mark for Review
(1) Points
7. From your Greenfoot lessons, what types of values cannot be stored in a local variable? Mark for Review
(1) Points
(Choose all correct answers)
[Incorrect] Incorrect. Refer to Section 3 Lesson 10.
8. In the Greenfoot IDE, what does the AND operator (&&) do? Mark for Review
(1) Points
Compares two boolean variables or expressions and returns a result that is true if either of its operands are true.
Compares two boolean values and returns a boolean value which is true if either one of the operands is true.
Compares two boolean values, and returns a boolean value which is true if and only if both of its operands are true. (*)
Compares two boolean values, and returns a boolean value which is true if and only if one of its operands are true.
[Incorrect] Incorrect. Refer to Section 3 Lesson 10.
9. Which of the following Greenfoot logic operators represents «not»? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 10.
10. From your Greenfoot lessons, abstraction techniques can only be used once in a class’s source code. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 9.
11. Writing more generic statements to handle the creation and positioning of many objects is one Abstraction technique? Mark for Review
(1) Points
12. In Java what is casting? Mark for Review
(1) Points
Casting is when we remove an object from the world
Casting is when we want to tell the java compiler that a class we are accessing is really another type of class (*)
Casting is when we reset the state of an instance.
Casting is when we change the coordinates of an actor
[Incorrect] Incorrect. Refer to Section 3 Lesson 9.
13. Defined methods are methods that are only created by the Greenfoot development team? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 6.
14. In Greenfoot, which of the following statement is true about Defined Methods? Mark for Review
(1) Points
A defined method is automatically executed once created.
A defined method is only relevant to the Greenfoot Development team.
A defined method must be called by your source code, normally in the Act method. (*)
A defined method only relates to the World class.
[Incorrect] Incorrect. Refer to Section 3 Lesson 6.
15. In reference to Greenfoot, if the following method was defined in a superclass,
public void turnAtEdge()<
.
>
all subclasses of the superclass will inherit the method.
True or false? Mark for Review
(1) Points
[Correct] Correct
1. From your Greenfoot lessons, a problem statement defines the purpose for your game. True or false? Mark for Review
(1) Points
2. From your Greenfoot lessons, what are the ways that you can view a class’s methods? Mark for Review
(1) Points
(Choose all correct answers)
In the Greenfoot gallery
In the scenario
In the class’s documentation (*)
By right-clicking on an instance (*)
[Incorrect] Incorrect. Refer to Section 3 Lesson 3.
3. From your Greenfoot lessons, where do you review a class’s inherited methods? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 3.
4. In Greenfoot, which of the following is the correct notation for calling a method for an instance of a class? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 5.
5. Which of the following comparison symbols represents equals? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 5.
6. Read the following method signature. Using your Greenfoot experience, what does this method do?
public static int getRandomNumber (int limit) Mark for Review
(1) Points
Returns a random number less than 10.
Returns a random coordinate position in the world.
Returns a random number for instances in the animal class only.
Returns a random number between zero and the parameter limit. (*)
[Incorrect] Incorrect. Refer to Section 3 Lesson 5.
7. From your Greenfoot lessons, which type of constructor can be used to automate creation of Actor instances? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 5.
8. In Greenfoot, a subclass is created by right-clicking on a superclass. True or false? Mark for Review
(1) Points
9. Which of the following demonstrates a Greenfoot subclass/superclass relationship? Mark for Review
(1) Points
A computer is a subclass of a video game superclass.
A dog is a subclass of the cat superclass.
A rose is a subclass of the flower superclass. (*)
A single person is a superclass of the human subclass.
[Incorrect] Incorrect. Refer to Section 3 Lesson 1.
10. In Greenfoot, after a subclass is created and compiled, you cannot edit the subclass’s source code. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 1.
11. An object is an instance of a class. True or false? Mark for Review
(1) Points
12. In Greenfoot, the move method expects what type of information in its parameters? Mark for Review
(1) Points
Integer of steps to move forward (*)
True or false response
Degrees to turn
13. In the Greenfoot IDE, an instance’s position is on the x and y coordinates. True or false? Mark for Review
(1) Points
14. A variable is also known as a ____________. Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 2.
15. In Greenfoot, methods can be called in the act method. When the Act button is clicked in the environment, the methods in the method body of the act method are executed. True or false? Mark for Review
(1) Points
[Correct] Correct
1. In Greenfoot, you can cast an Actor class to a World class? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 9.
2. From your Greenfoot lessons, abstraction techniques can only be used once in a class’s source code. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 9.
3. In Greenfoot you can only access the methods of the current class? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 9.
4. Which method is used to play sound in your Greenfoot game? Mark for Review
(1) Points
5. From your Greenfoot lessons, the isKeyDown method is located in which class? Mark for Review
(1) Points
[Correct] Correct
6. In Greenfoot, what type of parameter does the isKeyDown method expect? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 7.
7. From your Greenfoot lessons, to save space in the act method, you can write an entirely new method below it, called a _____________. Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 6.
8. In Greenfoot, a way to have all subclasses of a superclass inherit a method is by adding the method to the superclass. True or false? Mark for Review
(1) Points
9. To execute a method in your Greenfoot game, where is it called from? Mark for Review
(1) Points
The actor class
[Incorrect] Incorrect. Refer to Section 3 Lesson 6.
10. In the Greenfoot IDE, what symbols indicate that the variable is an array? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 10.
11. Which of the following is not a component of a while loop? Mark for Review
(1) Points
12. In Greenfoot, what happens if the end to a while loop isn’t established? Mark for Review
(1) Points
The code will keep executing and will never stop. (*)
The code will prompt you to enter a loop counter.
The code will execute once and then stop, due to controls in Greenfoot.
The code will not execute.
13. From your Greenfoot lessons, what is the parameter of the following constructor that creates a new image, and designates it to the Actor class?
setImage (new GreenfootImage(«Bee01.png»)); Mark for Review
(1) Points
14. Which operator is used to test if values are equal? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
14. Which of the following could be a reason to use a switch statement in a Java program? Mark for Review
(1) Points
Because it terminates the current loop.
Because it allows the code to be run through until a certain conditional statement is true.
Because it allows the user to enter an input in the console screen and prints out a message that the user input was successfully read in.
Because it allows the program to run certain segments of code and neglect to run others based on the input given. (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
15. How would you use the ternary operator to rewrite this if statement?
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
1. In a project, 1 of the classes must contain a main method. True or False? Mark for Review
(1) Points
2. When converting gallons to liters its best to put the calculation result into a variable with a _______________ data type. Mark for Review
(1) Points
None of the above
[Incorrect] Incorrect. Refer to Section 4 Lesson 1.
3. When converting gallons to liters its best to put the calculation result into a variable with a _______________ data type. Mark for Review
(1) Points
None of the above
[Incorrect] Incorrect. Refer to Section 4 Lesson 1.
4. Which of the following is the name of a Java primitive data type? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 4 Lesson 3.
5. What is the output of the following lines of code?
int j=7,k=5,m=8,result;
result=j/m*k;
System.out.println(result); Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 4 Lesson 3.
6. A local variable has precedence over a global variable in a Java method. True or false? Mark for Review
(1) Points
7. Which line of Java code properly calculates the area of a triangle using A=1/2(b)(h) where b and h are Java primitive integers? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 4 Lesson 3.
8. Given the following declaration, which line of Java code properly casts one type into another without data loss?
int i=3,j=4; double y=2.54; Mark for Review
(1) Points
double x= double i/j;
[Incorrect] Incorrect. Refer to Section 4 Lesson 3.
9. When importing another package into a class you must import the entire package as well as the package classes that will be called. True or False? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 4 Lesson 2.
10. The following defines a package keyword: Mark for Review
(1) Points
Provides the compiler information that identifies outside classes used within the current class.
Defines where this class lives relative to other classes, and provides a level of access control. (*)
Precedes the name of the class.
[Incorrect] Incorrect. Refer to Section 4 Lesson 2.
11. Which of the two diagrams below illustrate the general form of a Java program?
Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 4 Lesson 2.
12. The following defines a class keyword: Mark for Review
(1) Points
Defines where this class lives relative to other classes, and provides a level of access control.
Provides the compiler information that identifies outside classes used within the current class.
Precedes the name of the class. (*)
[Incorrect] Incorrect. Refer to Section 4 Lesson 2.
13. What is printed by the following code segment?
Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 4 Lesson 4.
14. Which of the following creates a String reference named str and instantiates it? Mark for Review
(1) Points
String str=new String(«str»); (*)
[Incorrect] Incorrect. Refer to Section 4 Lesson 4.
15. Consider the following code snippet.
What is printed? Mark for Review
(1) Points
ArrayIndexOutofBoundsException is thrown
Kunci Jawaban All Quiz Oracle Academy Java Fundamental 2017 Part 10
1. The Alice move procedure contains which arguments? Mark for Review
(1) Points
(Choose all correct answers)
[Incorrect] Incorrect. Refer to Section 2 Lesson 3.
2. When you disable a programming instruction, it is still executed when you run the Alice animation. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 3.
3. Which of the following would not be an argument in an Alice programming instruction that commands a person object to move forward 2 meters? Mark for Review
(1) Points
Direction to move
Distance to move forward
Number of seconds to execute the programming instruction
[Incorrect] Incorrect. Refer to Section 2 Lesson 3.
4. Functions answer questions about an object, such as its height, width, depth and even distance to another object. True or false? Mark for Review
(1) Points
5. From your Alice lessons, which programming instruction represents the following movement: A cat moves forward double (or twice) the distance to the tree. Mark for Review
(1) Points
this.Cat move forward this.Cat getDistanceTo this.tree + 2.0
this.Cat move Forward this.Cat getDistanceTo this.tree * 2.0 (*)
this.Cat move Forward this.Cat getDistanceTo this.tree / 2.0
[Incorrect] Incorrect. Refer to Section 2 Lesson 7.
6. Which Alice execution task corresponds with the following storyboard statement?
Cat turns to face mouse. Mark for Review
(1) Points
cat TurnTo mouse
this.mouse turnToFace this.cat
this.cat turnToFace this.mouse (*)
mouse turnTo cat
[Incorrect] Incorrect. Refer to Section 2 Lesson 4.
7. In Alice, which of the following arguments could be replaced with a random number? Mark for Review
(1) Points
(Choose all correct answers)
[Incorrect] Incorrect. Refer to Section 2 Lesson 4.
8. In Alice, which procedure is used to assign one object as the vehicle of another? Mark for Review
(1) Points
9. In Alice, the setVehicle procedure will associate one object to another. True or false? Mark for Review
(1) Points
10. What do moving objects provide to your scene? Mark for Review
(1) Points
The sky and ground
The non-moving scenery
[Incorrect] Incorrect. Refer to Section 2 Lesson 1.
11. Which Alice tool is used to demonstrate the process flow of an animation? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 5.
12. In Alice, what are the forms of a scenario? Mark for Review
(1) Points
(Choose all correct answers)
A problem to solve. (*)
A system to start.
A task to perform. (*)
A person to help.
A section of code to write.
[Incorrect] Incorrect. Refer to Section 2 Lesson 5.
13. From your Alice lessons, inheritance means that the superclass inherits its traits from the subclass. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 5.
14. All objects in Alice have three dimensional coordinates on which axes? Mark for Review
(1) Points
(Choose all correct answers)
All of the above
[Incorrect] Incorrect. Refer to Section 2 Lesson 2.
15. Rings will appear around a sub-part indicating how you can reposition it. True or false? Mark for Review
(1) Points
[Correct] Correct
1. A loop can be infinite (continue forever) or conditional (stops upon a condition). True or false? Mark for Review
(1) Points
2. In Alice, we use the While control statement to implement the conditional loop. True or false? Mark for Review
(1) Points
3. Consider the following: You want to use the BufferedReader and InputStreamReader classes to get user input from the command line. Which one of the following import statements must you use? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 14.
4. In Java, a function is a method that must return a value. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 14.
5. Which one of the following event listener types is not available at the top-level of the addEvent drop down list in Alice? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 11.
6. In Alice it is not possible to transfer a class from one animation to another. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 11.
7. The animation checklist helps you confirm that all elements of the Alice animation are operating as expected. True or false? Mark for Review
(1) Points
8. The Alice animation should be tested throughout development, not just at the end of the animation’s development. True or false? Mark for Review
(1) Points
9. When presenting your Alice animation, it is not important to give the audience a reason to listen to the presentation. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 12.
10. If a value has been assigned to (is stored in) a variable, that value will be overwritten when another value is assigned to the variable using the assignment «=» operator. True or false? Mark for Review
(1) Points
11. A data type defines the type of procedures a variable can store. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 13.
12. In Alice, which of the following programming statements moves the butterfly forward, double the distance to the tree? Mark for Review
(1) Points
this.Butterfly move backward
[Incorrect] Incorrect. Refer to Section 2 Lesson 9.
13. In Alice, which of the following programming statements moves the fish forward, the distance to the rock, minus the depth of the rock? Mark for Review
(1) Points
this.Fish move forward
this.Rock move forward
this.Fish move forward
[Incorrect] Incorrect. Refer to Section 2 Lesson 9.
14. Which is an example of the Boolean variable type? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 10.
15. In Alice, you can access the Java on the side option through which menu option? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 2 Lesson 10.
1. From your Greenfoot lessons, an instance inherits all of the characteristics of the class, and those characteristics cannot be changed. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 1.
2. A subclass has what kind of relationship to a superclass? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 1.
3. In Greenfoot, a subclass is a specialization of a superclass. True or false? Mark for Review
(1) Points
4. From your Greenfoot lessons, a scenario is a game or simulation implemented in Greenfoot. True or false? Mark for Review
(1) Points
5. In Greenfoot, the move method expects what type of information in its parameters? Mark for Review
(1) Points
True or false response
Integer of steps to move forward (*)
Degrees to turn
[Incorrect] Incorrect. Refer to Section 3 Lesson 2.
6. Using the Greenfoot IDE, only five instances can be added to a scenario. True or false? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 2.
7. In Greenfoot, methods can be called in the act method. When the Act button is clicked in the environment, the methods in the method body of the act method are executed. True or false? Mark for Review
(1) Points
8. In the Greenfoot IDE, an instance’s position is on the x and y coordinates. True or false? Mark for Review
(1) Points
9. The list below displays components of the Greenfoot source code editor except one. Which one should be removed? Mark for Review
(1) Points
[Incorrect] Incorrect. Refer to Section 3 Lesson 3.
10. From your Greenfoot lessons, source code is written in the Code editor. True or false? Mark for Review
(1) Points
11. Which keyword indicates that Greenfoot needs to create a new object? Mark for Review
(1) Points
12. An if-else statement executes its first code block if a condition is true, and its second code block if a condition is false, but not both. True or false? Mark for Review
(1) Points
In the greenfoot ide what does the and operator do
Method signatures
For each of these signatures, answer the fllowing questions:
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.
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.
Tutorial 1: Interacting with Greenfoot
This tutorial will explain the basics of the Greenfoot interface, and interacting with Greenfoot.
The Greenfoot interface
This tutorial uses a scenario called ‘wombats’ which you can download here (it is also included in the example scenarios with Greenfoot versions before 2.4.0). Open the wombats scenario in Greenfoot; you should then see this:
If you don’t see the world, and the classes on the right have diagonal slashes over them, this is because the code is uncompiled. Click on the «Compile» button in the bottom-right.
Below the world are the Execution Controls (the area with the ‘Act’ and ‘Run’ buttons and the slider). Let’s label all these things on our interface:
Place objects into the world
We will now place some objects into the world. Right-click (on Mac, Control-click whenever this tutorial says right-click) the Wombat class in the class display. You will see a pop-up menu like this:
Choose ‘new Wombat()’ from the menu. Then click anywhere in the world. You have just created a wombat (in Java terms: an object) and placed it into the world.
Wombats eat leaves, so let’s put some leaves into the world as well. Right-click the Leaf class, select ‘new Leaf()’ and place the leaf into the world.
There is a shortcut to place several objects a bit faster: shift-clicking into the world. Make sure the Leaf class is selected (left click on it in the classes panel, and it will get a thicker black border), then hold down the Shift key and left-click in the world several times. You will place one object of the selected class at every click. Much faster!
Make objects act
Run a scenario
Click the ‘Run’ button. This is equivalent to clicking the ‘Act’ button over and over again, very quickly. You will notice that the Run button changes to a ‘Pause’ button; Clicking Pause stops everything acting.
The slider next to the Act and Run buttons sets the speed. Click Run and then change the slider, and you’ll see the difference.
Invoke methods directly
Instead of just running the whole scenario, you can also invoke single methods. A method is a single action that an object can perform.
Make sure you have a wombat in the world, and the scenario is not running. Then right-click on the wombat (the one in the world, not the Wombat class), and you will see that objects in the world also have a pop-up menu:
You can select any of the methods shown here to ask the wombat to do something. Try, for example, turnLeft(). Selecting this from the menu tells the wombat to turn to its left. Try move() as well.
Some methods give you an answer. getLeavesEaten(), for example, will tell you how many leaves this wombat has eaten so far. Try it. Then get the wombat to eat another leaf, and try calling that method again.
You will also notice a method called ‘act()’. This method is called every time you click the Act button. If you want just one object to act instead of all the objects in the world, you can do this by invoking the object’s act() method directly.
Create a new world
Invoke a world method
We have seen that objects in the world have methods which you can invoke via a pop-up menu. The world itself is also an object with methods that you can invoke. Right-click on any empty space in the world, or in the grey area immediately next to the world, and you will see the world’s menu:
One of the methods in this menu is ‘populate()’. Try it out. It is a method that creates several leaves and wombats and places them into the world. You can then run the scenario.
Another world method is ‘randomLeaves(int howMany)’. This method places some leaves in the world at random locations. Note that this method has some words between the parenthesis after its name: ‘int howMany’. This is called a ‘parameter’. It means that you must specify some additional bit of information when you invoke this method. The term ‘int’ tells you that a whole number is expected, and the name ‘howMany’ suggests that you should specify how many leaves you want. Invoke this method. A dialogue will pop up that lets you enter a value for this parameter. Enter a number (say: 12) and click Ok.
(You may notice, if you count, that it sometimes appears as if fewer than the specified number of leaves were created. This is because some leaves may be at the same location, and are lying on top of each other.)
How to access one object from another
A common question that Greenfoot beginners ask is:
How can I access the variables (or methods) of one object/class from another one?
But of course usually it is phrased differently. sometimes beginners don’t yet understand what «variables» or «objects» are. So the question might look more like this:
How can I update the score counter whenever an asteroid is destroyed by the rocket?
Now, take a look at the Greenfoot scenario pictured above:
tut-access-p1 (right-click and open the link in a new window or tab).
This scenario is a simple game where you press the space bar to fire shots from a rocket at the asteroids that pass by overhead. There is a score counter, but it is currently not functional. Click on the «open scenario in Greenfoot» link to download a copy of the scenario to your computer, and start working on it.
In this example, there are several classes, including «Counter», «Rocket» and «Shot». The Counter has a method called «bumpCount()» which increases the score which the counter displays:
If you don’t understand that code, don’t worry about it for now. The important thing to know is: if you call the bumpCount() method, it will increase the number which the counter displays by the amount you specify. (Try it out! Right-click the counter object in the world, choose the «bumpCount(int)» method, and specify a number).
Now, in the Shot class, we might have a method which is called (from the act() method) whenever an asteroid is hit:
Open the editor for the Shot class (double-click the «Shot» class at the right).
Now the question is: how can I call the «bumpCount» method, which is in the Counter class, from the Shot class?
If we simply try to call bumpCount() in the normal way, we’ll get an error when we try to compile:
Before continuing, remove the «bumpCount()» method call from the hitAnAsteroid() method so that the code compiles cleanly again.
Solution: Store the reference in the world when creating the object
In this solution we’ll store a reference to the counter in the world, then retrieve it from the shot when we need to.
Take a look at the Space constructor:
We need to do two things: firstly, declare a field to store the reference to the Counter, and then assign that variable when the counter is created. Change the Space constructor so that it looks like this:
Make sure your code compiles before you continue.
Notice that we added a declaration for a variable called «theCounter». This is an instance variable, meaning that we want the keep the value for as long as the world exists, and so it is declared outside the SpaceWorld constructor (but inside the SpaceWorld class).
Now, add a method to the Space class to retrieve the value of «theCounter» so it can be accessed by the rocket:
We can then access the world from the shot, and then call the getCounter method on the world to obtain a reference to the counter; finally, we can call the bumpCount method on the counter reference! The hitAnAsteroid method in the Shot class should then look like this:
Go ahead and make the same changes in your copy of the scenario. Make sure it compiles!
And that’s it! We have a solution to the problem. When the shot needs to increment the counter, it asks the world for a reference to the counter, and then modifies the counter using that reference. Run your modified scenario and make sure it works correctly.
To see the completed solution, look at the source code here: