Question

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 a single queue. Each student has a unique id within their respective schools, but 2 students from different schools may have the same id.

At the registration booth, there is a long queue of students from different schools. As the queue has started getting very long very quickly, students are starting to employ devious tactics to complete their registration sooner than other schools. When a new student arrives at the queue,  they do 2 things

  1. They start searching for a schoolmate from the end of the queue. As soon as they find a schoolmate in the queue, the student will stand behind the schoolmate in the queue.
  2. If no schoolmate is found, the student will stand  at the very end of the queue.

When the student at the beginning of the queue is registered, they are immediately removed form the queue.

So there are 2 types of operations that can take place on the queue.

  • E x y : A new student of school x with id y joins the queue according to the strategy
  • D : The student at the front of the queue is registered and is removed from the queue.

Given a set operations defined above, your task is to output the order in which students registered for the competition. See below for the input and output format

Input Format

12
E 1 1
E 2 1
E 3 2
E 1 5
D
E 2 2
E 4 1
D
D
D
D
D

Output Format

1 1

1 5

2 1

2 2

3 2

4 1

Explanation

The 12 in the first line means we will do 12 operations, which are then listed below.

Let's go through this one operation at a time. Initially the queue is empty

Denoting Front of Queue as F, rear of Queue as R

F/R

  1. E 1 1

    F/R

    (1,1)

    Initially the queue is empty, so add the first student with school 1 and id 1 to queue

  2. E 2 1

    F R

    (1,1) (2,1)

    Next student with school 2 and id 1 arrives. As there are no schoolmates that go to school 2 in the queue, student (2,1) joins the back of the queue

  3. E 3 2

    F R

    (1,1) (2,1) (3,2)

    Next student with school 3 and id 2 arrives. As there are no schoolmates that go to school 2 in the queue currently, student (3,2) joins the back of the queue

  4. E 1 5

    F R

    (1,1) (1,5) (2,1) (3,2)
    Now, when student with school 1 and id 5 arrives they, start form the rear and start searching for another schoolmate, they find one (1,1) and joins the queue behind them
  5. D

    F R

    (1,5) (2,1) (3,2)

    D means a student finished registering, so output their details and remove them from the queue

    Output : 1 1
  6. E 2 2

    F R

    (1,5) (2,1) (2,2) (3,2)

    Next student with school 2 and id 2 arrives, starts scanning from the rear, and finds student (2,1) and joins behind them.

  7. E 4 1

    F R

    (1,5) (2,1) (2,2) (3,2) (4,1)

    Next student with school 4 and id 1 arrives, starts searching for a schoolmate, finds none, thus joins the back of the queue.

  8. D Output : 1 5
  9. D Output : 2 1
  10. D Output : 2 2
  11. D Output : 3 2
  12. D Output : 4 1

Operations 8-12 will simply output the school and student id at the front of the queue and remove them .

So the final output will look like

1 1

1 5

2 1

2 2

3 2

4 1

You may assume that a maximum of 4 schools participated.

As there may be multiple approaches to this problem, please make sure output matches mine exactly. That is each line in your output should be exactly 2 numbers separated by a single whitespace, denoting the school and id of the student being registered,

To ensure this please use the following template code and write all your code inside this class

Squeue.java

Sample runs

Run 1

Input

12
E 1 1
E 2 1
E 3 2
E 1 5
D
E 2 2
E 4 1
D
D
D
D
D

Output

1 1

1 5

2 1

2 2

3 2

4 1

Run 2

Input

5
E 1 1
E 2 1
E 1 2
D
D

Output

1 1
1 2

PreviousNext

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

Please find the JAVA code below

_____________________________

package com;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

public class Registration {

public static void main(String[] args) {

System.out.println("Enter the input details");

//create a list to maintain the student data

LinkedList<SchoolAndId> list1 = new LinkedList<SchoolAndId>();

String var[] = null;

//variable to take the input

String trace = null;

Scanner input =new Scanner(System.in);

//initializing the index to -1

int index =-1;

//Read the number of operations

int operations = input.nextInt();

//iterate the while loop until the end of operations

while(operations>=0) {

//input the student ID and school ID with spaces in between

trace = input.nextLine();

var = trace.split(" ");

//condition to check whether to insert or to delete

if(var[0].equals("E")) {

//create a school object and place it in the list defined earlier

SchoolAndId obj = new SchoolAndId(var[1],var[2]);

//check whether the school ID is already present or not in the given list

index= checkforschool(list1,var[1]);

//if the school ID is already present in the list place the present object in the list after that index

if(index!= -1)

list1.add(index+1,obj);

else

list1.add(obj);

}else if(var[0].equals("D")) {

System.out.println(list1.get(0).getSchool() +" " + list1.get(0).getId());

list1.remove(0);

}

operations = operations-1;

}

}

public static int checkforschool(LinkedList<SchoolAndId> list1, String schoolId) {

//we are using the list to store other than queue so we should iterate the list from backward to check for the school ID in the list

int index = list1.size()-1;

while(index>=0) {

// return index if found

if(list1.get(index).getSchool().equals(schoolId))

return index;

index= index - 1;

}

//return -1 if not found

return -1;

}

}

