Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/********************************************** * COMP 2140 Lab 4 Winter 2020 * * Simulating a simple survivor game with a queue. * * N people (numbered 0 to
/********************************************** * COMP 2140 Lab 4 Winter 2020 * * Simulating a simple survivor game with a queue. * * N people (numbered 0 to N-1) stand in circle, * then every k-th person is counted out (leaves * the circle), until only one person is left. * * Student name: * Student id: * **********************************************/ public class Lab4 { // The main method simply tests the survivor method with several sets of parameters. public static void main( String[] args ) { System.out.println( " COMP 2140 Lab 4 Fall 2015 " ); survivor( 7, 2 ); System.out.println( " " ); survivor( 11, 3 ); System.out.println( " " ); survivor( 17, 5 ); System.out.println( " Program ends normally. " ); } // end main // Simulates the survivor game using a queue for the circle of people. public static void survivor( int numPeople, int k ) { Queue circle; int person; if ( numPeople > 0 && k > 1 ) { System.out.print( numPeople + " people numbered 0 to " + (numPeople-1) + " stand in a circle. Every " ); if ( k == 2 ) System.out.print( "second" ); else if ( k == 3 ) System.out.print( "third" ); else System.out.print( k + "-th" ); System.out.println( " person is eliminated repeatedly until only one person is left." ); System.out.print( "The people are eliminated in the following order: " ); // Set up queue: add people 0 to numPeople-1 circle = new Queue(); for ( int i = 0; i
Lab 4: Simulating a Simple Survivor Game with a Queue Week of March 3, 2020 Objective To simulate a simple survivor game with a queue. Exercise File Lab4.java. contains an almost complete application with a simple Queue. You need to add most of the body of the method survivor, which currently only prints some information The survivor mcthod should simulate (using & cucuc) the survivor purc. In this purc, pcoplc (numbered 0 to N-1) stand in circle, then every kth person is counted out ( laves the circle), until only One person is lell. Alcach elimination, you should print out the eliminal al person's number. For example, if there are 7 people (N = 7) and k = 2, then you should print out 1 3 5 0 4 2 6 because the circle changes as follows: : GI First elimination: 1 Second elimination: 3 Third elimination: 5 Fourth elimination: 0 Fifth elimination: 1 Tliri elimination: 2 Last element remaining: G The code that you add to the survivor method mist simulate the game using a quelle to represent the circle of people. To do the eliminating, you must use the standard queue methods (enter, leave, front, and is Emptyl provided in the Queue cluss You mey not change the Queue class or the main method in any pople are left in the circle. Hint: You rzu kxp eliminating until Sample: output: COMP 2140 Lab 4 Winter 2020 7 people numbered 0 to 6 stand in a circle. Every second person is eliminated repeatedly until only one person is left. The people are eliminated in the following order: 1 3 5 0 4 2 6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started