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

Write a program that will play the childs game mother may i? The program should

ID: 3569992 • Letter: W

Question

Write a program that will play the childs game mother may i? The program should accept how many children are playing this round. The specified number of children should initially be the same distance from Mother, the computer. The program then processes each child in turn by inputting the request to move forward which consists of the type of step and how many. Have the computer randomly decide whether the child is allowed ( Yes, you may move n type steps) or not (No, you may not!). If the move is allowed the child moves that distance closer to Mother. Optionally output the distance moved this time and how many steps left. The game ends when a child reaches mother.

Explanation / Answer

import java.util.Scanner;

public class ChildGame {

   public static final int distance = 5; // distance to reach mother
   static int childDistance[]; //array to hold distance of children from mother
  
   public static void main(String[] args) {
       int rand; // to store yes(1) or no(0)
       boolean b = true;
       Scanner sc = new Scanner(System.in);
      
       System.out.print("Enter the number of children to play the round : ");
       int childs = sc.nextInt(); //prompting user to enter number of children
       childDistance = new int[childs]; //creating array for that number of children
      
       while(b)
       {
           for(int i = 1; i <= childs; i++)
           {
               System.out.println("child " + i + " is requesting mother...");
               rand = (int)(Math.random() * 2); // computer randomly decides whether the child is allowed or not
               System.out.println("Yes to move, No to stay : " + (rand == 1 ? 'Y' : 'N'));
               if(rand == 1)
               {                  
                   childDistance[i-1]++;
               }
               System.out.println("Distance between child " + i + " and mother is : " + (distance - childDistance[i-1]));
               System.out.println();
               if(childDistance[i-1] == distance)
               {
                   System.out.println("Child " + i + " is the winner.");
                   b = false;
                   break;
               }
           }
       }
       sc.close();
   }

}

Sample Run :

Enter the number of children to play the round : 2
child 1 is requesting mother...
Yes to move, No to stay : N
Distance between child 1 and mother is : 5

child 2 is requesting mother...
Yes to move, No to stay : Y
Distance between child 2 and mother is : 4

child 1 is requesting mother...
Yes to move, No to stay : N
Distance between child 1 and mother is : 5

child 2 is requesting mother...
Yes to move, No to stay : N
Distance between child 2 and mother is : 4

child 1 is requesting mother...
Yes to move, No to stay : Y
Distance between child 1 and mother is : 4

child 2 is requesting mother...
Yes to move, No to stay : N
Distance between child 2 and mother is : 4

child 1 is requesting mother...
Yes to move, No to stay : N
Distance between child 1 and mother is : 4

child 2 is requesting mother...
Yes to move, No to stay : Y
Distance between child 2 and mother is : 3

child 1 is requesting mother...
Yes to move, No to stay : Y
Distance between child 1 and mother is : 3

child 2 is requesting mother...
Yes to move, No to stay : N
Distance between child 2 and mother is : 3

child 1 is requesting mother...
Yes to move, No to stay : Y
Distance between child 1 and mother is : 2

child 2 is requesting mother...
Yes to move, No to stay : N
Distance between child 2 and mother is : 3

child 1 is requesting mother...
Yes to move, No to stay : Y
Distance between child 1 and mother is : 1

child 2 is requesting mother...
Yes to move, No to stay : Y
Distance between child 2 and mother is : 2

child 1 is requesting mother...
Yes to move, No to stay : Y
Distance between child 1 and mother is : 0

Child 1 is the winner.

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