Question

Please make the following code modular. Therefore contained in a package with several classes and not...

Please make the following code modular. Therefore contained in a package with several classes and not just one.

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);
           }
       }
   }
  
}

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

JAVA PROGRAM

=========================

TheQueue class

=========================

package test.queue;

import java.util.LinkedList;

//Parent class : TheQueue
public class TheQueue {
  
   protected LinkedList<String> theList; //protected : will be accessible to child classes
  
   //constructor
   public TheQueue(LinkedList<String> linkList){
       this.theList = linkList;
   }
  
  
   //remove From Queue logic
   //The String with name will be removed from linkedlist
   private void removeFromQueue(String name){
   LinkedList<String> tmp = new LinkedList<String> ();
while (!theList.isEmpty()){
if(!theList.getFirst().equals(name)){
tmp.add(theList.getFirst());
}
theList.removeFirst();
}
while (!tmp.isEmpty()){
   theList.add(tmp.removeLast());
}
   }
  
   //ennque: name will be added
   public void enqueue(String name){
       theList.add(name);
   }
  
   //dequeue: removeFromQueue () will be called
   public void dequeue(String name){
       removeFromQueue(name);
   }
  
}

====================================

CatQueue class

====================================

package test.queue;

import java.util.LinkedList;

//Child class
public class CatQueue extends TheQueue{
   private static LinkedList<String> cats = new LinkedList<String>();//static linked list created
   public CatQueue() {
       super(cats);//passed to superclass constructor
   }

}

=====================================

DogQueue class

=====================================

package test.queue;

import java.util.LinkedList;

//Child class
public class DogQueue extends TheQueue{
   private static LinkedList<String> dogs = new LinkedList<String>();//static linked list created
   public DogQueue() {
       super(dogs);//passed to superclass constructor
   }

}

==========================================

AnimalQueue class

==========================================

package test.queue;

import java.util.LinkedList;

//Child class
public class AnimalQueue extends TheQueue{
   private static LinkedList<String> animals = new LinkedList<String>();//static linked list created
   public AnimalQueue() {
       super(animals);//passed to superclass constructor
   }

}

===============================

Q class (the main program)

===============================

package test.queue;

import java.util.*;

//The main class: Q
public class Q {

//create 3 static queues
private static CatQueue cq = new CatQueue();
private static DogQueue dq = new DogQueue();
private static AnimalQueue aq = new AnimalQueue();

//this method will take the name from user
private static String getName(Scanner sc){
   System.out.println("Enter name");
String name = sc.next();
return name;
}

//this method will enqueue the cat in cat queue as well as animal queue
private static void enqueueCats(Scanner sc){
   String name = getName(sc);
cq.enqueue(name);
aq.enqueue(name);
}

//this method will enqueue the dog in dog queue as well as animal queue
public static void enqueueDogs(Scanner sc){
   String name = getName(sc);
dq.enqueue(name);
aq.enqueue(name);
}
//this method will dequeue the cat from cat queue as well as animal queue
public static void dequeueCats(Scanner sc){
   String name = getName(sc);
cq.dequeue(name);
aq.dequeue(name);
}

//this method will dequeue the dog from dog queue as well as animal queue
public static void dequeueDogs(Scanner sc){
   String name = getName(sc);
dq.dequeue(name);
aq.dequeue(name);
}

//this method will dequeue the animal from animal queue as well as cat/dog queue
public static void dequeueAnimals(Scanner sc){
   String name = getName(sc);
aq.dequeue(name);
cq.dequeue(name);
dq.dequeue(name);
}

//display the animals, cats, dogs
public static void display(){
System.out.println("animals:");
for (String s : aq.theList)
System.out.print(s+" ");

System.out.println();
System.out.println("cats:");
  
for (String s : cq.theList)
System.out.print(s+" ");

System.out.println();
System.out.println("dogs:");
  
for (String s : dq.theList)
System.out.print(s+" ");

System.out.println();
}

//display choices
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]);
}
}

//main method
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(sc);
display();
break;
case 2 :
enqueueDogs(sc);
display();
break;
case 3 :
dequeueCats(sc);
display();
break;
case 4 :
dequeueDogs(sc);
display();
break;
case 5:
dequeueAnimals(sc);
display();
break;
case 6 :
System.exit(0);
}
}
}
  
}

==========================================

OUTPUT

==========================================

1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
1
Enter name
whitecat
animals:
whitecat
cats:
whitecat
dogs:

1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
1
Enter name
bluecat
animals:
whitecat bluecat
cats:
whitecat bluecat
dogs:

1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
2
Enter name
husky
animals:
whitecat bluecat husky
cats:
whitecat bluecat
dogs:
husky
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
2
Enter name
pitbull
animals:
whitecat bluecat husky pitbull
cats:
whitecat bluecat
dogs:
husky pitbull
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
1
Enter name
redcat
animals:
whitecat bluecat husky pitbull redcat
cats:
whitecat bluecat redcat
dogs:
husky pitbull
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
2
Enter name
dalmetian
animals:
whitecat bluecat husky pitbull redcat dalmetian
cats:
whitecat bluecat redcat
dogs:
husky pitbull dalmetian
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
3
Enter name
redcat
animals:
dalmetian pitbull husky bluecat whitecat
cats:
bluecat whitecat
dogs:
husky pitbull dalmetian
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
4
Enter name
pitbull
animals:
whitecat bluecat husky dalmetian
cats:
bluecat whitecat
dogs:
dalmetian husky
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
5
Enter name
husky
animals:
dalmetian bluecat whitecat
cats:
whitecat bluecat
dogs:
dalmetian
1.Donate a Cat
2.Donate a Dog
3.Adopt a Cat
4.Adopt a Dog
5.Adopt Oldest Pet
6.Exit
6

Add a comment
Know the answer?
Add Answer to:
Please make the following code modular. Therefore contained in a package with several classes and not...
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
  • This is not my entire line of code but the errors I'm getting are from this...

    This is not my entire line of code but the errors I'm getting are from this section. Can you please correct it and tell me where I went wrong? Thank you! package comJava; Vimport java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.Scanner; //Lis public Driver { static List<RescueAnimal> list = new ArrayList<>(); public static void main(String[] args) { // Class variables // Create New Dog Dog d = new Dog(); // Create New Monkey Monkey m = new Monkey(); // Method...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

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