1、java实验报告数学与计算机学院实 验 报 告( 2010/2011 学年 第 1 学期)课程名称 Java程序设计课程代码8421991任课教师指导教师学生姓名学 号32年 级2008级专 业软件工程综合成绩实验名称类与对象指导教师夏梅宸实验类型验证综合实验学时6实验日期实验时间2010-10-172010-10-242010-10-31实验编号1分组号1实验地点6A-413一、 实验目的和要求1. 熟悉和掌握如何声明类、创建类的实例;2. 熟悉和掌握类和成员对象的修饰符的使用;3. 掌握类的方法的定义和调用;4. 掌握类的继承;掌握类的构造方法的使用。5. 掌握多态、接口、抽象类的定义和使
2、用;6. 掌握基础类的使用;7. 掌握String、StringBuffer类和Number、Arrays等类的使用;8. 要求能熟练使用开发工具,设计java的类及其应用。二、实验环境(实验设备)硬件:微型计算机设备一套软件: Windows XPJava JDK+Netbean IDE 三、实验内容设计一个小型的图书馆管理系统。假设在一个小型图书馆中有书籍,期刊,报纸,为此设计一个类层次结构,用来描述书籍,期刊,与报纸的相关信息,并编写一个设计类,验证所设计类的使用情况。如:书籍 信息包括 名称、书号、作者、出版社、价格。要求如下:1) 尽可能保证程序代码的可重用性。2) 请编写实现下列功
3、能:1. 增加;2. 更新;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
4、的方法有: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()return isbn; public String getAuthor()return author; public String getPublisher()retu
5、rn 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)=publisher; public void setPrice(double price)=price; public static String readStri
6、ng(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 st
7、atic int readInt(String s) throws IOException,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
8、 String toString()return 出版物名字:+name+ ISBN:+isbn+ 编者:+ author+ 出版社:+publisher+ 单价:+price;3 public void print()类book是如何定义为类publisher的子类的子类的定义格式 答:类Book是这样定义为类Publication的子类的:public class Book extends Publication。具体如下:public class Book extends Publication public Book()super(); public Book(String name,
9、String isbn,String author,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(作者),
10、readString(出版社),readDouble(价格); return book; public void print()。子类的定义格式为public class extends (). .4 子类可以继承超类哪些访问特性的成员答:子类可以继承超类的带有public、protected特性的成员。5 写出类book中各方法的方法头及其功能 答:Book():无参构造函数; Book(String name,String isbn,String author,String publisher,double price):带参构造函数,用于添加书时用输入的成员变量构造一个具体的book对象
11、; toString():用于将对象转化为字符串信息,便于整个对象信息的输出; addBook():用于添加Book类对象成员时进行必要的提示和输入; print():用于输出对象的具体信息,减少每次调用函数的麻烦。6 如果要将publisher类定义为一个抽象类,请写出其定义。 答:public abstract class Publication protected String name; protected String isbn; protected String publisher; protected double price; protected String author;
12、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()return isbn; public String getAuthor()return author; public String getPublisher()return pu
13、blisher; 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)=publisher; public void setPrice(double price)=price; public abstract void setInfo(Strin
14、g name,String isbn,String author,String publisher,double price);public abstract String toString();public static String readString(String s) throws IOException : ); BufferedReader buf=new BufferedReader(new InputStreamReader); return (); public static Double readDouble(String s) throws IOException,Nu
15、mberFormatException : ); BufferedReader buf=new BufferedReader(new InputStreamReader); String line=(); return (line); public static int readInt(String s) throws IOException,NumberFormatException : ); BufferedReader buf=new BufferedReader(new InputStreamReader); String line=(); return (line); public
16、abstract void print();7 book类中各方法的调用是通过什么进行的类中调用了publisher类的方法吗如果有,请写出调用语句。 答:如果Book类的对象调用其方法,就直接通过“对象名方法()”的方式调用;如果是在其他类中调用公有的方法则需要强制类型转换后再通过“对象名方法()”的方式调用;如果调用私有成员变量,则需要通过Book类中获取该私有变量的成员方法进行调用。类中有调用Publication类的方法,例如:public Book()super(); public Book(String name,String isbn,String author,String p
17、ublisher,double price) super(name,isbn,author,publisher,price); 。 8 N本书的相关信息是如何存放的请写出语句。 答:N本书的相关信息是通过Java语言中的LinkedList类创造的实例链表list实现存放的。相关语句如:public Book addBook() throws IOException Book book=new Book(readString(书名),readString(ISBN), readString(作者),readString(出版社),readDouble(价格); return book; sta
18、tic LinkedList list=new LinkedList(); Book book=new Book(); 按依次分别按以下格式添加出版物信息,当输入非0时继续添加,输入0时结束添加:); flag=1; while(flag!=0) (); 书籍信息添加成功!是否继续); flag=(); 。四、实验小结(包括问题和解决方法、心得体会、意见与建议等)1 给出这几个类的结构的UML图2.写出接口的定义格式答:接口定义格式为 public interface 接口中的内容;3.如果有下列语句 book b1=new book6;它表示什么含义其中b11还需要创建吗为什么 答:语句“b
19、ook b1=new book6;”的含义是定义一个包含6个book对象的数组b1(或者数组引用b1)。其中b11还需要创建,因为虽然定义了b1数组,但是系统并没有给其分配内存空间,还需要根据变量的类型用new方法给其分配对应的内存空间,这样才真正的创建了b11。4.通过本次实验,你有些什么收获有什么不足答:通过本实验,我进一步掌握了Java语言中类的声明及创建方法;熟悉了类和对象的修饰符作用和访问权限;掌握了类中的方法的定义和调用;熟悉了类的继承、构造方法、抽象类的定义和使用方法;掌握了多态以及Java语言中一些基础、常用类的应用方法;熟悉了Java的开发工具。不足的是对于类结构较多、较复杂
20、的程序还不是很清晰;在复杂类各层次的调用还不是很熟练;对设计多个类、类的联系较复杂、成员方法的调用较为繁复的程序设计思路不够清晰。五、指导教师评语成 绩批阅人日 期程序代码;public class Publication protected String name; protected String isbn; protected String publisher; protected double price; protected String author; protected Publication() public Publication(String name,String isb
21、n,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 getPrice()return price; public void setNam
22、e(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 : ); BufferedReader buf=new BufferedRe
23、ader(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,NumberFormatException :
24、); 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+ 编者:+ author+ 出版社:+pu
25、blisher+ 单价:+price; public void print() name:import .*;public class Book extends Publication public Book()super(); public Book(String name,String isbn,String author,String publisher,double price) super(name,isbn,author,publisher,price); public String toString()return 书 名:+name+ ISBN:+isbn+ 作者: +auth
26、or+ 出版社:+publisher+ 单价:+price; public Book addBook() throws IOException Book book=new Book(readString(书名),readString(ISBN), readString(作者),readString(出版社),readDouble(价格); return book; public void print() name:import .*;public class Periodical extends Publication protected int No; public Periodical()
27、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 No)=No; public int getNo()return No; public void setInfo(String name,String isbn,String author,String publisher,double price,int N
28、o) (name,isbn,author,publisher,price);=No; public String toString()return 期刊名称:+name+ ISBN:+isbn+ 主 编:+ author+ 发行社:+publisher+ 单价:+price+ 第+No+期; public Periodical addPeriodical() throws IOException Periodical per=new Periodical(readString(期刊名称),readString(ISBN), readString(主编),readString(发行社),read
29、Double(单价),readInt(期数); return per; public void print() name:import .*;public class Newspaper extends Publication protected String date; public Newspaper()super(); public Newspaper(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) (name,isbn,aut