昆明理工大学理学院
信息与计算科学专业操作性实验报告
年级: 10级姓名:刘陈学号: 2 指导教师:朱志宁
实验课程名称: java程序设计开课实验室:理学院机房
实验内容:
1.实验/作业题目: 面向对象程序设计1、2
2.实验/作业课时:各2学时,共4学时
3.实验过程(包括实验环境、实验内容的描述、完成实验要求的知识或技能):实验环境:eclipse
实验内容:编写一个简单的程序,利用数组保存书(Book),cd (CompactDisk),磁带(Tape)信息,并能实现插入、删除、查找功能。注意使用面向对象的思想进行程序设计。
完成实验要求的知识或技能:通过这个实验,能用eclipse来编写java程序,掌握java数组的使用和面向对象的编程思想。
4.程序结构(程序中的函数调用关系图)
5.算法描述、流程图或操作步骤:
程序:
父类:
package Product;
import java.io.IOException;
import java.util.Scanner;
import BookMain.BookMain;
public class Product {
private String name;
private String type;
private float price;
}
public void setName(String name) {
https://www.doczj.com/doc/9e15448046.html, = 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 Book extends Product{
private int weight;//书的重量
public int getweight(){
return weight;
}
public void setweight(int weight){
this.weight = weight;
}
}
public static void main(String[] args) throws IOException { BookMain test = new BookMain ();
Product []productdb = new Product[100];
int op = -1;
try {
do {
System.out.println("1.添加 2.查找 3.删除 0.退出");
System.out.println("请选择:");
Scanner cin=new Scanner(System.in);
op = cin.nextInt();
switch( op ) {
case 0:
case 1:
Product p = new Product();
System.out.println("请输入产品名称:");
p.setName(cin.next());
System.out.println("请输入产品类型:");
p.setType(cin.next());
System.out.println("请输入产品价格:");
p.setPrice(cin.nextFloat());
System.out.println("请输入书的质量:");
p.setPrice(cin.nextFloat());
boolean flagadd = test.add(productdb,p);
if (flagadd)
System.out.println("添加成功!");
else
System.out.println("添加失败!");
break;
case 2:
Product 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.println("名称:" + resultP.getName()
+ ",类型:" + resultP.getType()
+ ",价格:" + resultP.getPrice());
} else {
System.out.println("没有匹配产品!");
}
} else if (search == 2) {
System.out.println("请输入产品类型:");
searchP.setType(cin.next());
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());
Product resultP = test.getProductByKey(productdb,searchP); if (resultP != null) {
System.out.println("名称:" + resultP.getName()
+ ",类型:" + resultP.getType()
+ ",价格:" + resultP.getPrice());
} else {
System.out.println("没有匹配产品!");
}
} else if (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();
boolean flagdel = test.delete(productdb, name);
if (flagdel)
System.out.println("删除成功!");
else
System.out.println("删除失败!");
break;
}
} while (op != 0);
} catch (Exception e) {
System.out.println("输入了无效值!系统退出!");
e.printStackTrace();
}
}
}
//子类继承测试类
package BookMain;
import Product.Product;
public class BookMain {
public Product getProductByKey(Product []productdb,Product p) { for (Product product : productdb) {
if (product == null) {
continue;
}
if (product.getName().equals(p.getName())) {
return product;
}
if (product.getType().equals(p.getType())) {
return product;
}
if (product.getPrice() == p.getPrice()) {
return product;
}
}
return null;
}
public boolean add(Product []productdb, Product addP) {
for (int i = 0; i < productdb.length; i++) {
if (productdb[i] == null) {
productdb[i] = addP;
return true;
}
}
return false;
}
public boolean delete(Product []productdb, String name) {
for (int i = 0; i < productdb.length; i++) {
if (productdb[i].getName().equals(name)) {
productdb[i] = null;
return true;
}
}
return false;
}
}
6.实验数据和实验结果(用屏幕图形表示,可另加附页):
程序运行结果:
7.改进建议:改进源码,实现子类的更多功能。
操作能力--正确输入程序,熟悉编程环境调试能力--熟练使用调试功能解决程序错误