Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you please help me with this code Use the LinkedList class to simulate a q

ID: 3815558 • Letter: C

Question

Could you please help me with this code

Use the LinkedList class to simulate a queue, and use the enqueue and the dequeue methods to add and remove from the queue, using FIFO.

Create 3 queues, one called Animal Shelter, another called Cats, and another called Dogs. Use the following menu-driven application:

1. Donate a Cat
2. Donate a Dog
3. Adopt a Cat
4. Adopt a Dog
5. Adopt Oldest Pet
6. Exit

Each time a person donates a cat or dog, ask for the name of the pet, and then add the pet to both its species queue and the general Animal Shelter queue. When a person selects to adopt a pet, remove the pet from the appropriate species queue, and also from the Animal Shelter queue. Take care to find the pet in the Animal queue, and remove it, but put back all the others that follow it, in the same order you found them (hint: may need a stack as a temp storage).

If a person comes in to adopt the oldest pet in the shelter, then process the Animal Shelter first, dequeue the oldest, and then dequeue it from the appropriate species queue (may have to search both, unless you make the node a Pet with name and species attributes).

Create 3 global variables that represent the 3 queues for dogs, cats, and animal shelter. In the driver class, create the following methods that will be called from main, according to the menu option selected:

1. enqueueCats()
2. enqueueDogs()
3. dequeueCats()
4. dequeueDogs()
5. enqueueAnimals()
6. dequeueAnimals()

You create a Pet domain class that contains the name and species of the animal. Then, you can store the pet object in the corresponding queue(s). Ensure you select the correct pet when you are removing it from the animal shelter and the corresponding dog or cat queue. You may have to peek at the front of the dog or cat queue, to ensure the pet you just dequeued from the animal shelter queue, is found at the front of the line in the corresponding dog or cat queue. Once verified, go ahead and dequeue the pet. Another technique may be to create a new queue, removing the dog or cat that needs to be dequeued, and keeping all the rest. Once the new queue is created, point the old queue to the new queue.

Explanation / Answer

import java.util.*;
public class Q {
   public static LinkedList<String> dogs = new LinkedList<String> ();
   public static LinkedList<String> cats = new LinkedList<String> ();
   public static LinkedList<String> animals = new LinkedList<String> ();
   public static void enqueueCats(){
       System.out.println("Enter name");
       Scanner sc=new Scanner(System.in);
       String name = sc.next();
       cats.addLast(name);
       animals.addLast(name);
   }
   public static void enqueueDogs(){
       System.out.println("Enter name");
       Scanner sc=new Scanner(System.in);
       String name = sc.next();
       dogs.addLast(name);
       animals.addLast(name);
   }
   public static void removeFromQueue(char q,String name){
       LinkedList<String> L = new LinkedList<String> ();
       switch (q) {
           case 'c':
               L = cats;
               break;
           case 'd':
               L = dogs;
               break;
           case 'a':
               L = animals;
       }
      
       LinkedList<String> tmp = new LinkedList<String> ();
       while (!L.isEmpty()){
           if(!L.getFirst().equals(name)){
               tmp.add(L.getFirst());
           }
           L.removeFirst();
       }
       while (!tmp.isEmpty()){
           L.add(tmp.removeLast());
       }
   }
   public static void dequeueCats(){
       System.out.println("Enter name");
       Scanner sc=new Scanner(System.in);
       String name = sc.next();
       removeFromQueue('c',name);
       removeFromQueue('a',name);
   }
   public static void dequeueDogs(){
       System.out.println("Enter name");
       Scanner sc=new Scanner(System.in);
       String name = sc.next();
       removeFromQueue('d',name);
       removeFromQueue('a',name);
   }

   public static void dequeueAnimals(){
       System.out.println("Enter name");
       Scanner sc=new Scanner(System.in);
       String name = sc.next();
       removeFromQueue('a',name);
       removeFromQueue('d',name);
       removeFromQueue('c',name);
   }
   public static void display(){
       System.out.println("animals:");
       for (String s : animals)
            System.out.print(s);
        System.out.println();
        System.out.println("cats:");
       for (String s : cats)
            System.out.print(s);
        System.out.println();
        System.out.println("dogs:");
       for (String s : dogs)
            System.out.print(s);
        System.out.println();
   }

   public static void display_choices(){
       String [] choices = {"Donate a Cat","Donate a Dog","Adopt a Cat","Adopt a Dog","Adopt Oldest Pet","Exit"};
       for (int i=0;i<choices.length ;i++ ) {
           System.out.println((i+1)+"."+choices[i]);
       }
   }
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       while(true){
           display_choices();
           int choice=sc.nextInt();
           switch (choice) {
               case 1 :
                   enqueueCats();
                   display();
                   break;
               case 2 :
                   enqueueDogs();
                   display();
                   break;
               case 3 :
                   dequeueCats();
                   display();
                   break;
               case 4 :
                   dequeueDogs();
                   display();
                   break;
               case 5:
                   dequeueAnimals();
                   display();
               case 6 :
                   System.exit(0);
           }
       }
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote