Question: Java programming In an ancient land, a King had many prisoners. He decided on the following procedure to determine which prisoner to grand freedom. First,

Java programming

In an ancient land, a King had many prisoners. He decided on the following procedure to determine which prisoner to grand freedom. First, all of the prisoners would be lined up one after the other and assigned numbers. The first prisoner would be number 1, the second number 2, and so on up to the last prisoner, number n . Starting at the prisoner in the first position, he would then count k prisoners down the line, and the kth prisoner would be eliminated from winning her/his freedom removed from the line. The King would then continue, counting k more prisoners, and eliminate every kth prisoner. When he reached the end of the line, he would continue counting from the beginning. For example, if there were six prisoners, the elimination process would proceed as follows (with step k=2): 123456 Initial list of prisoners; start counting from 1. 12456 Prisoner 3 eliminated; continue counting from 4. 1245 Prisoner 6 eliminated; continue counting from 1. 125 Prisoner 4 eliminated; continue counting from 5. 15 Prisoner 2 eliminated; continue counting from 5. 1 Prisoner 5 eliminated; 1 is the lucky winner.

Write a program that creates a circular linked list of nodes to determine which position you should stand in to win your freedom if there are n prisoners. Your program should simulate the elimination process by deleting the node that corresponds to the prisoner that is eliminated for each step in the process. Create appropriate JUnits to test your program.

Instructions for developing JUnit:

Upon creation of linked list,

Check if linked list is empty using assertTrue.

Check if length is 0 using assertEquals.

Similarly, after adding elements, linked list should not be empty and size not equal to 0.

Sample code:

@Test

public void test() {

linkedList prisoners=new linkedList();

assertTrue(prisoners.isEmpty()); //before inserting, list is empty

assertEquals(0, prisoners.size); // Size is 0

prisoners.insert(5);

assertFalse(prisoners.isEmpty()); // after inserting element, list is not empty

assertEquals(1,prisoners.size); //size is 1

Test cases:

1. n 123456, k - 2

Output 1

2. n 1, k - 9

Output 1

3. n 1234567, k - 7

Output 4

4. n 12, k - 8

Output 2

5. n 12345, k - 1

Output 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!