A “Dog and Cat Farm” works on a “first in, first out” basis when it comes to pro
ID: 3739245 • Letter: A
Question
A “Dog and Cat Farm” works on a “first in, first out” basis when it comes to processing adoption requests from clients and has this strict rule - a client must adopt either the “oldest” (based on arrival time) of all animals in the farm, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structure (with an interface file) to maintain this system and implement operations such as enqueue, dequeueAny, dequeDog, and dequeCat.
(Using the following code to complete)
class Animal
{
private String name;
protected boolean isCat;
public Animal(String name)
{
this.name = name;
}
}
class Dog extends Animal
{
public Dog(String name)
{
super(name);
isCat = false;
}
}
class Cat extends Animal
{
public Cat(String name)
{
super(name);
isCat = true;
}
}
interface AnimalQueueInterface
{
Animal enqueue(Animal animal);
Animal dequeueDog();
Animal dequeuCat();
Animal dequeAny(); // Returns oldest animal (dog or cat) in the queue
}
// This is the class that I will check
public class AnimalQueue implements AnimalQueueInterface
{
public static void main(String []args)
{
Animal d1 = new Dog("MyBullDog");
Animal c1 = new Cat("SmallCat");
Animal d2 = new Dog("MyGermanShepherd");
AnimalQueueInterface q = new AnimalQueue();
q.enqueue(d1);
q.enqueue(c1);
q.enqueue(d2);
Animal adoptedAnimal = q.dequeAny(); // should return d1
}
@Override
public Animal enqueue(Animal animal) {
return null;
}
@Override
public Animal dequeueDog() {
return null;
}
@Override
public Animal dequeuCat() {
return null;
}
@Override
public Animal dequeAny() {
return null;
}
}
Explanation / Answer
Please find the classes below. Proper explanation is provided in the class/method's java doc. Program execution is also provided at the end.
Java Classes :
============
Execution Output :
=============
Removing ANY : -Animal{name='MyBullDog', isCat=false}
Removing a CAT : -Animal{name='SmallCat', isCat=true}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.