《java多媒体继承(共24页).doc》由会员分享,可在线阅读,更多相关《java多媒体继承(共24页).doc(24页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上昆 明 理 工 大 学 理 学 院信息与计算科学专业 操作性实验报告年级: 09级 姓名: 陈龙飞 学号: 1 指导教师: 朱志宁 实验课程名称: java程序设计 开课实验室: 理学院机房216 实验成绩:学风(5)观察能力(15)操作能力(30)调试能力(50)其它总分实验内容:1实验/作业题目: 面向对象程序设计1、22实验/作业课时:各2学时,共4学时 3实验过程(包括实验环境、实验内容的描述、完成实验要求的知识或技能): 实验环境:eclipse 实验内容:编写一个简单的程序,利用数组保存书(Book),cd(CompactDisk),磁带(Tape)信息,
2、并能实现插入、删除、查找功能。注意使用面向对象的思想进行程序设计。 完成实验要求的知识或技能:通过这个实验,能用eclipse来编写java程序,掌握java数组的使用和面向对象的编程思想。4程序结构(程序中的函数调用关系图。5算法描述、流程图或操作步骤:6实验数据和实验结果(用屏幕图形表示,可另加附页):1)、父类与子类定义与继承:package medea1;public class Media private String name;private String type;private float price;public String getname()return name;pub
3、lic void setname(String name)this.name = name;public String gettype()return type;public void settype(String type)this.type = type;public float getprice()return price;public void setprice(float price)this.price = price;class CompactDisk extends Mediaprivate int size;/光盘容量public int getsize()return si
4、ze;public void setsize(int size)this.size = size;public String getweight() / TODO Auto-generated method stubreturn null;class Book extends Mediaprivate int weight;/书的重量public int getweight()return weight;public void setweight(int weight)this.weight = weight;class Tape extends Mediaprivate String aut
5、or; /出厂厂家public String getautor()return autor;public void setautor(String autor)this.autor = autor;2) 、3个子类的函数定义:CompactDisk类:package medea1;public class CompactDiskMain public CompactDisk getCompactDiskByKey(CompactDisk CompactDiskb,CompactDisk searchc)for(CompactDisk compactDisk:CompactDiskb)if(co
6、mpactDisk = null)continue;if (compactDisk.getname().equals(searchc.getname() return compactDisk;if (compactDisk.gettype().equals(searchc.gettype() return compactDisk;if (compactDisk.getprice() = searchc.getprice()return compactDisk;if (compactDisk.getsize() = searchc.getsize()return compactDisk;retu
7、rn null;public boolean add(CompactDisk CompactDiskb, CompactDisk addP) for (int i = 0; i CompactDiskb.length; i+) if (CompactDiskbi = null) CompactDiskbi = addP;return true;return false;public boolean delete(CompactDisk CompactDiskb, String name) for (int i = 0; i CompactDiskb.length; i+) if (Compac
8、tDiskbi.getname().equals(name) CompactDiskbi = null;return true;return false;public Book getBookByKey(CompactDisk compactDiskb, CompactDisk searchb) / TODO Auto-generated method stubreturn null;Book类package medea1;public class BookMain public Book getBookByKey(Book Bookb,Book p)for(Book book:Bookb)i
9、f(book = null)continue;if (book.getname().equals(p.getname() return book;if (book.gettype().equals(p.gettype() return book;if (book.getprice() = p.getprice()return book;if (book.getweight() = p.getweight()return book;return null;public boolean add(Book Bookb, Book addP) for (int i = 0; i Bookb.lengt
10、h; i+) if (Bookbi = null) Bookbi = addP;return true;return false;public boolean delete(Book Bookbb, String name) for (int i = 0; i Bookbb.length; i+) if (Bookbbi.getname().equals(name) Bookbbi = null;return true;return false;Tape类package medea1;public class TapeMain public Tape getTapeByKey(Tape Tap
11、eb,Tape p)for(Tape tape:Tapeb)if(tape = null)continue;if (tape.getname().equals(p.getname() return tape;if (tape.gettype().equals(p.gettype() return tape;if (tape.getprice() = p.getprice()return tape;if (tape.getautor() = p.getautor()return tape;return null;public boolean add(Tape Tapeb, Tape addP)
12、for (int i = 0; i Tapeb.length; i+) if (Tapebi = null) Tapebi = addP;return true;return false;public boolean delete(Tape Tapeb, String name) for (int i = 0; i Tapeb.length; i+) if (Tapebi.getname().equals(name) Tapebi = null;return true;return false;3) 、主函数编写:package medea1;import java.io.IOExceptio
13、n;import java.util.Scanner;public class MediaMain private static final CompactDisk Searchc = null;private static final Tape Tapeb = null;public Media getMediabykey(Media Mediab,Media p)for(Media media:Mediab)if(media = null)continue;if (media.getname().equals(p.getname() return media;if (media.getty
14、pe().equals(p.gettype() return media;if (media.getprice() = p.getprice()return media;return null;public boolean add(Media mediab, Media addP) for (int i = 0; i mediab.length; i+) if (mediabi = null) mediabi = addP;return true;return false;public boolean delete(Media mediab, String name) for (int i =
15、 0; i mediab.length; i+) if (mediabi.getname().equals(name) mediabi = null;return true;return false;/public static void main(String args) throws IOException MediaMain test = new MediaMain();Media mediab = new Media100;int choose = -1;while(choose != 0) System.out.println(请输入对应的数字进行操作); System.out.pr
16、intln(1.对Media进行操作 2.对CompactDisk进行操作 3.对Book进行操作 4.对Tay进行操作 0 + 退出系统); System.out.println(请选择:); Scanner omm = new Scanner(System.in); choose = omm.nextInt(); / if(choose = 1)int op = -1;try do System.out.println(1.添加 2.查找 3.删除 0.退出);System.out.println(请选择:);Scanner cin=new Scanner(System.in);op =
17、cin.nextInt();switch( op ) case 0:System.out.println(系统退出!);/return; break;case 1:Media p = new Media();System.out.println(请输入播放器名称:);p.setname(cin.next();System.out.println(请输入播放器品牌:);p.settype(cin.next();System.out.println(请输入播放器价格:);p.setprice(cin.nextFloat();boolean flagadd = test.add(mediab,p);
18、if (flagadd)System.out.println(添加成功!);elseSystem.out.println(添加失败!);break;case 2:Media searchP = new Media();int search = -1;String key = ;System.out.println(请输入查询内容:1.按名称 2.按类型 3.按价格 4.列出所有产品);search = cin.nextInt();if (search = 1) System.out.println(请输入播放器名称:);searchP.setname(cin.next();Media resu
19、ltP = test.getMediabykey(mediab,searchP);if (resultP != null) System.out.println(名称: + resultP.getname() + ,类型: + resultP.gettype() + ,价格: + resultP.getprice(); else System.out.println(没有匹配播放器!); else if (search = 2) System.out.println(请输入播放器类型:);searchP.settype(cin.next();Media resultP = test.getMe
20、diabykey(mediab,searchP);if (resultP != null) System.out.println(名称: + resultP.getname() + ,类型: + resultP.gettype() + ,价格: + resultP.getprice(); else System.out.println(没有匹配播放器!); else if (search = 3) System.out.println(请输入播放器价格:);searchP.setprice(cin.nextFloat();Media resultP = test.getMediabykey(m
21、ediab,searchP);if (resultP != null) System.out.println(名称: + resultP.getname() + ,类型: + resultP.gettype() + ,价格: + resultP.getprice(); else System.out.println(没有匹配播放器!); else if (search = 4) for (Media product : mediab) if (product != null) System.out.println(名称: + product.getname() + ,类型: + product
22、.gettype() + ,价格: + product.getprice();break;case 3:System.out.println(请输入要删除的播放器名称:);String name = cin.next();boolean flagdel = test.delete(mediab, name);if (flagdel)System.out.println(删除成功!);elseSystem.out.println(删除失败!);break; while (op != 0); catch (Exception e) System.out.println(输入了无效值!系统退出!);
23、e.printStackTrace(); /else if(choose = 2)CompactDiskMain text1 = new CompactDiskMain();CompactDisk CompactDiskb = new CompactDisk100;int tp = -1;trydoSystem.out.println(1.添加 2.查找 3.删除 0.退出);System.out.println(请输入);Scanner cin = new Scanner(System.in);tp = cin.nextInt();switch(tp)case 0:System.out.pr
24、intln(系统退出);break;case 1:CompactDisk b = new CompactDisk();System.out.println(请输入光盘名称);b.setname(cin.next();System.out.println(请输入光盘的品牌);b.settype(cin.next();System.out.println(请输入光盘的价格);b.setprice(cin.nextFloat();System.out.println(请输入光盘的容量);b.setsize(cin.nextInt();boolean flagadd = text1.add(Compa
25、ctDiskb,b);if(flagadd)System.out.println(添加成功!);elseSystem.out.println(添加失败!);break;case 2:CompactDisk Searchc = new CompactDisk();int search = -1;String key1 = ;System.out.println(请输入查询内容:1.按名称 2.按类型 3.按价格 4.按容量 5.列出所有产品);search = cin.nextInt();if(search = 1)System.out.println(请输入光盘的名称:);Searchc.se
26、tname(cin.next();CompactDisk resultb = text1.getCompactDiskByKey(CompactDiskb, Searchc);if(resultb != null)System.out.println(名称: + resultb.getname() + ,品牌: + resultb.gettype() + ,价格: + resultb.getprice() + ,容量: + resultb.getsize();elseSystem.out.println(没有匹配的光盘!);else if(search = 2)System.out.print
27、ln(请输入光盘的类型:);Searchc.settype(cin.next();CompactDisk resultb = text1.getCompactDiskByKey(CompactDiskb, Searchc);if(resultb != null)System.out.println(名称: + resultb.getname() + ,品牌: + resultb.gettype() + ,价格: + resultb.getprice() + ,容量: + resultb.getsize();elseSystem.out.println(没有匹配的光盘!);else if(sea
28、rch = 3)System.out.println(请输入光盘的价格:);Searchc.setprice(cin.nextInt();CompactDisk resultb = text1.getCompactDiskByKey(CompactDiskb, Searchc);if(resultb != null)System.out.println(名称: + resultb.getname() + ,品牌: + resultb.gettype() + ,价格: + resultb.getprice() + ,容量: + resultb.getsize();elseSystem.out.p
29、rintln(没有匹配的光盘!);else if(search = 4)System.out.println(请输入光盘的容量:);Searchc.setsize(cin.nextInt();CompactDisk resultb = text1.getCompactDiskByKey(CompactDiskb, Searchc);if(resultb != null)System.out.println(名称: + resultb.getname() + ,品牌: + resultb.gettype() + ,价格: + resultb.getprice() + ,容量: + resultb
30、.getsize();elseSystem.out.println(没有匹配的光盘!);break;case 3:System.out.println(请输入要删除光盘的光盘名);String name = cin.next();boolean flagde1 = text1.delete(CompactDiskb, name);if(flagde1)System.out.println(删除成功!);elseSystem.out.println(删除失败!);break;while(tp != 0); catch (Exception e1) System.out.println(输入了无效
31、值!系统退出!);e1.printStackTrace(); /else if(choose = 3)BookMain text1 = new BookMain();Book Bookb = new Book100;int tp = -1;trydoSystem.out.println(1.添加 2.查找 3.删除 0.退出);System.out.println(请输入);Scanner cin = new Scanner(System.in);tp = cin.nextInt();switch(tp)case 0:System.out.println(系统退出);break;case 1:
32、Book b = new Book();System.out.println(请输入书名);b.setname(cin.next();System.out.println(请输入书的类别);b.settype(cin.next();System.out.println(请输入书的价格);b.setprice(cin.nextFloat();System.out.println(请输入书的重量);b.setweight(cin.nextInt();boolean flagadd = text1.add(Bookb,b);if(flagadd)System.out.println(添加成功!);e
33、lseSystem.out.println(添加失败!);break;case 2:Book Searchb = new Book();int search = -1;String key1 = ;System.out.println(请输入查询内容:1.按名称 2.按类型 3.按价格 4.按重量 5.列出所有产品);search = cin.nextInt();if(search = 1)System.out.println(请输入书的名称:);Searchb.setname(cin.next();Book resultb = text1.getBookByKey(Bookb, Search
34、b);if(resultb != null)System.out.println(名称: + resultb.getname() + ,类型: + resultb.gettype() + ,价格: + resultb.getprice() + ,重量: + resultb.getweight();elseSystem.out.println(没有匹配的书!);else if(search = 2)System.out.println(请输入书的类型:);Searchb.settype(cin.next();Book resultb = text1.getBookByKey(Bookb, Sea
35、rchb);if(resultb != null)System.out.println(名称: + resultb.getname() + ,类型: + resultb.gettype() + ,价格: + resultb.getprice() + ,重量: + resultb.getweight();elseSystem.out.println(没有匹配的书!);else if(search = 3)System.out.println(请输入书的价格:);Searchb.setprice(cin.nextInt();Book resultb = text1.getBookByKey(Bookb, Searchb);if(resultb != null)System.out.println(名称: + resultb.getname() + ,类型: + resultb.gettype() + ,价格: + resultb.getprice() + ,重量: + resultb.getweight();elseSystem.out.p
限制150内