Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

Assess the contributions of small businesses to our economy.

Answered: 1 week ago

Question

6. Conclude with the same strength as in the introduction

Answered: 1 week ago

Question

7. Prepare an effective outline

Answered: 1 week ago