![]() ![]() From the Class menu option, select the Print option to print your code for each class. Double click on the Cat World class to open the editor windows. When the object is created, the actor isn’t in a world, so it can’t get its position. You try to save the location of the actor in a variable. So it’s no in the world anymore, so you can’t get a reference to the world with this method anymore. Add your name and a brief description to the top of your program. You call ‘getWorld’ after you removed the actor from the world. Review your code and add comments to explain the actions taking place. The call uses the argument 8 to replace the first while loop and the argument 10 to replace the second loop. Replace the two while loops with method calls. If you add the actor in the UI, then they’ll disappear when you reset. So if you add the actor in the code, they’ll be placed automatically. Any time you click Reset, the Actor will be added. ![]() Write the method definition in the class Cat: private void twoPlayMoves(int howMany) count = 1 while (count <= how Many) 8. Creating a class instance (object) When the game updates in Greenfoot, the Actor will be added automatically. The method's signature had return type void and one parameter, let's call it how Many of type int. Instead of writing two almost identical while loops, we can write a method let's call it twoPlayMoves. How similar are the two while loops you wrote? What makes them different? Answer: number of times the code in the for-loop is executed repeatedly (8, first for-loop, and 10, second for-loop). Is there another opportunity to replace repetitive code with a while lago Comment it out and write a while loop instead. Test your code that uses a while loop with 4 statements instead of 4 statements repeated 8 times 6. Increment your counter variable with the statement 5. Inside ( ) write the three calls that repeat so much: setImage("cat-play-3-piano.png") wait(6): setImage("cat-play-48-piano.png") wait(6) vi. You are not using 'world' (lines 72, 73, 82 and 83), 'food' (84 and 85) or 'superfood' (74 and 75) in the blocks they are declared in and 'getWorld' cannot be null until removal of the actor. Open bracket (start new line, and close) on another new line. Use 'else if' at line 80 so that collision checking (isTouching) is not attempted if the actor was removed by the first 'if' block. Open (and close ) and inside the parentheses write ne 8 iv. Initialize your counter variable with the statement: n=1 ii. Programming in Greenfoot at its most basic consists of subclassing two built-in classes, World and Actor. Greenfoot offers methods to easily program these actors, including method for movement, rotation, changes of appearance, collision detection, etc. Start a while loop on the line ABOVE the second call to play Sound: Greenfoot playSound("Snoopy.wav), which is online 104. The world and actors are represented by Java objects and defined by Java classes. Select these lines of code and use F8 key (or select Edit-Comment). Comment out the first block of code that uses these three lines repeatedly clines 71 to 102). We can make the cat do the same thing with a while loop in which we use each of the calls identified at #3 only once a. wait(6), and setImage("cat-play-4B-piano.png"). Work your way through the following tutorial to create a game similar to one required in the WJEC/Eduqas 2016 specification Component 2 exam. In Greenfoot modifying an actors constructor to accept an initial speed is a. This is called object-orientated programming (OOP) because objects are coded to interact in a visual environment. Use your Greenfoot knowldege: If an Actor class Fly has a variable defined. Which are the lines of code that repeat the most? Answer: setImage["cat-play-3-piano.png"). Greenfoot is software that uses the programming language Java to make simple games. When you have code that repeatedly does the same thing, you can use a while loop to shorten the text of code. A Wombat moves forward until it can't do so anymore, at which point it turns left. (World, Actor, GreenfootImage, and Greenfoot) import import / Wombat. Run the code and try to learn from what's printed out. We are going to alter the code to use while loops. Copy and paste all of the code below into the class. Describe what is happening in the body of the play method. The Greenfoot programming model consists of a World class (represented by a rectangular screen area) and any number of actor objects that are present in the world and can be programmed to act independently. On the cat object placed in the CatWorld, call the playPiano method. Lastly, I have a Button class.Transcribed image text: 2. Now I have another class, named Car and one subclass, Redcar. In Greenfoot, I have a world class, let's call it World. I'm very noobish when it comes to static/non-static, and I'm iffy on instances also. I'm using Greenfoot at the moment, learning Java. ![]()
0 Comments
Leave a Reply. |