-
-
Save JamesJi9277/8b7c266b65ef197c27bd to your computer and use it in GitHub Desktop.
3.7.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cats and dogs, | |
public class Solution{ | |
private int order; | |
protected String name; | |
public Animal(String n){ | |
n = name; | |
} | |
public void setOrder(int ord){ | |
ord = order; | |
} | |
public int getOrder(){ | |
return order; | |
} | |
public boolean isOlderThan(Animal a){ | |
return this.order < a.getOrder; | |
} | |
public class AnimalQueue{ | |
LinkedList<Dog> dogs = new LinkedList<Dog>(); | |
LinkedList<Cat> cats = new LinkedList<Cat>(); | |
public void enqueue(Animal a){ | |
a.setOrder(order); | |
order++; | |
if( a instanceof Dog) | |
dogs.addList((Dog) a); | |
else if(a instanceof Cat) | |
cats.addList((Cat) a); | |
} | |
public Animal dequeueAny(){ | |
if(dogs.size()==0) | |
return dequeueCats(); | |
else if(cats.size()==0) | |
return dequeueDogs(); | |
Dog dog = dogs.peek(); | |
Cat cat = cats.peek(); | |
if(dog.isOlderThan(cat)) | |
return dequeueDogs(); | |
else return dequeueCats(); | |
} | |
public Dog dequeueDogs(){ | |
return dogs.poll(); | |
} | |
public dequeueCats(){ | |
return cats.poll(); | |
} | |
public class Dog extends Animal{ | |
public Dog(String n) {super(n);} | |
} | |
public class Cat extends Animal{ | |
public Cat(String n){super(n);} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment