Question

use java There are n students in a class and n numbered chairs. The instructor has...

use java

There are n students in a class and n numbered chairs. The instructor has assigned a presentation for each student and is going to select the order of presentations by the following scheme. A number m is chosen. Beginning at seat number 1, she counts to m. The student sitting in that seat presents first.
Then she starts counting seats again, starting from the next seat, wrapping around at the end, and skipping any student who has already been selected, until m is again reached. This student presents next. The process continues until all students have been selected. For example, if there were 5
students and the instructor selected m=3, then the order would be (3,1,5,2,4).

Write an application program that will take n and m as input from the keyboard and output the order of presentations. This is what is usually referred to as a Josephus permutation. There are some mathematical tricks to finding the permutation quickly (or at least finding the last number of the sequence), but I want you to implement this by simulation using a queue to represent the data.
You should start with a queue containing the first n integers. Numbers are removed
from the queue and reinserted until you reach the mth number. The mth number is then removed and inserted into the result queue. This process is repeated until the initial queue is empty. Then you simply dequeue and print each item from the result queue.

For example, with our example of 5 students with m=3

Class queue Result queue
1 2 3 4 5 (empty)
4 5 1 2 3
2 4 5 3 1
2 4 3 1 5
4 3 1 5 2
(empty) 3 1 5 2 4

So the resulting permutation, J(5,3) = {3,1,5,2,4}.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Using java’s built in Queue libraries to implement this. If you want this to be a custom made queue, let me know. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

// JosephusProblem.java

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

public class JosephusProblem {

      public static void main(String[] args) {

            // creating two queues, one for representing students in class, second

            // is for representing the order of presentations

            Queue<Integer> classQueue = new LinkedList<Integer>();

            Queue<Integer> resultQueue = new LinkedList<Integer>();

            // using a Scanner, reading the values for n and m

            Scanner scnr = new Scanner(System.in);

            System.out.print("Enter n and m: ");

            int n = scnr.nextInt();

            int m = scnr.nextInt();

            // adding numbers 1 to n to the class queue

            for (int i = 1; i <= n; i++) {

                  classQueue.add(i);

            }

            // looping until classQueue is empty

            while (!classQueue.isEmpty()) {

                  // looping for m-1 number of times

                  for (int i = 0; i < m - 1; i++) {

                        // removing front value from classQueue, adding to the rear

                        int value = classQueue.remove();

                        classQueue.add(value);

                  }

                  // removing m'th value into a variable

                  int removed = classQueue.remove();

                  // adding this value to the result queue

                  resultQueue.add(removed);

            }

            // now simply looping and printing result queue until it is empty

            System.out.print("The order: ");

            while (!resultQueue.isEmpty()) {

                  System.out.print(resultQueue.remove() + " ");

            }

            System.out.println();

      }

}

/*OUTPUT*/

Enter n and m: 5 3

The order: 3 1 5 2 4

Add a comment
Know the answer?
Add Answer to:
use java There are n students in a class and n numbered chairs. The instructor has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a history class is comprised of 8 students, 3 female, 5 male. if the instructor of...

    a history class is comprised of 8 students, 3 female, 5 male. if the instructor of the class randomly chooses 6 students from the class for an oral exam what is the probability that 2 female and 4 male students will be selected. round to 3 decimal places.

  • Java Programming (Queues and LinkedLists) The annual First robotics competition is hosted by SVSU...

    Java Programming (Queues and LinkedLists) The annual First robotics competition is hosted by SVSU every year, and each year students from different schools come in numbers to compete and show off their technical prowess.The  winner of the competition wins the prestigious Michigan state championship and bragging rights for the next 12 months. The first step to participate in the competition is to register yourself as an contestant. This can be done at the registration booth at SVSU, where contestant form...

  • Java Programming (Queues and LinkedLists) The annual First robotics competition is hosted by SVSU every year,...

    Java Programming (Queues and LinkedLists) The annual First robotics competition is hosted by SVSU every year, and each year students from different schools come in numbers to compete and show off their technical prowess.The  winner of the competition wins the prestigious Michigan state championship and bragging rights for the next 12 months. The first step to participate in the competition is to register yourself as an contestant. This can be done at the registration booth at SVSU, where contestant form a...

  • Exercise 5. (13pt) Consider a class of 16 students. Every week 4 students have to give presentation. At the end of the...

    Exercise 5. (13pt) Consider a class of 16 students. Every week 4 students have to give presentation. At the end of the course, each pair of students has done exactly one presentation together (1) How many weeks does the course have? (Hint: use double counting) (4pt) (2) How can you interpret this as a block system? (4pt) (3) What is the total number of block in this system and what does it represent? Compare this to your answer in par...

  • 10. On the 1st day of class, after instructor A introduces themselves, the instructor presents students...

    10. On the 1st day of class, after instructor A introduces themselves, the instructor presents students with a syllabus to students about to take the class. Select each of the answers (3) that arguably would not be seen as the main motivation/reason behind deciding to communicate such information? (* You must select all the correct answers to get the points associated with the question) 1. Information Gathering 2. Establishing Identity 3. Building a Context of Understanding 4. Interpersonal Needs 11....

  • You have to use a h file for class definitions and two (as specified below) cpp...

    You have to use a h file for class definitions and two (as specified below) cpp file for the C++ implementation. You have to generate the list from scratch and check for overflow I will be giving extra points (up to 2 points this time) to students that exceed the requirements posed in the assignments. For example, extensive testing, checking for exceptions, and usable user interface. Please submit enough screenshots showing compiling and execution under Linux. Write a main program...

  • Need different C code then what has been posted here Problem In this assignment, you have...

    Need different C code then what has been posted here Problem In this assignment, you have to simulate the Josephus problem. There are n number of prisoners standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller...

  • In Java Programming: Create a flow chart for a program that prompts for an integer N...

    In Java Programming: Create a flow chart for a program that prompts for an integer N (number of students) and M (number of test scores for each students), and allows the user to N*M real numbers. The program then calculates the average for each student and class average. (1) Prompt the user to input N students and M test scores, then display the inputs. Enter number of students: You entered: 2 Enter number of test scores: You entered: 3 Enter...

  • JAVA Programming Assignment: You run four computer labs. Each lab contains computer stations that are numbered...

    JAVA Programming Assignment: You run four computer labs. Each lab contains computer stations that are numbered as shown in the table below:- Lab Number   Computer Station Numbers 1   1-5 2   1-6 3   1-4 4   1-3 Each user has a unique five-digit ID Number. Whenever a user logs on, the user's ID, Lab Number, and the computer station are transmitted to your station. For example, if user 49193 logs onto station 2 in Lab 3, your system receives (49193, 2, 3)...

  • Questions 1-4: There are 1300 students taking an introductory class, and we'd like to know the...

    Questions 1-4: There are 1300 students taking an introductory class, and we'd like to know the proportion of these students that are freshmen. We draw a random sample of size 100. We ask each of these 100 students whether they are freshmen. We then find the proportion in this sample that are freshmen. 1. What is the population?: * O a. The 1300 total students enrolled Ob. The 100 students selected Oc. Whether or not a student is a freshman...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT