类设计与实现综合实验

实验内容:

请替宠物医院设计并完成一个宠物信息系统,现有完成系统的的类的uml图,及部分类的部分实现代码,和一个测试类P3main.java及main方法的标准输出。总体要求如下:

  1. P3main.java中的main()方法实现对整个宠物信息系统的测试,请不要对main()方法的已有语句做任何修改,但如果你觉得这个方法有些功能还没有测试到的话,可以在该方法新增测试语句。
  2. 系统完成后,对比P3main.java的输出和标准输出,调整你的程序,直至输出和标准输出完全一样。
  3. 仔细阅读类的uml图,明确类之间继承关系,以及类与接口的关系。
  4. 本次作业会考察大家对数组的应用,请不要用java类库中的ArrayList或其它的集合类来完成数据的存储及相关操作。
  5. 完成的类中不能有public的成员变量。

以下是关于你需要实现的类的一些重要说明,请仔细阅读,但说明并不全面,所以你还需要根据功能编写一些额外的方法或域。

  1. Pet类:该类主要用户记录一个宠物的相关信息

A. 一个宠物必须包含name(宠物名),owner’s name(宠物主人的名字)和weight(宠物体重)和其它一些必须的数据。

B. Pet类必须实现Comparable接口,以支持在Vet类中有对庞物排序。排序的依据为首先考虑宠物主人的姓名的字母顺序,再考虑宠物的名字字母顺序,排序的时候均忽略大小写。

C.Pet类必须实现equals方法,如果两个宠物的主人名字和庞物的名字都相等(忽略大小写),则认为这两个宠物相等。

D. 已提供该类的基本架构和相关的文档注释,除了补充完成相关方法之外,还需要根据类的功能增加必要的域和方法。

  1. Cat类:Pet的子类,记录宠物猫的相关信息。

A.该类需要一个实例域来记录该宠物猫是一只仅在户内饲养的猫(inside)还是一只也会去户外活动的猫(outside)。

B.该类需要一个实例域方法能在猫去户外后,改变其状态为一只(outside)的猫。

C. 所有的宠物猫默认情况下仅在户内饲养。但一旦去了一次户外,就是一只outside的猫了。

D.该类需要重写visit()方法,重写后功能为:宠物猫每次来宠物医院,会需要额外20$的洗牙费(每次来都会洗),同时outside猫每次相比一般宠物(Pet)还要多打一针(shot),该针的费用同一般宠物(Pet)打针费用。重写该方法的时候需要注意,Pet类的相关数据也需要正确更新(提示,可先调用父类的visit()方法)。

  1. Dog类:Pet的子类,记录宠物狗的相关信息。

A.该类需要一个实例域来记录该宠物狗是一只小型(small)、中型(medium)还是大型(large)的狗。该实例域在构造方法中赋值,可参看P3main.java中的语句。

B.该类需要重写visit()方法,重写后功能为:宠物狗每次来宠物医院,需要收取的额外费用为:15$的修剪指甲,中型(Medium)狗的打针费用每针比一般宠物(Pet)多收取2.5$,大型(Large)狗的打针费用每针比一般宠物(Pet)多收取5$。重写该方法的时候需要注意,Pet类的相关数据也需要正确更新(提示,可先调用父类的visit()方法)。

  1. Vet类:宠物医院类

A. 该类需要保存但不限于医院的名字以及在该医院就诊的宠物信息集合。

B.该类必须实现已提供的Database.java中的接口,部分代码已经给出,除了完善相关方法之外,也需要根据类的功能增加必要的实例域和相关方法。

C.delete方法在删除相关数据后,不能改变数组中数据顺序。

cat

package Hospital;public class Cat extends Pet{private boolean inside=true;void goOutside(){inside=false;}public Cat(String pname, String oname, double wt) {super(pname, oname, wt);this.inside = true;}@Overridepublic double visit(int shots) {double cost=super.visit(shots);cost+=20;this.setSum_cost(20);if(!this.inside){cost+=30;this.setSum_cost(30);}return cost;}
}

Database

