Question

In: Computer Science

java.. please dont change the format and give me an output sample! user need to input...


java.. please dont change the format and give me an output sample! user need to input k.

public class Josephus {
   /**
   * All persons sit in a circle. When we go around the circle, initially starting
   * from the first person, then the second person, then the third...
   * we count 1,2,3,.., k-1. The next person, that is the k-th person is out.
   * Then we restart the counting from the next person, go around, the k-th person
   * is out. Keep going the same way, when there is only one person left, she/he
   * is the winner.
   *
   * @parameter persons an array of string which contains all player names.
   * @parameter k an integer specifying the k-th person will be kicked out of the game
   * @return return a doubly linked list in the order when the players were out of the game.
   * the last one in the list is the winner.
   */
   public DoublyLinkedList<String> order(String[] persons, int k ) {
       //TODO: implement this method with the help of CircularArrayQueue
       return null;
   }  
}

Solutions

Expert Solution

Working code implemented in Java and appropriate comments provided for better understanding.

Source Code for Josephus.java:

public class Josephus {
  
   /**
   * All persons sit in a circle. When we go around the circle, initially starting
   * from the first person, then the second person, then the third...
   * we count 1,2,3,.., k-1. The next person, that is the k-th person is out.
   * Then we restart the counting from the next person, go around, the k-th person
   * is out. Keep going the same way, when there is only one person left, she/he
   * is the winner.
   *
   * @parameter persons an array of string which contains all player names.
   * @parameter k an integer specifying the k-th person will be kicked out of the game
   * @return return a doubly linked list in the order when the players were out of the game.
   * the last one in the list is the winner.
   */
   public DoublyLinkedList<String> order(String[] persons, int k ) {
       //TODO: implement this method with the help of CircularArrayQueue
       CircularArrayQueue<String> inGame = new CircularArrayQueue(persons.length);
       DoublyLinkedList<String> outPut = new DoublyLinkedList();
       for(int i=0; i < persons.length;i++) {//used to load all data into an array
           inGame.enqueue(persons[i]);
       }
       while (!inGame.isEmpty()) {
           int current =0;
           for(int i = 0; i < k-1; i++) { //Cycles through the data
               String temp = inGame.dequeue();
               inGame.enqueue(temp);
               current++;
           }
           String temp = inGame.dequeue();//cuts the k-th person from the list
           outPut.addLast(temp);   
       }   
       return outPut;
   }  
}

Sample Output Screenshots:


Related Solutions

hi i need a code that will give me this output, For the multiply_list, the user...
hi i need a code that will give me this output, For the multiply_list, the user will be asked to input the length of the list, then to input each element of the list. For the repeat_tuple, the user is only asked to enter the repetition factor, but not the tuple. Your program should take the list created before and convert it to a tuple. output expected: (**user input**) ******Create your List ****** Enter length of your list: 3 ******...
Define a problem with user input, user output, -> operator and destructors. C ++ please
Define a problem with user input, user output, -> operator and destructors. C ++ please
5. Take user input and give corresponding output. User will enter a sentence. The program will...
5. Take user input and give corresponding output. User will enter a sentence. The program will output the word that appears most frequently in this sentence. If there are multiple words with same frequency, output the first of these words. Please enter a sentence: I like batman because batman saved the city many times. The most frequent word is: batman The frequency is: 2 PYTHON
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
please run it and show a sample. please dont change the methods. public interface PositionalList extends...
please run it and show a sample. please dont change the methods. public interface PositionalList extends Iterable { /** * Returns the number of elements in the list. * @return number of elements in the list */ int size(); /** * Tests whether the list is empty. * @return true if the list is empty, false otherwise */ boolean isEmpty(); /** * Returns the first Position in the list. * * @return the first Position in the list (or null,...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives • Be able to write methods • Be able to call methods Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct...
give me an example of fully funded defined benefit plan please dont use hand writing
give me an example of fully funded defined benefit plan please dont use hand writing
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT