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 ******...
Program using java Take user input and give corresponding output. A user is considering different options...
Program using java Take user input and give corresponding output. A user is considering different options of operating air conditioning. Depending on room temperature (here, room temperature is given by user), this program should give different instructions. There are three scenarios. - If temperature is above 90, the program should output “cooling”. -If the temperature is below 70, the program should output “heating”. -Otherwise, the program should output “stopped”. For example, if user enters “95”, this is how the program...
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
Define a java problem with user input, user output, Do While Statement and some mathematical computation....
Define a java problem with user input, user output, Do While Statement and some mathematical computation. Write the pseudocode, code and display output.
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!
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
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.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT