interface Pet{
public String getName();
public String getColor();
public int getAge();
}
class Cat implements Pet{
private String name;
private String color;
private int age;
public Cat(String name,String color,int age){
this.setName(name);
this.setColor(color);
this.setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Dog implements Pet{
private String name;
private String color;
private int age;
public Dog(String name,String color,int age){
this.setName(name);
this.setColor(color);
this.setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
class PetShop{
private Pet[]pets;
private int foot;
public PetShop(int len){
if(len>){
this.pets = new Pet[len];
}else{
this.pets = new Pet[];
}
}
public boolean add(Pet pet){
if(this.foot<this.pets.length){
this.pets[this.foot]=pet;
this.foot++;
return true;
}else{
return false;
}
}
public Pet[] search(String keyWord){
Pet p[]=null;
int count=;
for(int i=;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-||this.pets[i].getColor().indexOf(keyWord)!=-){
count++;
}
}
}
p=new Pet[count];
int f=;
for(int i=;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-||this.pets[i].getColor().indexOf(keyWord)!=-){
p[f]=this.pets[i];
f++;
}
}
}
return p;
}
} public class work {
public static void main(String[] args) {
PetShop ps = new PetShop();
ps.add(new Cat("白猫","白色",));
ps.add(new Cat("黑猫","黑色",));
ps.add(new Cat("花猫","黑白色",));
ps.add(new Cat("泰迪","棕色",));
ps.add(new Cat("博美","白色",));
ps.add(new Cat("二哈","黑白色",));
print(ps.search("猫"));
}
public static void print(Pet p[]){
for(int i=;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName()+","+p[i].getColor()+","+p[i].getAge());
}
}
}
}

java作业8的更多相关文章

  1. 第五次Java作业

    作业一: 文件显示列表框. 增加了下拉式组合框,text区域设置颜色为红色. import javax.swing.*; import java.awt.*; import java.io.File; ...

  2. Java作业:第四次过程性考核 ——长春职业技术学院 16级网络工程

    Java作业:第四次过程性考核 码云链接:https://gitee.com/SoridoD/java_kaohe4    (时间匆忙没打注释,真有急事) (客户端和服务器会自动创建表,所以没有sql ...

  3. Java作业:第一次过程性考核 ——长春职业技术学院 16级网络工程

    Java作业:第一次过程性考核 ••<结构化程序设计>•• 考核目标:初步了解Java基本语法规则,学习结构化程序设计思想. 码云链接:https://gitee.com/SoridoD/ ...

  4. 初识Java作业

    初识Java作业 一.    填空题 Java技术按照用途不同分为三大版本,分别是JavaSE.     javaEE       和JavaMe Java虚拟机就是一个虚拟的用于执行  .class ...

  5. JAVA作业—字符串操作

    ------------恢复内容开始------------ ------------恢复内容开始------------ ------------恢复内容开始------------ ------- ...

  6. JAVA作业02

    一,      课堂练习 (一)构造方法 1,源代码 public class Test{ public static void main(String[] args){ Foo obj1=new F ...

  7. java作业4

    (一)  请查看String.equals()方法的实现代码,注意学习其实现方法.(发表到博客作业上) (二)  整理String类的Length().charAt(). getChars().rep ...

  8. Java作业代写

    作业一 试用java编写一个九九乘法表并打印. 作业二: 设计两个人类与书类,并设置两者的关系,试用人去找书,书去找人,假如某人有一个儿子,它也有一本书,试用儿子去找书,书找儿子. 大作业 熟悉QQ农 ...

  9. java作业—3

    动手动脑: 一.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. 方法1(数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例:(int)(1+Math.r ...

  10. Java作业-网络编程

    Java网络编程 关于结合以前的大作业(即我的图书馆管理系统) 我感觉,图书馆管理系统更像是一个偏向于B/S模式的体系,如果想让他可用性变得更好,可以优化的地方只有使用数据库来代替文件,我个人是没有想 ...

随机推荐

  1. Python基础总结与实践

    Python简介 Python是一种动态解释型编程语言,在模块载入时将源码编译成字节码, 这些字节码被虚拟机PVM解释执行,其中解释执行是Python性能较低的主要原因: Python使用C语言编写, ...

  2. JS.match方法 正则表达式

    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. <sc ...

  3. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

  4. 手机上如何远程控制Linux服务器?

    这里介绍3个手机软件,分别是JuiceSSH.Termius和Termux,这3个软件都可以实现远程控制Linux服务器(相当于手机SSH客户端),而且使用起来都非常方便,下面我简单介绍一下这3个软件 ...

  5. RobotFramework:钉钉扫码登录UI自动化

    背景: 遇到一个项目,使用的是钉钉扫码登录,一时间不知道该怎么下手了,还是先F12抓包看下都有什么数据传输吧. 分析: 先熟悉下钉钉扫码登录的逻辑,参考官文:https://open-doc.ding ...

  6. JavaScript操作Array对象常用的方法

     转换方法 因为JavaScript内部机制(继承),所有的对象都具有toLocalString() .toString().valueOf()方法,Array也不例外so:var colors = ...

  7. vue-cli npm run build 打包问题 webpack@3.6

    1, vue-router 路由 有两个模式 (mode) hash (默认模式) 使用URL来模拟一个完整的URL 但是没个URL都会带上 "#/'' 支持所有浏览器 这个模式使用 red ...

  8. js控制时间显示格式

    Date.prototype.Format = function (fmt) { //author: meizz     var o = {        "M+": this.g ...

  9. jsp中的文件上传

    首先需要有以下的jar包 jsp代码如下: <!-- ${pageContext.request.contextPath}为: "/" + 当前项目名 --> < ...

  10. tcl之基本语法—1