下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数学与计算机学院实验报告(2010/2011课程名称课程代码任课教师指导教师学生姓名学 号 32学年第1学期)Java程序设计8421991年 级 2008级软件工程综合成绩实验名称类与对象指导教师夏梅宸实验类型今证口综合实验学时6实验日期 实验时间2010-10-172010-10-242010-10-31头驳编勺1分组号1实验地点6A-413一、实验目的和要求1 .熟悉和掌握如何声明类、创建类的实例;2 .熟悉和掌握类和成员对象的修饰符的使用;3 .掌握类的方法的定义和调用;4 .掌握类的继承;掌握类的构造方法的使用。5 .掌握多态、接口、抽象类的定义和使用;6 .掌握基础类的使用;7 .
2、掌握 String、StringBuffer 类和 Number、Arrays 等类的使用;8 .要求能熟练使用开发工具,设计java的类及其应用。二、实验环境(实验设备)硬件:微型计算机设备一套软件: Windows XPJava JDK+Netbean IDE三、实验内容设一个小型的图书馆管理系统。假设在一个小型图书馆中有书籍,期刊,报纸, 为此设一个类层次结构,用来描述书籍,期刊,与报纸的相关信息,并编写一个设计 类,验证所设计类的使用情况。如:书籍 信息包括 名血、书丛 作为 出版社、价格。要求如下:1)尽可能保证程序代码的可重用性。2)请编写实现卜刻功能:1 .增加;2 .更新;3
3、.删除;4 .查询;5 .排序;6 .打印:可以设个出版物 publisher的超类,书耒昔book,期刊journal,报纸newpaper作 为其子类。实验解答:1 .写出类publisher的变量定义答:publisher的变量定义如下: protected String name;protected String isbn;protected String publisher;protected double price;protected String author; 。2 .类publisher的方法有哪些请写出来答:类Publication的方法有:protected Publi
4、cation。public Publication(String name,String isbn,String author,String publisher,double price) =name产isbn产author产publisher产price;public String getName()return name;public String getIsbn()return isbn;public String getAuthor()return author;public String getPublisher()return publisher;public double get
5、Price()return price;public void setName(String name)=name;public void setIsbn(String isbn)=isbn;public void setAuthor(String author)=author;public void setPublisher(String publisher)=publisher;public void setPrice(double price)=price;public static String readString(String s) throws IOException:);Buf
6、feredReader buf=new BufferedReader(new InputStreamReader); return ();public static Double readDouble(String s) throws IOException,NumberFormatException :);BufferedReader buf=new BufferedReader(new InputStreamReader);String line=();return (line);public static int readInt(String s) throws IOException,
7、NumberFormatException :);BufferedReader buf=new BufferedReader(new InputStreamReader);String line=();return (line);public void setInfo(String name,String isbn,String author,String publisher,double price)=name尸isbn产author产publisher尸price;public String toString()return 出版物名字:+name+ ISBN:+isbn+ 编者:”+ a
8、uthor+ 出版社:+publisher+单价:+price;3 . public void print()类book是如何定义为类 publisher的子类的子类的定义格式答:类 Book 是这样定义为类 Publication 的子类的:public class Book extends Publication 。具体 如下:public class Book extends Publicationpublic Book()super();public Book(String name,String isbn,String author,String publisher,double p
9、rice)super(name,isbn,author,publisher,price);public String toString()return 书名:+name+ISBN:+isbn+ 作者:+author+ 出版社:+publisher+单价:+price;public Book addBook() throws IOException Book book=new Book(readString(书名),readString(ISBN), readString(作者),readString(出版社readDouble(价格);return book;public void print
10、()。 子类的定义格式为public class extends ().4 .子类可以继承超类哪些访问特性的成员答:子类可以继承超类的带有public protected特性的成员。5 .写出类book中各方法的方法头及其功能答:Book():无参构造函数;Book(String name,String isbn,String author,String publisher,double price) :带参构造函数,用于添加书时用输入的成员变量构造一个具体的book对象;toString():用于将对象转化为字符串信息,便于整个对象信息的输出;addBook():用于添加Book类对象成员时
11、进行必要的提示和输入; print():用于输出对象的具体信息,减少每次调用函数的麻烦。6 .如果要将publisher类定义为一个抽象类,请写出其定义。protected String name;protected String isbn;protected String publisher;protected double price;protected String author;protected Publication。public Publication(String name,String isbn,String author,String publisher,double pr
12、ice)=name产isbn产author产publisher产price;public String getName()return name;public String getIsbn()return isbn;public String getAuthor()return author;public String getPublisher()return publisher;public double getPrice()return price;public void setName(String name)=name;public void setIsbn(String isbn)=
13、isbn;public void setAuthor(String author)=author;public void setPublisher(String publisher)=publisher;public void setPrice(double price)=price;public abstract void setInfo(String name,String isbn,String author,String publisher,double price);public abstract String toString();public static String read
14、String(String s) throws IOException:);BufferedReader buf=new BufferedReader(new InputStreamReader); return ();public static Double readDouble(String s) throws IOException,NumberFormatException :);BufferedReader buf=new BufferedReader(new InputStreamReader);String line=();return (line);public static
15、int readInt(String s) throws IOException,NumberFormatException :);BufferedReader buf=new BufferedReader(new InputStreamReader);String line=();return (line);public abstract void print();7. book类中各方法的调用是通过什么进行的类中调用了publisher类的方法吗如果有,请写出调用语句。答:如果Book类的对象调用其方法,就直接通过“对象名加法()”的方式调用;如果是在其他类中调用公有的方法则需要强制类型转
16、换后再通过“对象名加法()”的方式调用;如果调用私有成员变量,则需要通过Book类中获取该私有变量的成员方法进行调用。类中有调用Publication 类的方法,例如: public Book()super(); public Book(String name,String isbn,String author,String publisher,double price) super(name,isbn,author,publisher,price); 。8. N本书的相关信息是如何存放的请写出语句。答:N本书的相关信息是通过Java语言中的LinkedList类创造的实例链表list实现存放
17、的。相关语句如:public Book addBook() throws IOException Book book=new Book(readString(书名),readString(ISBN), readString(作者),readString(出版社),readDouble(价格); return book; static LinkedList list=new LinkedList();Book book=new Book();按依次分别按以下格式添加出版物信息,当输入非0时继续添加,输入0时结束添加:);flag=1;while(flag!=0) ();”书籍信息添加成功!是否继
18、续);flag=();。四、实验小结(包括问题和解决方法、心得体会、意见与建议等)1 .给出这几个类的结构的UML图2 .写出接口的定义格式答:接口定义格式为public interface 接口名称接口中的内容;3 .如果有下列语句book b1=new book6;它表示什么含义其中b11还需要创建吗 为什么答:语句book b1=new book6;的含义是定义一个包含 6个book对象的数组b1 (或者数 组引用b1)。其中b11还需要创建,因为虽然定义了b1数组,但是系统并没有给其分配内存空间,还需要根据变量的类型用new方法给其分配对应的内存空间,这样才真正的创建了b11o4 .通
19、过本次实验,你有些什么收获有什么不足答:通过本实验,我进一步掌握了Java语言中类的声明及创建方法;熟悉了类和对象的修饰符作用和访问权限;掌握了类中的方法的定义和调用;熟悉了类的继承、构造方法、抽象类的定义和使用方法;掌握了多态以及Java语言中一些基础、常用类的应用方法;熟悉了 Java的开发 工具。不足的是对于类结构较多、较复杂的程序还不是很清晰;在复杂类各层次的调用还不是很熟练;设计多个类、类的联系较复杂、成员方法的调用较为繁复的程序设计思路不够清晰。五、指导教师评语成绩批阅人日期程序代码public class Publication protected String name;pro
20、tected String isbn;protected String publisher;protected double price;protected String author;protected Publication()public Publication(String name,String isbn,String author,String publisher,double price) =name;=isbn;=author;=publisher;=price;public String getName()return name;public String getIsbn()
21、return isbn;public String getAuthor()return author;public String getPublisher()return publisher;public double getPrice()return price;public void setName(String name)=name;public void setIsbn(String isbn)=isbn;public void setAuthor(String author)=author;public void setPublisher(String publisher)=publ
22、isher;public void setPrice(double price)=price;public static String readString(String s) throws IOExceptionII. IIBufferedReader buf=new BufferedReader(new InputStreamReader); return ();public static Double readDouble(String s) throws IOException,NumberFormatExceptionII. IIBufferedReader buf=new Buff
23、eredReader(new InputStreamReader);String line=();return (line);public static int readInt(String s) throws IOException,NumberFormatExceptionII. IIBufferedReader buf=new BufferedReader(new InputStreamReader);String line=();return (line);public void setInfo(String name,String isbn,String author,String
24、publisher,double price)=name;=isbn;=author;=publisher;=price;public String toString()return 出版物名字 :+name+ ISBN:+isbn+ 编者 :+ author+ 出版社 :+publisher+ 单价 :+price;public void print() name:import .*;public class Book extends Publicationpublic Book()super();public Book(String name,String isbn,String auth
25、or,String publisher,double price)super(name,isbn,author,publisher,price);public String toString()return 书 名:+name+ ISBN:+isbn+ 作者:+author+ 出版社 :+publisher+ 单价 :+price;public Book addBook() throws IOException Book book=new Book(readString(书名),readString(ISBN), readString(作者),readString(出版社),readDoubl
26、e(价格);return book;public void print() name:import .*;public class Periodical extends Publicationprotected int No;public Periodical()super();public Periodical(String name,String isbn,String author,String publisher,double price,int No)super(name,isbn,author,publisher,price);=No;public void setNo(int N
27、o)=No;public int getNo()return No;public void setInfo(String name,String isbn,String author,String publisher,double price,int No)(name,isbn,author,publisher,price);=No;public String toString()return 期刊名称 :+name+ ISBN:+isbn+ 主 编:+ author+发行社:+publisher+单价:+price+第+No+期”public Periodical addPeriodical
28、() throws IOException Periodical per=new Periodical(readString(期千U名称),readString(ISBN), readString(主编),readString(发行社),readDouble(单价),readInt(期数);return per;public void print() name:import .*;public class Newspaper extends Publication protected String date;public Newspaper()super();public Newspaper(
29、String name,String isbn,String author,String publisher,double price,String date)super(name,isbn,author,publisher,price);=date;public void setDate(String date)=date;public String getDate()return date;public void setInfo(String name,String isbn,String author,String publisher,double price,String date)(
30、name,isbn,author,publisher,price);=date;public String toString()return 报纸名称 :+name+ ISBN:+isbn+ 总主编 :+author+” 报社:+publisher+单价:+price+日期+date;public Newspaper addNewspaper() throws IOExceptionNewspaper nep=new Newspaper(readString(报纸名称),readString(ISBN), readString(总主编),readString(报社),readDouble(单价
31、),readString(日 期);return nep;public void print() name:import .*;import .*;public class Operating static LinkedList list=new LinkedList();int index,len,No;Publication pb; int ch;String name,isbn,author,publisher,date;double price;Scanner in=new Scanner;public void add() throws IOException try 请按以下选项提
32、示选择你将进行添加的出版物信息: );1. 书籍2. 期刊 3. 报纸0. 返回上一级);ch=();int flag;switch(ch)case 1:Book book=new Book();按依次分别按以下格式添加出版物信息,输 0 时结束添加: );flag=1;while(flag!=0)();书籍信息添加成功 !是否继续 );flag=();break;case 2:Periodical perd=new Periodical();按依次分别按以下格式添加出版物信息,输 0 时结束添加: );flag=1;while(flag!=0)();期刊信息添加成功 !是否继续 );flag
33、=();break;case 3:Newspaper nep=new Newspaper();按依次分别按以下格式添加出版物信息,输 0 时结束添加: );flag=1;while(flag!=0)();报纸信息添加成功 !是否继续 );flag=();0 时继续添加 ,0 时继续添加 ,0 时继续添加 ,break;catch(InputMismatchException e) public void Print() len=();for(int i=0;ilen;i+)public int search(String isbn)len=();for(int i=0;ilen;i+)if(P
34、ublication)(i).getIsbn().equals(isbn)index = i;return index;return -1;public void update(String isbn)short choice;index=search(isbn);if(index!=-1)你将要修改的出版物的原始信息如下:);(Publication)(index).print();请确认是否修改1:修改,0:不修改 );choice=();if(choice=1) 出版物名称:);name=(); 出版物ISBN:);isbn=(); 出版物作者:);author=(); 出版物出版社:);publisher=(); 出版物价格:);price=();(Publication)(index).setInfo(name,isbn,author,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年山东省春季高考《设备维修类》专业知识模拟试题(一)
- 2026年中级经济师《工商管理实务》基础试题库(名校卷)附答案详解
- 2026年磨削技术真题含答案详解(培优)
- 2026年县乡教师选调进城《教育学》-提分评估复习(突破训练)附答案详解
- 2026年证券从业之金融市场基础知识综合练习及参考答案详解1套
- 【低空经济】无人机玻璃幕墙与光伏清洗服务设计方案
- 2026学年历史八年级下学期第一单元-中华人民共和国成立和社会主义制度的建立素养提升题(含答案)
- 2026年动漫教师节幼儿园
- 2026年东西分析幼儿园
- 2025福建石狮福狮数据运营有限责任公司招聘6人笔试参考题库附带答案详解
- (行业典型)计量技术比武考试(选择题)试题库(附答案)
- 运输公司安全隐患大排查整治行动方案
- CQCC2301-2024强制性产品认证实施细则防爆电气
- 四川省拟任县处级党政领导职务政治理论水平任职资格考试题全套共12套
- 2024-2025学年河南省安阳市五中教育集团八年级下学期期中语文试题
- (新北师大版)数学八年级下册全册说课稿
- 2025年下半年江西南昌市消防救援局面向社会招聘政府专职消防队员169人考试参考试题及答案解析
- 国旗国徽国歌的含义
- 农村小型引调水工程可行性研究报告
- 邮政业务与管理考试题及答案
- 浙教版中考化学复习知识点总结
评论
0/150
提交评论