java上机实验实验2

上传人:re****.1 文档编号:395662766 上传时间:2022-08-12 格式:DOCX 页数:9 大小:126.65KB
返回 下载 相关 举报
java上机实验实验2_第1页
第1页 / 共9页
java上机实验实验2_第2页
第2页 / 共9页
java上机实验实验2_第3页
第3页 / 共9页
java上机实验实验2_第4页
第4页 / 共9页
java上机实验实验2_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《java上机实验实验2》由会员分享,可在线阅读,更多相关《java上机实验实验2(9页珍藏版)》请在金锄头文库上搜索。

1、昆明理工大学理学院信息与计算科学专业操作性实验报告年级: 10 级姓名:刘陈学号: 2指导教师:宁实验课程名称:java程序设计开课实验室:理学院机房实验成绩:学风 (5)观察能力 (15)操作能力 (30)调试能力 (50)其它朱志总分实验内容:1 实验 / 作业题目 : 面向对象程序设计1 、22 实验 / 作业课时: 各 2 学时,共 4 学时3 实验过程 (包括实验环境、实验内容的描述、完成实验要求的知识或技能):实验环境: eclipse实验 内容:编写一个简单的程序 ,利 用数组保存书( Book ), cd( CompactDisk ),磁带( Tape )信息,并能实现插入、删

2、除、查找功能。注意使用面向对象的思想进行程序设计。完成实验要求的知识或技能: 通过这个实验, 能用 eclipse 来编写 java 程序,掌握 java 数组的使用和面向对象的编程思想。Media-父类+insert()+delete()+update()CompactDiskBookTape-cd-book-tape+insert()+insert()+insert()+delete()+delete()+delete()+update()+update()+update()+.()+.()+.()4 程序结构 ( 程序中的函数调用关系图)5 算法描述、流程图或操作步骤:程序:父类:pac

3、kageProduct;importimportimportBookMain.BookMain;publicclassProduct privateStringname ;privateStringtype;privatefloatprice;publicString getName() returnname ;publicvoidsetName(String name) this. name = name;publicString getType() returntype;publicvoidsetType(String type) this. type = type;publicfloat

4、getPrice() returnprice;publicvoidsetPrice(floatprice) this. price= price;/ 子类继承classBookextendsProductprivateintweight ; /书的重量publicintgetweight()returnweight;publicvoidsetweight(int weight)this. weight= weight;publicstaticvoidmain(String args)throwsIOException BookMain test =new BookMain ();Product

5、 productdb =new Product100;intop = -1;trydoSystem.out.println(1.添加 2.查找 3.删除 0.退出);System.out.println( 请选择: );Scanner cin=new Scanner(System.in );op = cin.nextInt();switch ( op ) case 0:System.out.println( 系统退出! );return;case1:Product p =new Product();System.out.println( 请输入产品名称: );p.setName(cin.nex

6、t();System.out.println( 请输入产品类型: );p.setType(cin.next();System.out.println( 请输入产品价格: );p.setPrice(cin.nextFloat();System.out.println( 请输入书的质量: );p.setPrice(cin.nextFloat();booleanflagadd = test.add(productdb,p);if (flagadd)System.out.println( 添加成功! );elseSystem.out.println( 添加失败! );break;case2:Produ

7、ct searchP =new Product();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();Product resultP = test.getProductByKey(productdb,searchP);if (resultP !=null ) System.out

8、.println( 名称: + resultP.getName()+ , 类型: + resultP.getType()+ , 价格: + resultP.getPrice(); else System.out.println( 没有匹配产品! ); elseif(search = 2) System.out.println( 请输入产品类型: );searchP.setType(cin.next();Product resultP = test.getProductByKey(productdb,searchP);if(resultP !=null) System.out .println(

9、 名称: + resultP.getName()+ , 类型: + resultP.getType()+ , 价格: + resultP.getPrice(); else System.out.println( 没有匹配产品! ); elseif(search = 3) System.out.println( 请输入产品价格: );searchP.setPrice(cin.nextFloat();Product resultP = test.getProductByKey(productdb,searchP);if(resultP !=null) System.out .println( 名称

10、: + resultP.getName()+ , 类型: + resultP.getType()+ , 价格: + resultP.getPrice(); else System.out.println( 没有匹配产品! );elseif (search = 4) for(Product product : productdb) if(product !=null) System.out.println( 名称: + product.getName()+ , 类型: + product.getType()+ , 价格: + product.getPrice();break ;case 3:System.out.println( 请输入要删除的产品名称: );String name = cin.next();booleanflagdel = test.delete(productdb, name);if (flagdel)System.out.println( 删除成功! );elseSystem.out.println( 删除失败! );break ; while(op != 0);catch(Exception e) System.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号