Question

In: Computer Science

Description: The students in a class would really like it if Professor NotHearingIt would move an...

Description:

The students in a class would really like it if Professor NotHearingIt would move an homework assignment back a week. None of the students wish to confront the professor and so decided to get in a circle and remove every Nth student until only one is left. This student is the one who must go ask for the extension.

One student, Joseph, does not actually wish to move the assignment back. At the same time he does not want the other students to know that he is the only one who feels this way. He does not know what number N will be decided for deciding speaker until the process begins. He wants to know where to stand in the circle so that he will be chosen to go speak to the professor so the assignment will not change.

You are to build the simulation to help Joseph decided where to stand for any given N value.

Program:

You are to create a file called CircleQueue.java. You will create two class CircleQueue and Node. The queue will be a circular queue with the nodes storing the students’ names and starting position. This queue must be reference based since you will not know how many students to have at the beginning.

Your Node should be generic but have two generic values. The students name will be a string while starting position will be an integer, but the node should be generic and accept any two values.

You are given a file called names.txt. This file contains the name of each student on a line. You must read in each student and add him or her to the queue. The node created will contain the student’s name as well as some integer that keeps track of the order added to the queue as the starting position.

NOTE: this starting position will not change will NOT change after set. The first position will be 0 and the last will be size – 1.

Once the queue is filled you will go through and start removing students. First ask the user to enter the N value to be the counter for removing students (for example an N of two will remove person 2,4,6,etc…)

Start with the first person and dequeue him or her. If the person does not meet the order corresponding to the number, add the student back to the queue. If the student does correspond to the N value, excuse the student and do not add him or her back.

Do this until you are left with one student, and display the name and starting position of the student who must go talk to the professor.

Input:

There is an input file called names.txt were each line contains a name.

These are the names in the file:

Sammy
Susie
Angela S.
George
Thomas the Great
Jack Spratt
Steven
Chuckles the Clown
Joseph
Jim Bob
What's His Name
Eileen
Elizabeth Anne
Kenny
Daniel
Mr. Know-it-All
Ruth
Robert

Output:

Enter the number used to remove people from list: 2

Sammy is excused.

Susie is excused.

Angela S. is excused.

George is excused.

Thomas the Great is excused.

Jack Spratt is excused.

Steven is excused.

Chuckles the Clown is excused.

Joseph is excused.

Jim Bob is excused.

What's His Name is excused.

Eileen is excused.

Elizabeth Anne is excused.

Kenny is excused.

Daniel is excused.

Mr. Know-it-All is excused.

Ruth is excused.

Robert : 4 must go talk to the instructor.

Solutions

Expert Solution

public class CircleQueue {
  
   // Node class to store data
   static class Node
   {
       public String name;
       public int position ;
       public Node next;
       public Node( String name,int data )
       {
       this.name=name;
           this.position = data;
       }
   }
  
   /* Function to find the only person left
   after one in every m-th node is removed
   in a circle of n nodes */
   static void lastPosition(int m, int n,List<String> arr)
   {
       // Create a circular linked list of
       // size n.
       Node head = new Node(arr.get[0],0);
       Node prev = head;
       for(int i = 1; i < n; i++)
       {
           prev.next = new Node(arr.get(i),i);
           prev = prev.next;
       }
      
       // Connect last node to first
       prev.next = head;
      
       /* while only one node is left in the
       linked list*/
       Node ptr1 = head, ptr2 = head;
      
       while(ptr1.next != ptr1)
       {
          
           // Find m-th node
           int count = 1;
           while(count != m)
           {
               ptr2 = ptr1;
               ptr1 = ptr1.next;
               count++;
           }
          
           /* Remove the m-th node */
           ptr2.next = ptr1.next;
           ptr1 = ptr2.next;
       }
       System.out.println (ptr1.name+":"+ptr1.position+" must go talk to the instructor"); //last position
   }
  
   /* Driver program to test above functions */
   public static void main(String args[])
   {
   BufferedReader abc = new BufferedReader(new FileReader("names.txt"));
List<String> lines = new ArrayList<String>();
while((String line = abc.readLine()) != null) {
lines.add(line);
  
}
abc.close();
Scanner sc = new Scanner(System.in);
       int n = lines.size(), m =sc.nextInt();
       if(n!=0)
       lastPosition(m, n, lines); //pass list,listsize and m(positions to remove)
   }
}


