猫狗队列题目 2022-05-22 11:45 132阅读 0赞 源码下载链接:[https://github.com/huijuanl/AlgorithmLearning.git][https_github.com_huijuanl_AlgorithmLearning.git] 目录: ./[src][]/[main][]/[java][]/CatDogPackage/ 实现一种猫狗队列的结构,要求如下: 用户可以调用add方法将cat类或者dog类的实例放入队列中; 用户可以调用poll方法,将队列中所有的实例按照队列的先后顺序依次弹出; 用户可以调用pollDog方法,将队列中dog类的实例按照队列的先后顺序依次弹出; 用户可以调用pollCat方法,将队列中cat类的实例按照队列的先后顺序依次弹出; 用户可以调用isEmpty方法,检查队列中是否还有dog和cat的实例; package CatDogPackage; import java.util.LinkedList; import java.util.Queue; //已经有的类:Pet,Dog,Cat class Pet { private String type; public Pet(String type) { this.type = type; } public String getPetType() { return this.type; } } class Dog extends Pet { public Dog() { super("Dog"); } } class Cat extends Pet { public Cat() { super("Cat"); } } //为解决猫狗队列问题新加的类:PetEnterQueue class PetEnterQueue { Pet pet; int count; public PetEnterQueue(Pet pet, int count) { this.pet = pet; this.count = count; } public Pet getPet() { return this.pet; } public int getCount() { return this.count; } } public class DogCatQueue { Queue<PetEnterQueue> DogQueue; Queue<PetEnterQueue> CatQueue; int count; public DogCatQueue() { DogQueue = new LinkedList<PetEnterQueue>(); CatQueue = new LinkedList<PetEnterQueue>(); count = 0; } public void add(Pet pet) { if (pet.getPetType().equals("Dog")) this.DogQueue.add(new PetEnterQueue(pet, count++)); else if (pet.getPetType().equals("Cat")) this.CatQueue.add(new PetEnterQueue(pet, count++)); else throw new RuntimeException("error:not Dog or Cat"); } public boolean isEmpty() { return this.DogQueue.isEmpty() && this.CatQueue.isEmpty(); } public boolean isDogQueueEmpty() { return this.DogQueue.isEmpty(); } public boolean isCatQueueEmpty() { return this.DogQueue.isEmpty(); } public Pet poll() { if (!this.DogQueue.isEmpty() && !this.CatQueue.isEmpty()) { if (this.DogQueue.peek().getCount() < this.CatQueue.peek().getCount()) return this.DogQueue.poll().getPet(); else return this.CatQueue.poll().getPet(); } else { if (!this.DogQueue.isEmpty()) return this.DogQueue.poll().getPet(); else if (!this.CatQueue.isEmpty()) return this.CatQueue.poll().getPet(); else throw new RuntimeException("error:DogCatQueue is empty"); } } public Dog pollDog() { if (!this.DogQueue.isEmpty()) return (Dog) this.DogQueue.poll().getPet();//强制转换 else throw new RuntimeException("error:DogQueue is empty"); } public Cat pollCat() { if (!this.CatQueue.isEmpty()) return (Cat) this.CatQueue.poll().getPet();//强制转换 else throw new RuntimeException("error:CatQueue is empty"); } } 测试: package CatDogPackage; import org.junit.Test; import static org.junit.Assert.*; public class DogCatQueueTest { @Test public static void main(String[] args) { DogCatQueue test = new DogCatQueue(); Pet dog1 = new Dog(); Pet cat1 = new Cat(); Pet dog2 = new Dog(); Pet cat2 = new Cat(); Pet dog3 = new Dog(); Pet cat3 = new Cat(); test.add(dog1); test.add(cat1); test.add(dog2); test.add(cat2); test.add(dog3); test.add(cat3); // while (!test.isDogQueueEmpty()) { // System.out.println(test.pollDog().getPetType()); // } while (!test.isEmpty()) { System.out.println(test.poll().getPetType()); } } } [https_github.com_huijuanl_AlgorithmLearning.git]: https://github.com/huijuanl/AlgorithmLearning.git [src]: https://github.com/huijuanl/AlgorithmLearning/tree/master/src [main]: https://github.com/huijuanl/AlgorithmLearning/tree/master/src/main [java]: https://github.com/huijuanl/AlgorithmLearning/tree/master/src/main/java
相关 java-猫狗继承案例 猫狗继承案例 定义一个父类: package day08; public class Animal { private String n 绝地灬酷狼/ 2022年06月13日 03:14/ 0 赞/ 199 阅读
相关 猫狗队列题目 源码下载链接:[https://github.com/huijuanl/AlgorithmLearning.git][https_github.com_huijuanl_Alg 桃扇骨/ 2022年05月22日 11:45/ 0 赞/ 133 阅读
相关 数据结构与算法之猫狗队列 数据结构与算法之猫狗队列 -------------------- 目录 1. 猫狗队列 -------------------- 1. 猫狗队列 1 一时失言乱红尘/ 2021年12月24日 00:59/ 0 赞/ 166 阅读
相关 猫狗数据集 import numpy as np import pickle import cv2 import pandas as pd import tensorflow as 怼烎@/ 2021年11月24日 02:10/ 0 赞/ 375 阅读
相关 AlexNet-基于keras训练猫狗分类 什么是AlexNet模型 ![在这里插入图片描述][20210326193455111.png] ![在这里插入图片描述][20210326193502158.png 以你之姓@/ 2021年11月17日 22:46/ 0 赞/ 398 阅读
相关 keras实战-猫狗数据-VGG16 原理图 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLm 男娘i/ 2021年11月17日 18:28/ 0 赞/ 264 阅读
相关 【图像识别】猫狗识别(CNN) > 数据集: > > 1. 共2000张图片,1000张狗,1000张猫; > 2. 1400张用于训练,600张用于测试; > 3. 图片均为 RGB 3 通道,尺 水深无声/ 2021年11月01日 13:22/ 0 赞/ 780 阅读
相关 paddlepaddle实现猫狗分类 目录 1.预备工作 1.1 数据集准备 1.2 数据预处理 2.训练 2.1 模型 2 待我称王封你为后i/ 2021年10月13日 04:19/ 0 赞/ 266 阅读
相关 【牛客】猫狗收容所 题目描述 有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫 朴灿烈づ我的快乐病毒、/ 2021年10月03日 02:00/ 0 赞/ 272 阅读
还没有评论,来说两句吧...