Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help completing the code given instructions and classes ---------------------------- ButtonWorld class ----------------------------- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** *

I need help completing the code given instructions and classes

image text in transcribedimage text in transcribed

----------------------------

ButtonWorld class

-----------------------------

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/** * Write a description of this class here. */ public class ButtonWorld extends World {

/** * Constructor for objects of class ButtonWorld. * */ public ButtonWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } }

--------------------

Ball class

--------------------

import greenfoot.*;

/** * A ball with motion controlled by the keyboard and the ability * to remove buttons from the world. */ public class Ball extends Actor { /** * Responds to motion control from the arrow keys and removes * any buttons from the world when it intersects them. */ public void act() { handleKeyPress(); lookForButton(); }

/** * Checks to see if the ball is "touching" an object of the Button class. * If it is, then the ball removes from the World a Button that it touches. * If the ball isn't touching an object of the Button class, then this * method does nothing. */ public void lookForButton() { if (isTouching(Button.class)) { removeTouching(Button.class); } }

/** * Handles input from the arrow keys. */ public void handleKeyPress() { checkLeftArrow(); checkRightArrow(); checkUpArrow(); checkDownArrow(); }

/** * Checks to see if the left arrow key is being pressed. * If it is, then the ball sets its rotation angle to zero degrees * and moves backward 5 units. If it is not being pressed, * this method does nothing. */ public void checkLeftArrow() { if (Greenfoot.isKeyDown("left")) { setRotation(0); move(-5); } }

/** * Checks to see if the right arrow key is being pressed. * If it is, then the ball sets its rotation angle to zero degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkRightArrow() { if (Greenfoot.isKeyDown("right")) { setRotation(0); move(5); } }

/** * Checks to see if the up arrow key is being pressed. * If it is, then the ball sets its rotation angle to 270 degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkUpArrow() { if (Greenfoot.isKeyDown("up")) { setRotation(270); move(5); } }

/** * Checks to see if the down arrow key is being pressed. * If it is, then the ball sets its rotation angle to 90 degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkDownArrow() { if (Greenfoot.isKeyDown("down")) { setRotation(90); move(5); } } }

--------------------

Button Class

---------------------

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/** * A button that moves randomly around the World. */ public class Button extends Actor { /** * Changes the location of this button approximately * 2% of the time. */ public void act() { if (Greenfoot.getRandomNumber(100)

/** * Sets the location of this Button object to a new (x, y) * coordinate selected at random. The x coordinate wil be in * the range 100 to 500 inclusive, and the y coordinate will * be the range 100 to 300 inclusive. */ public void changeLocation() { setLocation(Greenfoot.getRandomNumber(401) + 100, Greenfoot.getRandomNumber(201) + 100); } }

1. Modify the constructor of the ButtonWorld class so that a single Ball object is placed at location 500, 400) and ten Button objects are placed at random locations throughout the world 2. Modify the Ball class so that it counts the number of Button objects it removes. Specifically: o You must declare an instance variable of type int in the Ball class for this purpose o You must initialize this instance variable to zero in the constructor of the Bal1 class. (You must write this constructor. It has not been provided for you.) o You must increment this instance variable at the appropriate point in the lookForButton method of the Ball class 3. Modify the Ball class so that it has two different images that it can use. Its default image, the one that it starts with, is steel-ball.png. The second image it can use is gold-ball.png. Specifically: o You must declare two instance variables of type Greenfoot Image in the Ball class for this purpose o You must initialize these instance variables to the appropriate images in the constructor of the Bal1 class o You must set the object's current image to steel-bai.png n the constructor of the Bali class 4. Modify the Ball class so that it changes its image to gold-ball.png as soon as it has removed five or more buttons. It keeps this image for the remainder of execution. Specifically: o You must modify the lookForButton method in the Ball class. 5. Modify the Ball class so that it stops the scenario execution once it has removed all ten Button objects Specifically: o You must create a method named checkToStop in the Ball class that checks to see if all ten buttons have been removed. If so, the scenario is stopped o You must modify the act method of the Ball class to call the checkToStop method 6. Modify the Button class so that it has two different images that it can use. Its default image, the one that it starts with, is button-red.png. The second image it can use is button-green.png. Specifically: o You must declare two instance variables of type GreenfootImage in the Button class for this purpose o You must initialize these instance variables to the appropriate images in the constructor of the Button class (You must write this constructor. It has not been provided for you.) o You must set the object's current image to button-red.png in the constructor of the Button class 1. Modify the constructor of the ButtonWorld class so that a single Ball object is placed at location 500, 400) and ten Button objects are placed at random locations throughout the world 2. Modify the Ball class so that it counts the number of Button objects it removes. Specifically: o You must declare an instance variable of type int in the Ball class for this purpose o You must initialize this instance variable to zero in the constructor of the Bal1 class. (You must write this constructor. It has not been provided for you.) o You must increment this instance variable at the appropriate point in the lookForButton method of the Ball class 3. Modify the Ball class so that it has two different images that it can use. Its default image, the one that it starts with, is steel-ball.png. The second image it can use is gold-ball.png. Specifically: o You must declare two instance variables of type Greenfoot Image in the Ball class for this purpose o You must initialize these instance variables to the appropriate images in the constructor of the Bal1 class o You must set the object's current image to steel-bai.png n the constructor of the Bali class 4. Modify the Ball class so that it changes its image to gold-ball.png as soon as it has removed five or more buttons. It keeps this image for the remainder of execution. Specifically: o You must modify the lookForButton method in the Ball class. 5. Modify the Ball class so that it stops the scenario execution once it has removed all ten Button objects Specifically: o You must create a method named checkToStop in the Ball class that checks to see if all ten buttons have been removed. If so, the scenario is stopped o You must modify the act method of the Ball class to call the checkToStop method 6. Modify the Button class so that it has two different images that it can use. Its default image, the one that it starts with, is button-red.png. The second image it can use is button-green.png. Specifically: o You must declare two instance variables of type GreenfootImage in the Button class for this purpose o You must initialize these instance variables to the appropriate images in the constructor of the Button class (You must write this constructor. It has not been provided for you.) o You must set the object's current image to button-red.png in the constructor of the Button class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Neo4j Data Modeling

Authors: Steve Hoberman ,David Fauth

1st Edition

1634621913, 978-1634621915

More Books

Students also viewed these Databases questions

Question

Identify conflict triggers in yourself and others

Answered: 1 week ago