Related Solutions

Professor Smith would like to see if giving the students chocolate made a difference in their...
Professor Smith would like to see if giving the students chocolate made a difference in their levels of happiness. The students were asked to rate how happy they felt on a 1(not happy at all) to 10(the happiest they have ever been) level before they were given chocolate and after. Please use the following data for questions a-d. Calculate the t value What are the critical values at the .05 and .01 significance levels? What is the interpretation of the...
A professor in the psychology dept at State U. would like to determine whether more students...
A professor in the psychology dept at State U. would like to determine whether more students have low grades in Statistics courses than in other courses in the dept. It is known that the overall grade distribution for the dept is 15% As, 25% Bs, 30% Cs, 20% Ds, and 10% Fs. A sample of 100 students taking statistics last year produced the following grade distribution for statistics grades (observed frequencies):      What specific statistical procedure would you use to test...
The professor would also like to know whether viewing the presidential debate would influence students’ intention...
The professor would also like to know whether viewing the presidential debate would influence students’ intention to vote and randomly assigns students to one of two groups. Group one viewed the last presidential debate of the major candidates while the second group did not view the debate. After the debate had aired, both groups were asked to report their intention to vote. The question was formatted in a five-point scale as follows: 1= “I do not intend to vote in...
A professor who teaches a large introductory statistics class (197 students) with eight discussion sections would...
A professor who teaches a large introductory statistics class (197 students) with eight discussion sections would like to test if student performance differs by discussion section, where each discussion section has a different teaching assistant. The summary table below shows the average score for each discussion section as well as the standard deviation of scores and the number of students in each section. Sec 1 Sec 2 Sec 3 Sec 4 Sec 5 Sec 6 Sec 7 Sec 8 ni...
Suppose a professor gives an exam to a class of 40 students and the scores are...
Suppose a professor gives an exam to a class of 40 students and the scores are as follows. (Type the data set in StatCrunch.) 35 44 46 47 47 48 49 51 53 54 55 55 57 57 57 58 59 59 59 59 60 60 60 60 60 62 62 62 64 68 69 70 72 73 73 75 75 77 82 88 Top 10% of scores receive an A Bottom 10% of scores receive an F Scores between...
A professor was curious as to whether the students in a very large class she was...
A professor was curious as to whether the students in a very large class she was teaching, who turned in their tests first, scored differently from the class mean on the test. The μ on the test was 75 with σ = 10; the scores were approximately normally distributed. The mean score for the first 20 tests was 78. Did the students turning in their tests first score significantly different from the larger mean at the .05 level? a) Use...
5.78 A selective college would like to have an entering class of 1000 students. Because not...
5.78 A selective college would like to have an entering class of 1000 students. Because not all studets who are offered admission accept, the college admits more than 1000. Past experience shows that about 83% of the students admitted will accept. The college decides to admit 1200 students.Assuming that students make their decisions independently, the number who accept his the B(1200, 0.83) distribution. If this number is less than 1000, the college will admit students from its waiting list. a...
An MPH professor claims that 50 % of the students in his class has a median...
An MPH professor claims that 50 % of the students in his class has a median weight different from 140 lb. He collects the weight of a random sample of 22 students. Enter the following data in SPSS and perform a binomial test using the standard method. Alpha level = 0.05 (Make sure to save your data before start analyzing the data). 135 119 106 135 180 108 128 160 143 175 170 205 195 185 182 150 175 190...
A nursing professor was curious as to whether the students in a very large class she...
A nursing professor was curious as to whether the students in a very large class she was teaching who turned in their tests first scored differently from the overall mean on the test. The overall mean score on the test was 75 with a standard deviation of 10; the scores were approximately normally distributed. The mean score for the first 20 tests was 78. Did the students turning in their tests first score significantly different from the mean?
a professor has learned that six students in her class of 19 will cheat on the...
a professor has learned that six students in her class of 19 will cheat on the exam. She decides to focus her attention on eight randomly chosen students during the exam. A. what is the probability that she finds at least one the students cheating? B. what is the probability that she finds at least one of the students cheating if she focuses on nine randomly chosen students?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT