问题:宠物、狗、猫的类如下
class Pet { private String type; public Pet(String type) { this.type = type; } public String getType() { return this.type; } public String toString(){ return this.getType(); } } class Dog extends Pet { public Dog(String type) { super(type); } @Override public String toString() { return super.toString(); } } class Cat extends Pet { public Cat(String type) { super(type); } @Override public String toString() { return super.toString(); } }
实现一个猫狗队列,要求如下:
1.可以调用add()方法,将cat类和dog类添加到队列中;
2.可以调用pollAll()方法,将队列中的所有实例按入队列的顺序依次弹出;
3.可以调用pollCat()方法,将队列中的所有Cat对象实例按入队列的顺序依次弹出;
4.可以调用pollDog()方法,将队列中的所有Dog对象实例按入队列的顺序依次弹出;
5.可以调用isEmpty()方法,判断对队列是否为空;
6.可以调用isCatEmpty()方法,判断队列是否有Cat对象实例;
7.可以调用isDogEmpty()方法,判断队列是否有Dog对象实例;
思路:队列是先进先出,由于要存放两种不同的对象,因此可以用两个队列分别存放Dog对象和Cat对象。由于要维持进入队列的顺序,在不改变原有类的情况下,我们可以构造一个类EnterPetQueue,在类中引入计数变量,来记录对象的入队顺序。
EnterPetQueue构造类
class EnterPetQueue{ private Pet pet; private Integer count; public EnterPetQueue(Pet pet,Integer count){ this.pet=pet; this.count=count; } public Pet getPet(){ return this.pet; } public Integer getCount(){ return this.count; } }
CatAndDogQueue类
public class CatAndDogQueue { private LinkedList<EnterPetQueue> catQueue; private LinkedList<EnterPetQueue> dogQueue; private Integer count=0; public CatAndDogQueue() { catQueue = new LinkedList<>(); dogQueue = new LinkedList<>(); } public void add(Pet pet){ if(pet.getType().contains("cat")){ catQueue.addLast(new EnterPetQueue(pet,count++)); }else if(pet.getType().contains("dog")){ dogQueue.addLast(new EnterPetQueue(pet,count++)); }else{ throw new RuntimeException("只能添加Cat和Dog实例,不能添加其他实例"); } } public boolean isDogEmpty(){ return dogQueue.isEmpty(); } public boolean isCatEmpty(){ return catQueue.isEmpty(); } public boolean isEmpyt(){ return isCatEmpty()&&isDogEmpty(); } public Pet poolAll(){ if(isEmpyt()){ return null; }else if(isDogEmpty()){ return catQueue.pollFirst().getPet(); }else if(isCatEmpty()){ return dogQueue.pollFirst().getPet(); }else{ if(catQueue.getFirst().getCount()<dogQueue.getFirst().getCount()){ return catQueue.pollFirst().getPet(); }else{ return dogQueue.pollFirst().getPet(); } } } public Dog pollDog(){ if(isDogEmpty()){ return null; }else{ return (Dog)dogQueue.pollFirst().getPet(); } } public Cat pollCat(){ if(isCatEmpty()){ return null; }else{ return (Cat)catQueue.pollFirst().getPet(); } } }
测试类Test
public class Test { public static void main(String[] args) { Cat cat1=new Cat("cat1"); Cat cat2=new Cat("cat2"); Cat cat3=new Cat("cat3"); Dog dog1=new Dog("dog1"); Dog dog2=new Dog("dog2"); Dog dog3=new Dog("dog3"); CatAndDogQueue catAndDogQueue=new CatAndDogQueue(); catAndDogQueue.add(cat1); catAndDogQueue.add(dog3); catAndDogQueue.add(cat3); catAndDogQueue.add(dog1); catAndDogQueue.add(cat2); catAndDogQueue.add(dog2); while (!catAndDogQueue.isEmpyt()){ System.out.println(catAndDogQueue.poolAll()); } while(!catAndDogQueue.isCatEmpty()){ System.out.println(catAndDogQueue.pollCat()); } while(!catAndDogQueue.isDogEmpty()){ System.out.println(catAndDogQueue.pollDog()); } System.out.println(catAndDogQueue.poolAll()); } }
原文地址:https://www.cnblogs.com/quxiangxiangtiange/p/10322380.html
时间: 2024-10-11 07:45:30