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 void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.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 void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class PetShop{
private Pet[] pets ;
private int foot ;
public PetShop(int len){
if(len>0){
this.pets = new Pet[len] ;
}else{
this.pets = new Pet[1] ;
}
}
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 = 0 ;
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
count++ ;
}
}
}
p = new Pet[count] ;
int f = 0 ;
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
p[f] = this.pets[i] ;
f++ ;
}
}
}
return p ; }
};
public class HomeworkDemo{
public static void main(String args[]){
PetShop ps = new PetShop(5) ;
ps.add(new Cat("英短","灰色的",3)) ;
ps.add(new Cat("布偶猫","白色的",2)) ;
ps.add(new Cat("花猫","花色的",4)) ;
ps.add(new Dog("哈士奇","黑白色的",5)) ;
ps.add(new Dog("金毛","金色的",6)) ;
ps.add(new Dog("黑贝","棕色的",3)) ;
print(ps.search("白")) ;
}
public static void print(Pet p[]){
for(int i=0;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName() + "," + p[i].getColor()
+"," + p[i].getAge()) ;
}
}
}
};

java第7次作业的更多相关文章

  1. 2016-2017-2 《Java程序设计》预备作业2总结

    2016-2017-2 <Java程序设计>预备作业2总结 古希腊学者普罗塔戈说过:「头脑不是一个要被填满的容器,而是一束需要被点燃的火把.」 在对计算机系的学生情况的调查中,我说: 最近 ...

  2. 2016-2017-2 《Java程序设计》预备作业1 总结

    2016-2017-2 <Java程序设计>预备作业1 总结 预备作业01:你期望的师生关系是什么见https://edu.cnblogs.com/campus/besti/2016-20 ...

  3. JAVA第三周作业(从键盘输入若干数求和)

    JAVA第三周作业(从键盘输入若干数求和) 在新的一周,我学习了JAVA的IO编程.下面的代码实现了从键盘输入若干数求和的目标.import java.util.Scanner; public cla ...

  4. JAVA第八次作业

    JAVA第八次作业 (一)学习总结 1.用思维导图对本周的学习内容进行总结 参考资料: XMind. 2.通过实验内容中的具体实例说明在执行executeUpdate()方法和executeQuery ...

  5. Java第三次作业——面向对象基础(封装)

    Java第三次作业--面向对象基础(封装) (一)学习总结 1.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明. 封装性 封装性是面向对象的方法所应遵循的一个重要原则,它有两个含义: ...

  6. oozie JAVA Client 编程提交作业

    1,eclipse环境搭建 在eclipse中新建一个JAVA工程,导入必要的依赖包,目前用到的有: 其次编写JAVA 程序提交Oozie作业,这里可参考:oozie官方参考文档 在运行提交程序前,首 ...

  7. 2017-2018-1 JAVA实验站 第二周作业

    2017-2018-1 JAVA实验站 第二周作业 小组成员: 组长 20162318张泰毓 成员 20162303石亚鑫 20162304张浩林 20162307张韵琪 20162321王彪 201 ...

  8. 2017《Java技术》预备作业01

    2017<Java技术>预备作业 1.学习使用MarkDown 本学期的随笔都将使用MarkDown格式,要求熟练掌握MarkDown语法,学会如何使用标题,插入超链接,列表,插入图片,插 ...

  9. Java第五次作业--面向对象高级特性(抽象类与接口)

    Java第五次作业--面向对象高级特性(抽象类与接口) (一)学习总结 1.在上周完成的思维导图基础上,补充本周的学习内容,对Java面向对象编程的知识点做一个全面的总结. 2.汽车租赁公司,出租汽车 ...

  10. Java第四次作业——面向对象高级特性(继承和多态)

    Java第四次作业--面向对象高级特性(继承和多态) (一)学习总结 1.学习使用思维导图对Java面向对象编程的知识点(封装.继承和多态)进行总结. 2.阅读下面程序,分析是否能编译通过?如果不能, ...

随机推荐

  1. github收藏夹

    技术面试必备基础知识 https://github.com/CyC2018/CS-Notes 程序员简历模板列表 https://github.com/geekcompany/ResumeSample ...

  2. jsp+postgresql学习笔记(1)用户登录与注册

    前期准备: tomcat的安装与配置(略) jdk的安装与配置(略) eclipse软件安装与配置(略) webstrom软件或IDEA的安装与配置(大概用了IDEA就不需要eclipse了,但是怎么 ...

  3. 【新特性】JDK1.9

    一.目录结构 JDK9具体目录结构如下所示: bin: 该目录包含所有的命令. conf: 包含用户可以编辑的配置文件,例如以前位于jre\lib 目录中的.properties 和 .policy ...

  4. vscode下运行matlab记录

    Linux下安装MATLAB可参照以下链接: https://blog.csdn.net/qq_31285709/article/details/82083902 在vscode 中运行.m文件. 首 ...

  5. 使用ODP.NET连接Oracle数据库

    项目需要和第三方进行对接,奇葩的是地方没给提供接口,却提供了一个Oracle的视图 所以有了C#访问Oracle数据库的需求 1.打开nuget,安装驱动 https://www.nuget.org/ ...

  6. BASE64加解密

    - JDK 自带 - Commons Codec  地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi - Bou ...

  7. 前端开发需要掌握的SEO的知识点

    SEO 工作的目的 seo 的工作目的是为了让网站更利于让各大搜索引擎抓取和收录,增加产品的曝光率. SEO 注意事项 1. 网站 TDK 标签的设置.title,description,keywor ...

  8. DAY 17常用模块

    一.时间模块:time 1.时间戳:time.time() # 可以作为数据的唯一标识 print(time.time) # 1554878849.8452318 2.延迟线程的运行:time.sle ...

  9. JAVA-变量

    静态变量只能在类主体中定义,不能在方法中定义

  10. snakemake使用小结

    首先在linux 里配置conda 下载 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linu ...