三.猫狗队列

【题目】 宠物、狗和猫的类如下:
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"); } }

实现一种狗猫队列的结构。

要求如下:

1.用户可以调用add方法将cat类或dog类的实例放入队列中;

2.用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;

3.用户可以调用pollDog方法,将队列中dog类的实例按照进队列的先后顺序依次弹出;

4.用户可以调用pollCat方法,将队列中cat类的实例按照进队列的先后顺序依次弹出;

5.用户可以调用isEmpty方法,检查队列中是否还有dog或cat的实例;

6.用户可以调用isDogEmpty方法,检查队列中是否有dog类的实例;

7.用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。

分析:

1.提供猫狗宠物类;

2.提供猫队列存放猫实现pollCat,提供狗队列存放狗实现pollDog方法,提供一个大的队列,存放的是是猫狗队列和时间戳(时间戳是用来标记猫狗的先后顺序)实现pollAll方法;实际中不一定是只存放猫狗,或者还有其他的宠物种类,我们抽象为PetEnterQueue类(包含有宠物类和时间戳)。 提供PetEnterQueue类和CatDogQueue;

3.isEmpty:猫狗队列都为空返回true;

4.isDogEmpty:狗队列为空返回true;

5.isCatEmpty:猫队列为空返回true;

6.pollDog:狗队列为空抛出异常,不为空抛出并且删除第一只狗;

7.pollCat:猫队列为空抛出异常,不为空抛出并且删除第一只猫;

8.pollAll:猫狗队列不为空时,获取猫队列中第一只猫的时间戳和狗队列中第一只狗的时间戳,小的先出;

9.9.add:如果是猫构建宠物进对象,加到猫队列;狗同理;

 

//1.提供猫狗宠物类;

//宠物类
public static class Pet{
	private String type;
	public Pet(String  type) {
		this.type=type;
	}
	public String getType() {
		return this.type;
	}
}
			
//猫
public static class Cat extends Pet {
	public Cat() {
		super("cat");
	}
}
			
//狗
public static class Dog extends Pet{
	public Dog() {
		super("dog");
	}
}

//2.提供PetEnterQueue类和CatDogQueue

//小队列中的元素种类
public static class PetEnterQueue{
	private Pet pet;
	private long count;
	public PetEnterQueue(Pet pet,long count) {
		this.pet=pet;
		this.count=count;
	}
	public Pet getPet() {
		return pet;
	}
	public long getCount() {
		return count;
	}
	public String getEnterPetType() {
		return this.getPet().getType();
	}
}


//CatDogQueue
public static class CatDogQueue{
	private Queue<PetEnterQueue> dogQ;
	private Queue<PetEnterQueue> catQ;
	private long count;//时间戳
		
	public CatDogQueue() {
		dogQ=new LinkedList<PetEnterQueue>();
		catQ=new LinkedList<PetEnterQueue>();
		count=0;
	}

    //3.isEmpty:猫狗队列都为空返回true;  
    public boolean isEmpty() {
		return this.dogQ.isEmpty()&&this.catQ.isEmpty();
	}

    //4.isDogEmpty:狗队列为空返回true;
    public boolean isDogQueueEmpty() {
		return this.dogQ.isEmpty();
	}

    //5.isCatEmpty:猫队列为空返回true;
    public boolean isCatQueueEmpty() {
		return this.catQ.isEmpty();
	}

    //6.pollDog:狗队列为空抛出异常,不为空抛出并且删除第一只狗;
    public Dog pollDog() {
		if(!this.isDogQueueEmpty()) {
			return (Dog) this.dogQ.poll().getPet();
		}
		else {
			throw new RuntimeException(" error , dogqueue is empty");
		}	
	}

    //7.pollCat:猫队列为空抛出异常,不为空抛出并且删除第一只猫;
    public Cat pollCat() {
		if(!this.isCatQueueEmpty()) {
			return (Cat) this.catQ.poll().getPet();
		}
		else {
			throw new RuntimeException(" error , catqueue is empty");
		}	
	}	

    //8.pollAll:猫狗队列不为空时,获取猫队列中第一只猫的时间戳和狗队列中第一只狗的时间戳,小的先出;
    public Pet pollAll() {
		if(!this.dogQ.isEmpty()&&!this.catQ.isEmpty()) {
			if(this.dogQ.peek().getCount()<this.catQ.peek().getCount()) {
				return this.dogQ.poll().getPet();
			}
			else {
				return this.catQ.poll().getPet();
			}
		}
		else if(!this.dogQ.isEmpty()) {
			return this.dogQ.poll().getPet();
		}
		else if(!this.catQ.isEmpty()) {
			return this.catQ.poll().getPet();
		}
		else {
			throw new RuntimeException(" error , queue is empty");
		}
	}

    //9.add:如果是猫构建宠物进对象,加到猫队列;狗同理;
    public void add(Pet pet) {
			if(pet.getType().equals("dog")) {
				this.dogQ.add(new PetEnterQueue(pet,this.count++));
			}else if(pet.getType().equals("cat")) {
				this.catQ.add(new PetEnterQueue(pet,this.count++));
			}else {
				throw new RuntimeException(" error , not dog or cat");
			}
		}
    

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值