Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java data structures Josephus queue. Gaurenteed thumbs up. Please use exact skeleton code included. Lab 3 - Queues In computer science and mathematics, the Josephus
Java data structures Josephus queue. Gaurenteed thumbs up. Please use exact skeleton code included.
Lab 3 - Queues In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game. The problem is named after Flavius Josephus, a Jewish historian living in the 19 century. According to Josephus' account of the Siege of Yodfat, he and his 40 soldiers were trapped in a cave by Roman soldiers. They chose suicide over capture, and settled on a serial method of committing suicide by drawing lots. Josephus states that by luck or possibly by the hand of God, he and another man remained until the end and surrendered to the Romans rather than killing themselves. Simply stated, there are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number k which indicates that k-1 persons are skipped and K" person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive. For example, if n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So the person at position 3 survives. There are multiple ways of solving this problem, however, we will use a queue to "execute" soldiers and record same. The basic procedure is as follows: 1. The user will enter a number of "soldiers" (numberSoldiers) and another number for the soldier to be executed (elimination Number). 2. Using the Java Queue interface, create a queue of Integer objects numbered 1 to numberSoldiers. Create a second empty queue of Integer objects to keep a record of those soldiers executed. 4. As a soldier is dequeued, he is either executed (and is added to the executed queue) or returns to the soldiers queue. Using the code below, complete and test the implementation. Note that is assumes a sole survivor. Modify it to print all survivors. import java.util. // Josephus Problem public class Josephus ! public static void main(String[] args) { Queue Integer soldiers - new Linkedlist Integer Queue Integer executed - new Linkedlist Integer>() int numberSoldiers, elimination Number Scanner keyboard new Scanner(System.in); System.out.print "Enter no. of soldiers: "); numberSoldiers = keyboard.nextInt(); System.out.print("Enter the elimination number: "); elimination Number - keyboard.nextInt(); for (int i -1; i () int numberSoldiers, elimination Number Scanner keyboard new Scanner(System.in); System.out.print "Enter no. of soldiers: "); numberSoldiers = keyboard.nextInt(); System.out.print("Enter the elimination number: "); elimination Number - keyboard.nextInt(); for (int i -1; i
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