Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a method in class Ground called turn() which is public, takes no paramete

ID: 3596753 • Letter: D

Question

Define a method in class Ground called turn() which is public, takes no parameters and returns nothing. This method allows the player to have his/her turn by asking the user to type in some command, either 'l' to move the player left, 'r' to move the player right, 'p' to pick up a rock at the current position or 'd' to drop a rock at the current position. Your method should follow exactly the following output format: Move (l/r/p/d): r Your program must print the prompt ”Move (l/r/p/d): ” and then read the user's input. 6. If the user types ‘l’ or ‘r’, your program should send a message to the player object to move one position to the left or right respectively. 7. If the user types ‘p’, your program should send a message to the player to pick up a rock from a jar on the ground at the current position. This command does nothing if the player’s jar already contains a rock. Note that you should apply good design principles to simplify your code, since you will also receive marks for the quality of your design. 8. If the user types ‘d’, your program should send a message to the player to drop a rock into a jar on the ground at the current position. This command does nothing if there is already a rock in a jar at that position. (You should define any additional methods as necessary)

Explanation / Answer

Hello there, your question is not very specific and it lacks information. First of all, you didn’t say in which language, the code should be implemented. Secondly, You only described about Ground class and a method called turn(). Also said ,there should be a calls to the Player object; which there’s no information about. So according to my understanding, I have created a Ground class in Java; and defined a function turn() which takes nothing and returns nothing. I have prompted the user to enter a choice "Move (l/r/p/d)" and read the input using a Scanner object. If the choice is ‘l’, a message is displayed on the screen that the player is moving left; and If the choice is ‘r’, a message is displayed on the screen that the player is moving right; Similarly, the other choices are displayed. Anyway, I have included comprehensive comments in each line; so that it’ll be easier for you to implement what you have wanted in your own way. And forgive me if this is not at all you wanted. Thanks J

//Ground.java file

import java.util.Scanner;

public class Ground {

      public static void main(String[] args) {

            Ground ground=new Ground(); /**creating a new Ground object*/

            ground.turn(); /**calling the turn() method*/

      }

      public void turn(){

            Scanner scanner=new Scanner(System.in); /**Scanner object to get the user input*/

            System.out.println("Move (l/r/p/d)"); /**prompting the player to enter the choice*/

            String choice=scanner.next(); /**reading the player's choice*/

            if(choice.equalsIgnoreCase("l")){

                  /**

                  * Implement the call to the Player object here, to move left

                  * For the time, I'm just printing it on screen

                  */

                  System.out.println("Player object moving to the left...");

            }else if(choice.equalsIgnoreCase("r")){

                  /**

                  * Implement the call to the Player object here, to move right

                  * For the time, I'm just printing it on screen

                  */

                  System.out.println("Player object moving to the right...");

            }else if(choice.equalsIgnoreCase("p")){

                  /**

                  * Implement the call to the Player object here, to pick up

                  * the rock; if the Player is not already holding a rock in

                  * the jar.

                  * probably like

                  * if(!player.isHoldingRock())

                  * {

                  *          player.pickTheRock(x,y); //where x and y are the coordinates of the player

                  * }

                  * For the time, I'm just printing it on screen

                  */

                  System.out.println("Player is picking the rock; if he's not holding one...");

            }else if(choice.equalsIgnoreCase("d")){

                  /**

                  * Implement the call to the Player object here, to drop

                  * the rock; if there is not a rock in a jar on the current

                  * position

                  * probably like

                  * if(!isThereArockInPosition(x,y)

                  * {

                  *          player.dropTheRock(x,y); //where x and y are the coordinates of the player

                  * }

                  * For the time, I'm just printing it on screen

                  */

                  System.out.println("Player is dropping the rock; if there's not a rock in the position...");

            } else{

                  System.out.println("Invalid choice");

            }

      }

}

//Outputs

Move (l/r/p/d)

l

Player object moving to the left...

Move (l/r/p/d)

r

Player object moving to the right...

Move (l/r/p/d)

p

Player is picking the rock; if he's not holding one...

Move (l/r/p/d)

d

Player is dropping the rock; if there's not a rock in the position...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote