【题目】
宠物、狗和猫的类如下:
public class Pet { private String type; public Pet(String type) { this.type = type; } public String getPetType() { return this.type; }}public class Dog extends Pet { public Dog() { super("dog"); } }public class Cat extends Pet { public Cat() { super("cat"); } }实现一种狗猫队列的结构,要求如下: 用户可以调用add方法将cat类或dog类的实例放入队列中; 用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出; 用户可以调用pollDog方法,将队列中dog类的实例按照进队列的先后顺序依次弹出; 用户可以调用pollCat方法,将队列中cat类的实例按照进队列的先后顺序依次弹出; 用户可以调用isEmpty方法,检查队列中是否还有dog或cat的实例; 用户可以调用isDogEmpty方法,检查队列中是否有dog类的实例; 用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。【题解】
使用类包含类的方法,且使用三个队列,分别存放总的类、cat类、dog类【代码】
1 package chapter_1_stackandqueue; 2 3 import java.util.LinkedList; 4 import java.util.Queue; 5 6 public class Problem_04_DogCatQueue { 7 8 public static class Pet { 9 private String type; 10 11 public Pet(String type) { 12 this.type = type; 13 } 14 15 public String getPetType() { 16 return this.type; 17 } 18 } 19 20 public static class Dog extends Pet { 21 public Dog() { 22 super("dog"); 23 } 24 } 25 26 public static class Cat extends Pet { 27 public Cat() { 28 super("cat"); 29 } 30 } 31 32 public static class PetEnterQueue { 33 private Pet pet; 34 private long count; 35 36 public PetEnterQueue(Pet pet, long count) { 37 this.pet = pet; 38 this.count = count; 39 } 40 41 public Pet getPet() { 42 return this.pet; 43 } 44 45 public long getCount() { 46 return this.count; 47 } 48 49 public String getEnterPetType() { 50 return this.pet.getPetType(); 51 } 52 } 53 54 public static class DogCatQueue { 55 private QueuedogQ; 56 private Queue catQ; 57 private long count; 58 59 public DogCatQueue() { 60 this.dogQ = new LinkedList (); 61 this.catQ = new LinkedList (); 62 this.count = 0; 63 } 64 65 public void add(Pet pet) { 66 if (pet.getPetType().equals("dog")) { 67 this.dogQ.add(new PetEnterQueue(pet, this.count++)); 68 } else if (pet.getPetType().equals("cat")) { 69 this.catQ.add(new PetEnterQueue(pet, this.count++)); 70 } else { 71 throw new RuntimeException("err, not dog or cat"); 72 } 73 } 74 75 public Pet pollAll() { 76 if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) { 77 if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) { 78 return this.dogQ.poll().getPet(); 79 } else { 80 return this.catQ.poll().getPet(); 81 } 82 } else if (!this.dogQ.isEmpty()) { 83 return this.dogQ.poll().getPet(); 84 } else if (!this.catQ.isEmpty()) { 85 return this.catQ.poll().getPet(); 86 } else { 87 throw new RuntimeException("err, queue is empty!"); 88 } 89 } 90 91 public Dog pollDog() { 92 if (!this.isDogQueueEmpty()) { 93 return (Dog) this.dogQ.poll().getPet(); 94 } else { 95 throw new RuntimeException("Dog queue is empty!"); 96 } 97 } 98 99 public Cat pollCat() {100 if (!this.isCatQueueEmpty()) {101 return (Cat) this.catQ.poll().getPet();102 } else103 throw new RuntimeException("Cat queue is empty!");104 }105 106 public boolean isEmpty() {107 return this.dogQ.isEmpty() && this.catQ.isEmpty();108 }109 110 public boolean isDogQueueEmpty() {111 return this.dogQ.isEmpty();112 }113 114 public boolean isCatQueueEmpty() {115 return this.catQ.isEmpty();116 }117 118 }119 120 public static void main(String[] args) {121 DogCatQueue test = new DogCatQueue();122 123 Pet dog1 = new Dog();124 Pet cat1 = new Cat();125 Pet dog2 = new Dog();126 Pet cat2 = new Cat();127 Pet dog3 = new Dog();128 Pet cat3 = new Cat();129 130 test.add(dog1);131 test.add(cat1);132 test.add(dog2);133 test.add(cat2);134 test.add(dog3);135 test.add(cat3);136 137 test.add(dog1);138 test.add(cat1);139 test.add(dog2);140 test.add(cat2);141 test.add(dog3);142 test.add(cat3);143 144 test.add(dog1);145 test.add(cat1);146 test.add(dog2);147 test.add(cat2);148 test.add(dog3);149 test.add(cat3);150 while (!test.isDogQueueEmpty()) {151 System.out.println(test.pollDog().getPetType());152 }153 while (!test.isEmpty()) {154 System.out.println(test.pollAll().getPetType());155 }156 }157 158 }