package com;

public class SchoolAndId {

private String school;

private String id;

public String getSchool() {

return school;

}

public SchoolAndId(String school, String id) {

super();

this.school = school;

this.id = id;

}

public void setSchool(String school) {

this.school = school;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

}

Output

______

Console X 3 Tasks terminated> Registration [Java Application]/Library/Java/JavaVirtualMachines/jdk1. Enter the input details

Add a comment
Know the answer?
Add Answer to:
Java Programming (Queues and LinkedLists) The annual First robotics competition is hosted by SVSU...
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
  • 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...

  • Calculate the probability of the following events A the first number is 2 or 3 or...

    Calculate the probability of the following events A the first number is 2 or 3 or 4 E the second digit is 3 or less F the second digit is 4 or greater PIE or F) P(E and F) P(A) P( A and E) P( A and F) P( A and E)+P( Aand F) 2 Dice Sample Space 1,1 2,1 3,1 4,1 5,1 1,6 1,2 2,2 3,2 4,2 5,2 6,2 1,3 2,3 3,3 4,3 5,3 6,3 1,5 2,4 3,4 4,4...

  • Write a java code : Student ID at University is composed of year of admission and...

    Write a java code : Student ID at University is composed of year of admission and students number. we aim to implement a structure that improves operations of inserting and searching for a student. To enhance these operations, we will build a tree of linked lists (TreeOfLists) to combine advantages of both structures. Each node in this tree contains a linked list of all students who were admitted in that year. Each node in the linked list represents a student(id,...

  • Java Gradebook

    Requirements:1.     The number of students to enter will be dynamic, minimum of 5.2.     Ask the user for a student's:a.     first nameb.     last namec.      student IDd.     4 exam grades (each out of 100 points)Calculate the student's:e.     final letter grade.3.     For each student, if the user enters invalid data he will be allowed to enter it again.4.     Letter grade calculation will use the standard grading scale.a.     90% - 100% = Ab.     80% - 89% = Bc.      70% - 79% = Cd.     60% - 69% = De.     Below 60% = F5.     The output will consist of:a.     A list of...

  • 22. You are looking at the CGA exam and want to assess whether or not there...

    22. You are looking at the CGA exam and want to assess whether or not there are different success rates with students from different schools. You wanted to take a random sample of 5 students from each of 3 schools; however one student was sick and doesnt show up. School 1 45 48 54 05 School 2 School 3 51 41 64 46 76 03 78 63 84 72 70.6 55 Average Exam Scores from the 3 schools (a) Calculate...

  • Language is C programming Student ID should be 8 numbers long. create a program to read...

    Language is C programming Student ID should be 8 numbers long. create a program to read your student ID number and store it in an array. Input can not be hard wired, I have to be able to input my number Then change the first number to a letter. If the same number repeats in other parts of your student ID, you must change that number to a letter as well. print original array and modified array as your output....

  • Suppose we want to estimate the average weight of children in a particular school district. The...

    Suppose we want to estimate the average weight of children in a particular school district. The district contains 15 elementary schools, 10 middle schools, and 2 high schools. For each of the following approaches, select the best description of the sampling design, and identify whether the sampling scheme ensures that the probability of being included is necessarily the same for every student. Take a simple random sample of 5 schools in the district. For each school in the sample, list...

  • (JAVA PROGRAMMING NEEDED)        An invitation card is sent asking stude...

    (JAVA PROGRAMMING NEEDED)        An invitation card is sent asking student to input their name to the forum. The interface sketching is incomplete. Provide a suitable widgets on the interface. The designer/developer may add more information on the interface that could assist the organizer such as: id number, group number and other (only relevant information). Then, the program, can produce a report that will list out students who can attend and cannot attend to the program and also total numbers for...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • Write a Java program, including comments, to compute statistics for how students did on an exam....

    Write a Java program, including comments, to compute statistics for how students did on an exam. The program should compute various things about a student and print it all out. Then it repeats the process for each new student until the entire set of data has been completed. (a) The program reads in and prints the ID number of a student [see step (g) below] and then the number of right answers and the number of wrong answers. (The total...

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
Active Questions
ADVERTISEMENT