Java:Netbeans-Use the LinkedList class to simulate a queue, and use the add method to simulate the enqueue, and the remove method to simulate the dequeue method for a Queue. Remember to use FIFO.
Need help with 0. Add New Microchips pushMicroChip(), popMicroChip() and . To simulate this, you will create a menu option 0, which will generate 100 microchip long objects, and place them into a stack of microchips. Those microchip objects will be generated by using the System.nanotime() method.
Create 3 queues, one called Animal Shelter, another called Cats, and another called Dogs. Use the following menu-driven application:
0. Add New Microchips
1. Donate a Cat
2. Donate a Dog
3. Adopt a Cat
4. Adopt a Dog
5. Adopt Oldest Pet
6. Exit
Create a Pet class that will be instantiated with the values in the following attributes: String name, int dateOfBirth //yyyymmdd format, String species, Long microchipNumber.
Note: The Dog queue and Cat queue are "regular" queues, and the Animal Shelter queue is a Priority Queue, where the priority is based upon the dateOfBirth of the pet. The older the pet, the higher its priority. See the attached .zip file for examples of a PriorityQueue.
Each week, on Monday before the Animal Shelter opens, an administrator of the Animal Shelter adds more microchips to the stack of microchips, so that there will be enough microchips to put in any animal that is donated into the shelter that week. To simulate this, you will create a menu option 0, which will generate 100 microchip long objects, and place them into a stack of microchips. Those microchip objects will be generated by using the System.nanotime() method.
Each time a person donates a cat or dog, ask for the name of the pet, the species, and the date of birth, and pop the microchip stack to get a chip to put in the pet. Create a new Pet object. Then, add the Pet object to both its specific species queue, and the general Animal Shelter queue. If the stack of microchips is empty, give an error message to the user, stating the pet cannot be donated because there are no microchips left. Ask the user to press option 0 to get more microchips.
When a person selects to adopt a specific species pet, remove the first pet object 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: You may need 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.)
When a person comes in to adopt the oldest pet in the shelter, then process the Animal Shelter as a PriorityQueue, where the pet's date of birth is the attribute used to retrieve the oldest pet (hint: Pet class should implement Comparable, and should define a compareTo method that would sort in ascending date-of-birth order. ) So, when you dequeue the Animal Shelter as a Priority Queue, you will automatically get the Pet with the earliest/oldest date of birth first . Then, after removing the oldest pet object from the animal shelter, then dequeue it from the appropriate species queue (will have to do a sequential search in the the appropriate queue, according to the Pet object's species attribute. See hint above for keeping the exact order in the queue).
Create 4 global variables that represent the 3 queues for dogs, cats, and animal shelter (Priority Queue based on date-of-birth of pet), and the stack for the microchips. Create methods that can be called multiple times to accomplish all of the menu options. The names of the methods should be:
enqueueCat(), enqueueDog(), dequeueCat(), dequeueDog(), enqueueAnimal(), dequeueAnimal(), pushMicroChip(), popMicroChip()
package pet;
import java.util.LinkedList;
import java.util.Scanner;
class Pet {
String name, species;
int dob;
public Pet(String name, String species, int dob) {
super();
this.name = name;
this.species = species;
this.dob = dob;
}
public Pet() {
super();
}
@Override
public String toString() {
return "Pet [name=" + name + ", species=" + species + ", dob=" + dob + "]";
}
}
public class AnimalHouse {
public LinkedList cat;
public LinkedList dog;
public LinkedList animal;
//for input output purpose
Scanner sc = new Scanner(System.in);
Scanner scName = new Scanner(System.in);
public AnimalHouse() {
cat = new LinkedList<>();
dog = new LinkedList<>();
animal = new LinkedList<>();
}
public void enqueueCats() {
Pet pet = new Pet();
System.out.println("Enter name of pet : ");
pet.name = scName.nextLine();
System.out.println("Enter DOB (yyyymmdd) : ");
pet.dob = sc.nextInt();
pet.species = "Cat";
cat.addLast(pet);
animal.addLast(pet);
}
public Pet dequeueCats() {
Pet pet = cat.removeFirst();
animal.remove(pet);
System.out.println("Adopted " + pet);
return pet;
}
public void enqueueDogs() {
Pet pet = new Pet();
System.out.println("Enter name of pet : ");
pet.name = scName.nextLine();
System.out.println("Enter DOB (yyyymmdd) : ");
pet.dob = sc.nextInt();
pet.species = "Dog";
dog.addLast(pet);
animal.addLast(pet);
}
public Pet dequeueDogs() {
Pet pet = dog.removeFirst();
animal.remove(pet);
System.out.println("Adopted " + pet);
return pet;
}
public void enqueueAnimals(Pet animalIn) {
animal.addLast(animalIn);
}
public Pet dequeueAnimals() {
//store the oldest pet object
Pet oldestPet = new Pet();
//assume oldest age is
int oldest = 99999999;
//loop through all animal
for (Pet checkThisPet : animal) {
//compare and get old age
if (checkThisPet.dob < oldest) {
oldest = checkThisPet.dob;
oldestPet = checkThisPet;
}
}
//print old age pet
System.out.println("Oldest pet is " + oldestPet);
//based on species remove it from the queue
if (oldestPet.species.equalsIgnoreCase("cat")) {
animal.remove(oldestPet);
cat.remove(oldestPet);
} else {
animal.remove(oldestPet);
dog.remove(oldestPet);
}
return null;
}
public static void main(String[] args) {
AnimalHouse animals = new AnimalHouse();
int choice;
boolean continueMe = true;
while (continueMe) {
System.out.println("1. Donate a Cat");
System.out.println("2. Donate a Dog");
System.out.println("3. Adopt a Cat");
System.out.println("4. Adopt a Dog");
System.out.println("5. Adopt Oldest Pet");
System.out.println("6. Exit");
choice = animals.sc.nextInt();
switch (choice) {
case 1:
animals.enqueueCats();
break;
case 2:
animals.enqueueDogs();
break;
case 3:
animals.dequeueCats();
break;
case 4:
animals.dequeueDogs();
break;
case 5:
animals.dequeueAnimals();
break;
default:
continueMe = false;
break;
}
System.out.println("Cats are : " + animals.cat);
System.out.println("Dogs are : " + animals.dog);
System.out.println("Animals are : " + animals.animal);
}
}
}
enqueueCat(), enqueueDog(), dequeueCat(), dequeueDog(),
enqueueAnimal(), dequeueAnimal(), pushMicroChip(),
popMicroChip() ;
package pet;
import java.util.LinkedList;
import java.util.Scanner;
class Pet {
String name, species;
int dob;
public Pet(String name, String species, int dob) {
super();
this.name = name;
this.species = species;
this.dob = dob;
}
public Pet() {
super();
}
@Override
public String toString() {
return "Pet [name=" + name + ", species=" + species + ", dob=" + dob + "]";
}
}
public class AnimalHouse {
public LinkedList cat;
public LinkedList dog;
public LinkedList animal;
//for input output purpose
Scanner sc = new Scanner(System.in);
Scanner scName = new Scanner(System.in);
public AnimalHouse() {
cat = new LinkedList<>();
dog = new LinkedList<>();
animal = new LinkedList<>();
}
public void enqueueCats() {
Pet pet = new Pet();
System.out.println("Enter name of pet : ");
pet.name = scName.nextLine();
System.out.println("Enter DOB (yyyymmdd) : ");
pet.dob = sc.nextInt();
pet.species = "Cat";
cat.addLast(pet);
animal.addLast(pet);
}
public Pet dequeueCats() {
Pet pet = cat.removeFirst();
animal.remove(pet);
System.out.println("Adopted " + pet);
return pet;
}
public void enqueueDogs() {
Pet pet = new Pet();
System.out.println("Enter name of pet : ");
pet.name = scName.nextLine();
System.out.println("Enter DOB (yyyymmdd) : ");
pet.dob = sc.nextInt();
pet.species = "Dog";
dog.addLast(pet);
animal.addLast(pet);
}
public Pet dequeueDogs() {
Pet pet = dog.removeFirst();
animal.remove(pet);
System.out.println("Adopted " + pet);
return pet;
}
public void enqueueAnimals(Pet animalIn) {
animal.addLast(animalIn);
}
public Pet dequeueAnimals() {
//store the oldest pet object
Pet oldestPet = new Pet();
//assume oldest age is
int oldest = 99999999;
//loop through all animal
for (Pet checkThisPet : animal) {
//compare and get old age
if (checkThisPet.dob < oldest) {
oldest = checkThisPet.dob;
oldestPet = checkThisPet;
}
}
//print old age pet
System.out.println("Oldest pet is " + oldestPet);
//based on species remove it from the queue
if (oldestPet.species.equalsIgnoreCase("cat")) {
animal.remove(oldestPet);
cat.remove(oldestPet);
} else {
animal.remove(oldestPet);
dog.remove(oldestPet);
}
return null;
}
public static void main(String[] args) {
AnimalHouse animals = new AnimalHouse();
int choice;
boolean continueMe = true;
while (continueMe) {
System.out.println("1. Donate a Cat");
System.out.println("2. Donate a Dog");
System.out.println("3. Adopt a Cat");
System.out.println("4. Adopt a Dog");
System.out.println("5. Adopt Oldest Pet");
System.out.println("6. Exit");
choice = animals.sc.nextInt();
switch (choice) {
case 1:
animals.enqueueCats();
break;
case 2:
animals.enqueueDogs();
break;
case 3:
animals.dequeueCats();
break;
case 4:
animals.dequeueDogs();
break;
case 5:
animals.dequeueAnimals();
break;
default:
continueMe = false;
break;
}
System.out.println("Cats are : " + animals.cat);
System.out.println("Dogs are : " + animals.dog);
System.out.println("Animals are : " + animals.animal);
}
}
}
Java:Netbeans-Use the LinkedList class to simulate a queue, and use the add method to simulate the...
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); } ...
Create a Java application that allows user to build a Priority Queue of Circle Elements (i.e., priority based on the radius of the circle). The application must be menu controlled similar to Project 4 and provide the following. Allow insertion of a "Circle" object/structure in the Priority Queue data structures. Allow display of all elements from Priority Queue data structure by Invoking a method/function "DisplayPriorityQueue" (uses "DeQueue" method). Allow for deletion of the Queue This is what I have so...
This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...
Priority Queue Demo import java.util.*; public class PriorityQueueDemo { public static void main(String[] args) { //TODO 1: Create a priority queue of Strings and assign it to variable queue1 //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1 System.out.println("Priority queue using Comparable:"); //TODO 3: remove from queue1 all the strings one by one // with the "smaller" strings (higher priority) ahead of "bigger"...
To do: Now add code to Pet and PetPlay to complete the comments in the code. import java.util.ArrayList; public class PetPlay { //----------------------------------------------------------------- // Stores and modifies a list of pets //----------------------------------------------------------------- public static void main (String[] args) { // always make your array lists generic ArrayList pets = new ArrayList(); pets.add(new Pet("dog", "Bella")); pets.add(new Pet("cat","Blackie")); pets.add(new Pet("bird", "Babs")); pets.add(new Pet("dog", "Lassie")); pets.add(new Pet("horse", "Sam")); pets.add(new Pet("dog", "Blackie")); pets.add(new Pet("turtle", "Joe")); pets.add(new Pet("bird","Annie")); pets.add(new Pet("bird", "Alex"));...
PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1. What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** * Program: PRG/421 Week 1 Analyze Assignment * Purpose: Analyze the coding for...
4.14 LAB: Ticketing service (Queue)Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to thepeopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.Ex. If the input isZadie Smith Tom Sawyer You Louisa Alcottthe output isWelcome to the ticketing service... You are number 3 in the queue. Zadie Smith has purchased a ticket. You are now number 2 Tom Sawyer has purchased a ticket. You are now number 1 You can now purchase your ticket!Code that I have been...
public class Pet { //Declaring instance variables private String name; private String species; private String parent; private String birthday; //Zero argumented constructor public Pet() { } //Parameterized constructor public Pet(String name, String species, String parent, String birthday) { this.name = name; this.species = species; this.parent = parent; this.birthday = birthday; } // getters and setters public String getName() { return name; ...
1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...
Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java. Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods. In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....