1、实 验 报 告数学与计算机学院实 验 报 告( 2010/2011 学年 第 1 学期)课程名称 Java程序设计课程代码8421991任课教师指导教师学生姓名学 号312008080611322年 级2008级专 业软件工程综合成绩2实验名称类与对象指导教师夏梅宸实验类型验证综合实验学时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 6.5三、实验内容设计一个小型的图书馆管理系统。假设在一个小型图书馆中有书籍,期刊,报纸,为此设计一个类层次结构,用来描述书籍,期刊,与报纸的相关信息,并编写一个设计类,验证所设计类的使用情况。如:书籍 信息包括 名称、书号、作者、出版社、价格。要求如下:1) 尽可能保证程序代码的
3、可重用性。2) 请编写实现下列功能: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的方法有哪些?请写
4、出来.答:类Publication的方法有:protected Publication() public Publication(String name,String isbn,String author,String publisher,double price) this.name=name;this.isbn=isbn;this.author=author;this.publisher=publisher;this.price=price; public String getName()return name; public String getIsbn()return isbn; pu
5、blic String getAuthor()return author; public String getPublisher()return publisher; public double getPrice()return price; public void setName(String name)this.name=name; public void setIsbn(String isbn)this.isbn=isbn; public void setAuthor(String author)this.author=author; public void setPublisher(S
6、tring publisher)this.publisher=publisher; public void setPrice(double price)this.price=price; public static String readString(String s) throws IOException System.out.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); return buf.readLine(); public static Double readD
7、ouble(String s) throws IOException,NumberFormatException System.out.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); String line=buf.readLine(); return Double.parseDouble(line); public static int readInt(String s) throws IOException,NumberFormatException System.ou
8、t.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); String line=buf.readLine(); return Integer.parseInt(line); public void setInfo(String name,String isbn,String author,String publisher,double price) this.name=name;this.isbn=isbn;this.author=author;this.publisher=p
9、ublisher;this.price=price; public String toString()return 出版物名字:+name+ ISBN:+isbn+ 编者:+ author+ 出版社:+publisher+ 单价:+price; public void print()System.out.println(this.toString();3 类book是如何定义为类publisher的子类的?子类的定义格式? 答:类Book是这样定义为类Publication的子类的:public class Book extends Publication。具体如下:public class
10、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+ 作者: +author+ 出版社:+publisher+ 单价:+price; public Book addBook() throws IOExcept
11、ion Book book=new Book(readString(书名),readString(ISBN), readString(作者),readString(出版社),readDouble(价格); return book; public void print()System.out.println(this.toString();。子类的定义格式为public class extends (). .4 子类可以继承超类哪些访问特性的成员?答:子类可以继承超类的带有public、protected特性的成员。5 写出类book中各方法的方法头及其功能? 答:Book():无参构造函数;
12、Book(String name,String isbn,String author,String publisher,double price):带参构造函数,用于添加书时用输入的成员变量构造一个具体的book对象; toString():用于将对象转化为字符串信息,便于整个对象信息的输出; addBook():用于添加Book类对象成员时进行必要的提示和输入; print():用于输出对象的具体信息,减少每次调用System.out.println()函数的麻烦。6 如果要将publisher类定义为一个抽象类,请写出其定义。 答:public abstract class Publica
13、tion 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 price) this.name=name;this.isbn=isbn;this.author=author;this.publis
14、her=publisher;this.price=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)this.name=name; public void setIsbn(Stri
15、ng isbn)this.isbn=isbn; public void setAuthor(String author)this.author=author; public void setPublisher(String publisher)this.publisher=publisher; public void setPrice(double price)this.price=price; public abstract void setInfo(String name,String isbn,String author,String publisher,double price);pu
16、blic abstract String toString();public static String readString(String s) throws IOException System.out.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); return buf.readLine(); public static Double readDouble(String s) throws IOException,NumberFormatException Syste
17、m.out.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); String line=buf.readLine(); return Double.parseDouble(line); public static int readInt(String s) throws IOException,NumberFormatException System.out.print(s+: ); BufferedReader buf=new BufferedReader(new Input
18、StreamReader(System.in); String line=buf.readLine(); return Integer.parseInt(line); public abstract void print();7 book类中各方法的调用是通过什么进行的?类中调用了publisher类的方法吗?如果有,请写出调用语句。 答:如果Book类的对象调用其方法,就直接通过“对象名方法()”的方式调用;如果是在其他类中调用公有的方法则需要强制类型转换后再通过“对象名方法()”的方式调用;如果调用私有成员变量,则需要通过Book类中获取该私有变量的成员方法进行调用。类中有调用Public
19、ation类的方法,例如: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实现存放的。相关语句如:public Book addBook() throws IOException Book book=new Book(re
20、adString(书名),readString(ISBN), readString(作者),readString(出版社),readDouble(价格); return book; static LinkedList list=new LinkedList(); Book book=new Book(); System.out.println(按依次分别按以下格式添加出版物信息,当输入非0时继续添加,输入0时结束添加:); flag=1; while(flag!=0) list.add(book.addBook(); System.out.println(书籍信息添加成功!是否继续?); fl
21、ag=in.nextInt(); 。四、实验小结(包括问题和解决方法、心得体会、意见与建议等)1 给出这几个类的结构的UML图PublicationNewspaperBookPeriodicalOperationTestMiniBMS2.写出接口的定义格式答:接口定义格式为 public interface 接口中的内容;3.如果有下列语句 book b1=new book6;它表示什么含义?其中b11还需要创建吗?为什么? 答:语句“book b1=new book6;”的含义是定义一个包含6个book对象的数组b1(或者数组引用b1)。其中b11还需要创建,因为虽然定义了b1数组,但是系统
22、并没有给其分配内存空间,还需要根据变量的类型用new方法给其分配对应的内存空间,这样才真正的创建了b11。4.通过本次实验,你有些什么收获?有什么不足?答:通过本实验,我进一步掌握了Java语言中类的声明及创建方法;熟悉了类和对象的修饰符作用和访问权限;掌握了类中的方法的定义和调用;熟悉了类的继承、构造方法、抽象类的定义和使用方法;掌握了多态以及Java语言中一些基础、常用类的应用方法;熟悉了Java的开发工具。不足的是对于类结构较多、较复杂的程序还不是很清晰;在复杂类各层次的调用还不是很熟练;对设计多个类、类的联系较复杂、成员方法的调用较为繁复的程序设计思路不够清晰。五、指导教师评语成 绩批
23、阅人日 期程序代码/file name:Publication.javaimport java.io.*;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 isbn,String author,String publisher,d
24、ouble price) this.name=name;this.isbn=isbn;this.author=author;this.publisher=publisher;this.price=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;
25、 public void setName(String name)this.name=name; public void setIsbn(String isbn)this.isbn=isbn; public void setAuthor(String author)this.author=author; public void setPublisher(String publisher)this.publisher=publisher; public void setPrice(double price)this.price=price; public static String readSt
26、ring(String s) throws IOException System.out.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); return buf.readLine(); public static Double readDouble(String s) throws IOException,NumberFormatException System.out.print(s+: ); BufferedReader buf=new BufferedReader(ne
27、w InputStreamReader(System.in); String line=buf.readLine(); return Double.parseDouble(line); public static int readInt(String s) throws IOException,NumberFormatException System.out.print(s+: ); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in); String line=buf.readLine(); return
28、 Integer.parseInt(line); public void setInfo(String name,String isbn,String author,String publisher,double price) this.name=name;this.isbn=isbn;this.author=author;this.publisher=publisher;this.price=price; public String toString()return 出版物名字:+name+ ISBN:+isbn+ 编者:+ author+ 出版社:+publisher+ 单价:+price
29、; public void print()System.out.println(this.toString();/file name:Book.javaimport java.io.*;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()r
30、eturn 书 名:+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()System.out.println(this.toString();/file name:Periodical.ja
31、vaimport java.io.*;public class Periodical extends Publication protected 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);this.No=No; public void setNo(int No)this.No=No; public i
32、nt getNo()return No; public void setInfo(String name,String isbn,String author,String publisher,double price,int No) super.setInfo(name,isbn,author,publisher,price);this.No=No; public String toString()return 期刊名称:+name+ ISBN:+isbn+ 主 编:+ author+ 发行社:+publisher+ 单价:+price+ 第+No+期; public Periodical a
33、ddPeriodical() throws IOException Periodical per=new Periodical(readString(期刊名称),readString(ISBN), readString(主编),readString(发行社),readDouble(单价),readInt(期数); return per; public void print()System.out.println(this.toString();/file name:Newspaper.javaimport java.io.*;public class Newspaper extends Pub
34、lication 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);this.date=date; public void setDate(String date)this.date=date; public String getDate()return date; pub
35、lic void setInfo(String name,String isbn,String author,String publisher,double price,String date) super.setInfo(name,isbn,author,publisher,price);this.date=date; public String toString()return 报纸名称:+name+ ISBN:+isbn+ 总主编:+ author+ 报 社:+publisher+ 单价:+price+ 日期+date; public Newspaper addNewspaper() t
36、hrows IOException Newspaper nep=new Newspaper(readString(报纸名称),readString(ISBN), readString(总主编),readString(报社),readDouble(单价),readString(日期); return nep; public void print()System.out.println(this.toString();/file name:Operating.javaimport java.util.*;import java.io.*;public class Operating static
37、LinkedList list=new LinkedList(); int index,len,No; Publication pb; int ch; String name,isbn,author,publisher,date; double price; Scanner in=new Scanner(System.in); public void add() throws IOException try System.out.println(请按以下选项提示选择你将进行添加的出版物信息:); System.out.println(1. 书籍 2. 期刊 3. 报纸 0. 返回上一级); c
38、h=in.nextInt(); int flag; switch(ch) case 1: Book book=new Book(); System.out.println(按依次分别按以下格式添加出版物信息,当输入非0时继续添加,输入0时结束添加:); flag=1; while(flag!=0) list.add(book.addBook(); System.out.println(书籍信息添加成功!是否继续?); flag=in.nextInt(); break; case 2: Periodical perd=new Periodical(); System.out.println(按依
39、次分别按以下格式添加出版物信息,当输入非0时继续添加,输入0时结束添加:); flag=1; while(flag!=0) list.add(perd.addPeriodical(); System.out.println(期刊信息添加成功!是否继续?); flag=in.nextInt(); break; case 3: Newspaper nep=new Newspaper(); System.out.println(按依次分别按以下格式添加出版物信息,当输入非0时继续添加,输入0时结束添加:); flag=1; while(flag!=0) list.add(nep.addNewspaper();