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
package cs146S19.Baral.project1;
public class CircularLinkedList {
// circular linked list has a reference to the first and the last node
private Node start;
private int count;
//method for adding the new node in the linked list
public void add(int data) {
count++;
Node temp1 = new Node(data);
if (start == null) {
start = temp1;
}
else {
Node temp2 = start;
while (temp2.link != start) {
temp2 = temp2.link;
}
temp2.link = temp1;
}
temp1.link = start;
}
//method for adding a head or start in the linked list
public void addStart(int data) {
count++;
Node temp = new Node(data);
if (start == null) {
temp.link = temp;
}
else {
Node temp1 = start;
while (temp1.link != start) {
temp1 = temp1.link;
}
temp1.link = temp;
temp.link = start;
}
start = temp;
}
//method for adding the node in the desired the position
public void addAt(int position, int data) {
Node temp, temp1;
temp = new Node(data);
temp1 = start;
for (int i = 0; i
if (temp1.link == start)
break;
temp1 = temp1.link;
}
temp.link = temp1.link;
temp1.link = temp;
count++;
}
//displaying the List
public String displayList() {
StringBuilder sb = new StringBuilder();
if (!isEmpty()) {
Node temp = start;
sb.append("[");
sb.append(temp.data).append(", ");
while (temp.link != start) {
temp = temp.link;
sb.append(temp.data).append(", ");
}
sb.append("]");
}
return sb.toString();
}
//methdo for deleting the node
public Integer deleteAt(int position) {
Node current = start;
Node previous = start;
Integer data = null;
for (int i = 0; i
if (current.link == start)
break;
previous = current;
current = current.link;
}
data = current.data;
if (position == 0) {
deleteFirst();
}
else {
previous.link = current.link;
}
count--;
return data;
}
//method for deleting the very first node
public Integer deleteFirst() {
Node temp = start;
Integer data = temp.data;
while (temp.link != start) {
temp = temp.link;
}
temp.link = start.link;
start = start.link;
count--;
return data;
}
//method to keep of count
public int getCount() {
return count;
}
//methods for the node if its empty
public boolean isEmpty() {
return start == null;
}
/ode class
package cs146S19.Baral.project1;
public class Node {
int data; //data of the node
Node link; //connection between node to node
public Node(int data) {
this.data = data; //instantiate the data
}
public Node(int data, Node link) {
//instantiatet the new node and data
this.data = data;
this.link = link;
}
}
}
//prisoner
package cs146S19.Baral.project1;
/*
*
*
* This class decides who is getting free from the prison
*/
public class Prisoners {
private CircularLinkedList prisoners = new CircularLinkedList();
private int index = 1;
public String loadPrisoners(int numberOfPrisoners) {
int count = 1; //setting counter value 1
while (count
if (prisoners.isEmpty()) {
prisoners.addStart(count++);
}
else {
prisoners.add(count++);
}
}
return prisoners.displayList();
}
//method for freeing prisoner
public Integer freePrisoners(int freePrisoner) {
for (int i = 0; i
index++;
}
return prisoners.deleteAt(index);
}
//method for displaying free prisoners
public String showPrisoners() {
return prisoners.displayList();
}
}
//JUNIT TEST
package cs146S19.Baral.project1; /** * * This class tests the CircularLinkedList * * */
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class FreePrisonerTest {
public static void main(String args[]) {
org.junit.runner.JUnitCore.main("Tests");
}
@Test
public void loadPrisonerTest1() {
Prisoners prisoners = new Prisoners();
String actualResult = prisoners.loadPrisoners(10);
assertEquals(expectedPrisoners1(), actualResult);
}
@Test
public void loadPrisonerTest2() {
Prisoners prisoners = new Prisoners();
String actualResult = prisoners.loadPrisoners(100);
assertEquals(expectedPrisoners2(), actualResult);
}
@Test
public void freePrisonerTest1() {
Prisoners prisoners = new Prisoners();
prisoners.loadPrisoners(100);
assertEquals(Integer.valueOf(5), prisoners.freePrisoners(5));
//assertEquals(expectedPrisoners3(), prisoners.showPrisoners());
}
@Test
public void freePrisonerTest2() {
Prisoners prisoners = new Prisoners();
prisoners.loadPrisoners(10);
assertEquals(Integer.valueOf(5), prisoners.freePrisoners(5));
assertEquals(expectedPrisoners4(), prisoners.showPrisoners());
assertEquals(Integer.valueOf(10), prisoners.freePrisoners(5));
assertEquals(expectedPrisoners5(), prisoners.showPrisoners());
}
private String expectedPrisoners1() {
return "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ]";
}
private String expectedPrisoners2() {
return "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,"
+ " 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, "
+ "47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,"
+ " 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,"//
+ " 95, 96, 97, 98, 99, 100, ]";
}
private String expectedPrisoners4() {
return "[1, 2, 3, 4, 6, 7, 8, 9, 10, ]";
}
private String expectedPrisoners5() {
return "[1, 2, 3, 4, 6, 7, 8, 9, ]";
}
}
Only problem is with the JUNIT test.
-freePrisonerTest1
-freePrisonerTest2
I
Runs: 4/4 X Errors: O Failures: 2 cs 146S1 9.Baral"project 1.FreePrisonerTest [Runner: JUnit 5] loadPrisonerTesti (0.000 s) loadPrisonerTest2 (0.000 s) dire ePrisonerTest 1 (0.000 s) :freePrisonerTest2 (0.000 s)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