package Hospital;/** An interface with common collection operations.*/
public interface Database {// REMEMBER THAT ALL MEMBERS OF AN INTERFACE ARE PUBLIC BY DEFAULT/** The default initial size of a database. */int STARTSIZE = 20;  // this is public, static, final by default/** Find out how many things are actually in the database.@return the number*/int size();/** Display the items in the database on the screen.*/void display();/** Find a particular item in the database.@param o the object to search for, based on equals@return the object if found, null otherwise*/Object find(Object o);/** Add an item to the database, if there is room.@param o the object to add@return true if added, false otherwise*/boolean add(Object o);/** Delete an item from the database, if it is there.@param o the object to delete@return the item if deleted, null otherwise*/Object delete(Object o);}

Dog

package Hospital;public class Dog extends Pet{private String huge;public Dog(String pname, String oname, double wt, String huge) {super(pname, oname, wt);this.huge = huge;}@Overridepublic double visit(int shots) {double cost= super.visit(shots);cost+=15;this.setSum_cost(15);if(!huge.equals("small")){cost=cost+2.5*shots;this.setSum_cost(2.5*shots);if(huge.equals("large")){cost=cost+2.5*shots;this.setSum_cost(2.5*shots);}}return cost;}
}

P3main

package Hospital;/***  This program does not test everything your class*  system is supposed to do, so you might want to write*  some of your own tests.*/
public class P3main {/** Main driver method.* @param args the arguments (not used)*/public static void main(String[] args) {Vet avet = new Vet(50, "Pets R Us");Pet p;p = new Pet("Rudolph", "Santa Claus", 350);p.visit(1);avet.add(p);avet.add(new Pet("Tweetie Bird", "Looney Tunes", 1.75));System.out.println("visit1: " + p.visit(1));System.out.println();show(avet);p = new Cat("Tiger", "Some Body", 8);avet.add(p);System.out.println("visit0: " + p.visit(0));Cat c = new Cat("Sylvester", "Looney Tunes", 15.5);System.out.println("inside visit1: " + c.visit(1));c.goOutside();System.out.println("outside visit1: " + c.visit(1));System.out.println("c is " + c);avet.add(c);System.out.println();show(avet);Dog d = new Dog("Fido", "Some Body", 32.2, "medium");System.out.println("med visit3: " + d.visit(3));avet.add(d);p = new Dog("Dino", "Flintstones", 150, "large");avet.add(p);System.out.println("lg visit3: " + p.visit(3));System.out.println();show(avet);avet.add(new Dog("Benji", "Joe Camp", 10, "small"));p = new Cat("Tony", "Kellogg", 20);avet.add(p);System.out.println();show(avet);System.out.println("\na few more tests...");System.out.println("find p: " + avet.find(p));p = (Pet) avet.find(new Pet("Tweetie Bird", "Looney Tunes", 1.5));System.out.println("Tweetie 5 shots: " + p.visit(5));System.out.println("found Tweetie: " + p);System.out.println("find Fido: "+ avet.find(new Dog("Fido", "some body", 32.2, "medium")));System.out.println("adding string to Vet: " + avet.add("not a pet"));System.out.println("deleting string: " + avet.delete("just a string"));System.out.println();System.out.println("\ndeleting pets:");System.out.println(avet.delete(p));System.out.println(avet.delete(d));System.out.println(avet.delete(c));show(avet);avet.add(new Pet("Happy", "Some Body", 13.5));avet.sort();System.out.println("\nafter sorting:");show(avet);}/** Output the current state of things to the screen.*  @param myvet the vet records to display*/public static void show(Vet myvet) {System.out.println("--- Vet has " + myvet.size() + " clients");myvet.display();System.out.println(">> average client weight: " + myvet.averageWeight());}
}/* Here is the result of running the program:visit1: 115.0--- Vet has 2 clients
Vet Pets R Us client list:
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit
>> average client weight: 175.875
visit0: 105.0
inside visit1: 135.0
outside visit1: 165.0
c is outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit--- Vet has 4 clients
Vet Pets R Us client list:
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit
>> average client weight: 93.8125
med visit3: 197.5
lg visit3: 205.0--- Vet has 6 clients
Vet Pets R Us client list:
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit
medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit
>> average client weight: 92.90833333333335--- Vet has 8 clients
Vet Pets R Us client list:
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit
medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit
small dog Benji (owner Joe Camp) 10.0 lbs, $0.00 avg cost/visit
inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit
>> average client weight: 73.43125a few more tests...
find p: inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit
Tweetie 5 shots: 235.0
found Tweetie: Tweetie Bird (owner Looney Tunes) 1.75 lbs, $235.00 avg cost/visit
find Fido: medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit
adding string to Vet: false
deleting string: nulldeleting pets:
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $235.00 avg cost/visit
medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit
--- Vet has 5 clients
Vet Pets R Us client list:
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit
small dog Benji (owner Joe Camp) 10.0 lbs, $0.00 avg cost/visit
inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit
>> average client weight: 107.6after sorting:
--- Vet has 6 clients
Vet Pets R Us client list:
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit
small dog Benji (owner Joe Camp) 10.0 lbs, $0.00 avg cost/visit
inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit
Happy (owner Some Body) 13.5 lbs, $0.00 avg cost/visit
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit
>> average client weight: 91.91666666666667*/

Pet

package Hospital;import java.text.DecimalFormat;
import java.util.Objects;public class Pet implements Comparable<Pet> {/*** Handy for formatting.*/private static DecimalFormat money = new DecimalFormat("0.00");/* The access specifiers for these variables must not be changed! */private String name;private String owner;private double weight;private double sum_cost;private int time;/*** Create a Pet object, initializing data members.** @param pname the Pet's name* @param oname the owner's name* @param wt    the weight of the pet*/public Pet(String pname, String oname, double wt) {this.name = pname;this.owner = oname;this.weight = wt;this.sum_cost = 0;this.time = 0;}public Pet() {/*this.name = null;this.owner = null;this.weight = 0;this.sum_cost=0;this.time=0;*/}public static DecimalFormat getMoney() {return money;}public static void setMoney(DecimalFormat money) {Pet.money = money;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getOwner() {return owner;}public void setOwner(String owner) {this.owner = owner;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public double getSum_cost() {return sum_cost;}public void setSum_cost(double sum_cost) {this.sum_cost += sum_cost;                         //更改}public int getTime() {return time;}public void setTime(int time) {this.time = time;}@Overridepublic String toString() {return this.name + " (owner " + this.owner + ") " + this.weight+ " lbs, $" + money.format(this.avgCost()) + " avg cost/visit  ";}/*** The Pet is visiting the vet, and will be charged accordingly.* The base cost for a visit is $85.00, and $30/shot is added.** @param shots the number of shots the pet is getting* @return the entire cost for this particular visit*/public double visit(int shots) {time++;sum_cost += 85 + shots * 30;return 85 + shots * 30;}/*** Determine the average cost per visit for this pet.** @return that cost, or 0 if no visits have occurred yet*/public double avgCost() {if (time == 0) {return 0;}return sum_cost / time;}public boolean equals(Pet o){if (this.compareTo(o)==0){return true;}else return false;}@Overridepublic int compareTo(Pet o) {for (int i = 0; i < owner.length(); i++) {if (this.owner.toLowerCase().toCharArray()[i] > o.owner.toLowerCase().toCharArray()[i]) {return 1;}if (this.owner.toLowerCase().toCharArray()[i] < o.owner.toLowerCase().toCharArray()[i]) {return -1;}}for (int i = 0; i < name.length(); i++) {if (this.name.toLowerCase().toCharArray()[i] > o.name.toLowerCase().toCharArray()[i]) {return 1;}if (this.name.toLowerCase().toCharArray()[i] < o.name.toLowerCase().toCharArray()[i]) {return -1;}}return 0;}
}

VetStart

package Hospital;import java.util.Arrays;/*** Class to keep track of client (Pet) information for a Veterinary* practice. Some methods are sketched for you, but others will need* to be added in order to implement the Database interface and* support the P3main program and expected output. You'll also need* to add the data members.*/
class Vet implements Database {private int StartSize;private String Who;private int now_size;private Pet[] pets;/*** Create a veterinary practice.** @param startSize the capacity for how*                  many clients they can handle* @param who       the name of the vet practice*/public Vet(int startSize, String who) {StartSize = startSize;Who = who;now_size = 0;pets=new Pet[STARTSIZE];}@Overridepublic int size() {return now_size;}/*** Display the name of the Vet and all the clients, one per line,* on the screen. (See sample output for exact format.)*/public void display() {System.out.println("Vet " + Who+" client list:");this.sort();for (int i=0;i<now_size;i++) {System.out.println(pets[i].toString());}}@Overridepublic Object find(Object o) {int i;for (i = 0; i < now_size; i++) {if (pets[i].equals((Pet)o)) {return pets[i];}}return null;}/*** Add an item to the database, if there is room.* You are limited by the initial capacity.** @param o the object to add (must be a Pet)* @return true if added, false otherwise*/public boolean add(Object o) {if (now_size >= STARTSIZE) {return false;}if (!(o instanceof Pet)) return false;pets[now_size]=(Pet) o;now_size++;return true;}/*** Delete an item from the database, if it is there,* maintaining the current ordering of the list.** @param o the object to delete* @return the item if one is deleted, null otherwise*/public Object delete(Object o) {if(o instanceof Pet){for (int i = 0; i < this.now_size; i++) {if (pets[i] == (Pet) o) {for (int j = i; j+1< this.now_size; j++){pets[j]=pets[j+1];}now_size--;return (Pet)o;}}}return null;}/*** Compute the average weight over all clients.** @return the average*/public double averageWeight() {int i;double aveWeight=0;for (i = 0; i < now_size; i++) {aveWeight+=pets[i].getWeight();}return aveWeight/now_size;}/*** Sort the clients. (This is complete.)*/public void sort() {Arrays.sort(this.pets, 0, this.size());}}

java作业:类设计与实现综合实验相关推荐

  1. 【数据库 Microsoft SQL Server】实验六 物业收费管理系统数据库设计与实施综合实验

    实验六 物业收费管理系统数据库设计与实施综合实验 一.实验目的 1.掌握数据库概念模型和逻辑模型设计,学会使用数据库规范化理论规范关系模式. 2.熟练掌握和使用SQL语言定义数据库.表.索引和视图等对 ...

  2. java实体类设计_java实验1 实体类的设计-答案

    实验一实体类设计 一.实验时间:姓名:学号: 二.实验目的 1.掌握Java的类结构: 2.掌握实体类的作用: 3.能够对相似对象的共同属性进行抽象: 4.掌握对成员变量的赋值和取值函数编写: 5.理 ...

  3. 学生课程成绩信息实体表设计mysql_数据库综合实验--设计某高校学生选课管理系统...

    数据库综合实验重做 因为期末的时候做数据库综合实验太匆忙,很多地方都是能用就好,做完之后突然想到可以改进的方法,所以现在寒假来重做一下 题目如下: 设计某高校学生选课管理系统 实现学生信息.课程学生管 ...

  4. java测试类写三角形_软件测试实验一——使用junit判断三角形

    一.简单描述下安装 junit, hamcrest and eclemma的过程 ①当然,有了eclipse软件,安装的过程会显得比较轻松 对于安装junit和hamcrest来说需要在官网(或者其它 ...

  5. 【计算机网络】期末课程设计 ENSP组网综合实验(附工程文件)

    目录 前言 前期准备 组网要求 开始组网 分公司1 分公司2 核心交换机配置 实现内部服务器的搭建 acl_deny部分用户与服务器出口 配置出口防火墙 (修正)防火墙实现上网限制 dhcp分配ip ...

  6. 面对对象课程设计报告java,面向对象编程 JAVA编程综合实验报告.doc

    PAGE \* MERGEFORMAT 20 成绩: JAVA编程B综合实验报告 实验名称:面向对象编程 实验时间:2012年 5月 31日星期四 JAVA编程B综合实验报告 一.实验名称 面向对象编 ...

  7. 基于eNSP中大型校园/企业网络规划与设计_ensp综合大作业(ensp综合实验)

    作者:BSXY_19计科_陈永跃 BSXY_信息学院 注:未经允许禁止转发任何内容 基于eNSP中大型校园/企业网络规划与设计_综合大作业(ensp综合实验) 前言及技术/资源下载说明( **未经允许 ...

  8. 综合实验 电子记事本的设计与实现——Java

    综合实验 电子记事本的设计与实现 文章目录 综合实验 电子记事本的设计与实现 一.实验目的 二.实验内容 三.实验要求 四.实验步骤与结果 1.创建菜单栏.菜单项和文本域 2.txt文件过滤器 3.创 ...

  9. 基于eNSP的IPv4加IPv6的企业/校园网络规划设计(综合实验/大作业)

    作者:BSXY_19计科_陈永跃 BSXY_信息学院_名片v位于结尾处 注:未经允许禁止转发任何内容 基于eNSP的IPv4加IPv6的企业/校园网络规划设计_综合实验/大作业 前言及技术/资源下载说 ...

最新文章

  1. 我们讲得比开复专业一点,是《奇葩大会》的李开复
  2. golang 反射 获取 设置 结构体 字段值
  3. Tomcat5配置mysql4数据源
  4. 【Java】七巧板着色问题
  5. LeetCode刷题(40)--Search a 2D Matrix
  6. WorldFirstClassOnline
  7. 在电脑上安装python-在电脑上安装python的方法
  8. 本田HR-V Sport官图发布 换装高功率1.5T发动机
  9. 2019中国云计算十一大趋势预测与分析
  10. JS原生读取 本地 JSON
  11. shell脚本的错误检测总结
  12. Vue中使用地图平台MapboxGL
  13. LQ0264 鲁卡斯队列【精度计算】
  14. android 输入法如何启动流程_Android程序打开和关闭输入法
  15. 基于STM32F407标准库串口DMA+空闲中断
  16. python制作qq登录界面_Python制作一个仿QQ办公版的图形登录界面
  17. react webpack配置
  18. 后版权时代,网易云用IPO开启进击之路
  19. 黑马程序员_Java_多线程
  20. php寻仙记,wap寻仙记

热门文章

  1. Stephen R.Schach《软件工程 面向对象和传统的方法》总结
  2. 手机拍的试卷打印出来是黑的一片怎么办
  3. telnet对应的端口不通,防火墙关闭了也不行
  4. Windows 10关闭快速启动的方法
  5. 如何使用教育邮箱申请JetBrains全家桶License(山科大版)
  6. Leetcode题解974 能被和可被 K 整除的子数组
  7. JVM 的线程堆栈等数据分析:操千曲而后晓声、观千剑而后识器
  8. java实现单链表就地逆置,单链表的就地逆置讲解
  9. 控诉理科男(蒋方舟)
  10. 滴滴治理算法探索与实践