样卷答案4
更新时间:2023-10-11 13:41:01 阅读量: 综合文库 文档下载
四、编程(共 60 分)
1、设生物信息管理系统中,需要定义一个实体类 Capture 表示生物类,要求如下: (1)具有属性生物名称 name、生物的分类 Catalog; (2)属性的 setter/geter方法;
(3)构造方法,默认的构造方法将生物初始化为“赤藻(Red Algae),藻类(Algae)”; (4)重写toString()方法;编写测试类TextCapture,测试该类。
public class Capture { private String name; private String catalog; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCatalog() { return catalog; } public void setCatalog(String catalog) { this.catalog = catalog; } public Capture() { this.name = \ this.catalog = \ } public String toString() { return \ } }
2、设计一个接口类Processable,该接口中一个方法 String print(),输出生物的基本信息;海参类 Seaweed 继承 Capture 类并实现接口Processable,新的属性 weight 表示重量、color 表示外观,编写测试类TestSeaweed,测试类 Seaweed。
//设计一个接口类Processable,该接口中一个方法 String print(),输出生物的基本信息: public interface Processable { public String print(); }
//海参类 Seaweed 继承 Capture 类并实现接口Processable,新的属性 weight 表示重量、color 表示外观:
importjava.io.IOException; importjava.io.Serializable;
//import java.io.ObjectInputStream; //import java.io.ObjectOutputStream;
public class Seaweed extends Capture implements Processable , Serializable { private float weight; private String color; public String print() { returnsuper.toString() +\ } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
//编写测试类TestSeaweed,测试类Seaweed: public class TestSeaweed { public static void main(String[] args) { Seaweed a = new Seaweed(); a.setName(\ System.out.println(a.print()); } }
3、设计上题 2 中的 Seaweed 类对象数据存储方法:
(1)设计 Comparator 接口类的子类MyComparator,实现对 Seaweed 类对象的比较,比较的规则是 name 的字典顺序(升或降)(其他代码不需要列出);
importjava.util.Comparator;
public class MyComparator implements Comparator
publicint compare(Seaweed o1, Seaweed o2) { return o1.getName().compareTo(o2.getName()); } }
(2)创建 TreeSetDemo.java,在 main()方法中用MyComparator为参数,创建一个SortedSet接口实现类TreeSet的对象seaweedSet;
(3)创建 4 个 Seaweed 类对象,并以存入seaweedSet; 运行结果: (4)使用迭代器,从seaweedSet检索出所有 Seaweed 类对象,调用 Seaweed 类对象中的 print 方法,输出按 name 排序后的信息; [其他数据结构的使用:分别用 Vector、LinkedList、ArrayList、HashSet、HashMap、TreeSet、TreeMap类]
importjava.util.Iterator; importjava.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { TreeSet
4、设应用程序中有一个数据录入界面,如下图所示。程序中有一个上题 3 中seaweedSet类对象 set 属性,保存用户输入的所有 Seaweed 类信息。试完成: (1)当用户单击“添加(Append)”按钮时,创建 Seaweed 类的对象 seaweed,添加的数据插入到图中的表格中,并将相关信息存入该对象,将其添加到 set 对象中;
JButtonbtnNewButton = new JButton(\ btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Seaweed newsea = new Seaweed(); String name = textField_name.getText(); String catalog = textField_catalog.getText(); float weight = Float.parseFloat(textField_weight.getText()); String color = comboBox_color.getSelectedItem().toString(); newsea.setName(name); newsea.setCatalog(catalog); newsea.setWeight(weight); newsea.setColor(color); if(appendIndex
(2)当用户单击“关闭(Close)”按钮时,将 set 中的 Seaweed 类的对象信息通过控制台显示出来。请完成“添加(Append)”、“关闭(Close)”按钮单击事件。
JButton btnNewButton_1 = new JButton(\ btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Seaweed newsea = new Seaweed(); String name = textField_name.getText(); String catalog = textField_catalog.getText(); float weight = Float.parseFloat(textField_weight.getText()); String color = comboBox_color.getSelectedItem().toString(); newsea.setName(name); newsea.setCatalog(catalog); newsea.setWeight(weight);
newsea.setColor(color); System.out.println(newsea.print()); saveData(); } }); btnNewButton_1.setBounds(282, 288, 93, 23); contentPane.add(btnNewButton_1);
(3)简述 JAVA 的事件对象模型、常用的事件监听器接口。如何注册事件监听器?
5、修改上题 4,实现数据的持久化(用文件实现)。当数据输入窗口初始化时,将保存在文件data.dat 中的对象数据读入到seaweedSet对象中(在构造方法中);当单击“关闭(Close)”按钮时,将seaweedSet中保存的信息写入到文件 data.dat 中。[注:通过 Seaweed 类实现Serializable接口中的以下接口方法:
private void writeObject(ObjectOutputStreamoutObj) throws IOException
private void readObject(ObjectInputStreaminObj) throws IOException来实现。]
importjava.io.ObjectInputStream; importjava.io.ObjectOutputStream; public void readObject(ObjectInputStream in) { try { in.readUTF(); in.readUTF(); in.readFloat(); in.readUTF(); } catch (IOException e) { e.printStackTrace(); } } public void writeObject(ObjectOutputStream out) { try { out.writeUTF(this.getName()); out.writeUTF(this.getCatalog()); out.writeFloat(this.getWeight()); out.writeUTF(this.getColor()); } catch (IOException e) { e.printStackTrace(); } }
private void readData() { File f = new File(\ ObjectInputStreamois = null; try { ois = new ObjectInputStream(new FileInputStream(f)); while(true) { seaweedSet.add((Seaweed)ois.readObject()); } } catch (FileNotFoundException e) { try { ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream(f)); oos.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (IOException e) { try { ois.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } private void saveData() { File f = new File(\ try { ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream(f)); Iterator
} } 6、修改上题4,实现数据库来实现持久化,数据库系统为MySQL,数据库为test,表名为 seaweed,表中的字段与类的属性相同。写出实现 Seaweed 对象数据的增、删、改、查,即实现以下方法:boolean add(Seaweed seaweed); boolean delete(Seaweed seaweed); boolean Update(Seaweed seaweed); Seaweed [] list(String condition); (condition 是查询条件)
publicboolean add(Seaweed seaweed) {
Connection conn = null; Statement st = null; try {
Class.forName(\
conn = DriverManager.getConnection(\
\
st = conn.createStatement();
String sql = \
seaweed.getCatalog() + \
+ seaweed.getWeight() + \
seaweed.getColor() + \
inteffectedRows = st.executeUpdate(sql); if(effectedRows> 0 ) else
return false; return true;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace(); return false;
} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace(); return false;
}finally{
}
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace();
}finally{ }
try {
conn.close();
} catch (SQLException e) { }
// TODO Auto-generated catch block e.printStackTrace();
}
publicboolean delete(Seaweed seaweed) {
Connection conn = null; Statement st = null; try {
Class.forName(\conn =
DriverManager.getConnection(\ \
}
publicboolean Update(Seaweed seaweed) {
int rows = st.executeUpdate(sql);
if(rows > 0 ) else
return false; return true;
st = conn.createStatement();
String sql = \
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace(); return false;
} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace(); return false;
}finally{ }
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace();
}finally{ }
try {
conn.close();
} catch (SQLException e) { }
// TODO Auto-generated catch block e.printStackTrace();
正在阅读:
样卷答案410-11
新课标人教版四年级语文下册 6 万年牢 教学设计 板书设计及教学反思07-23
2008年全国新课标数学高考试卷+答案04-27
下大雨作文450字07-07
软件工程课程设计学生信息管理系统12-16
中国彩棉行业商业模式专项分析与企业投资环境研究报告01-05
电大网上作业思政课程10-21
爬山虎的脚优秀教学设计08-27
英语论文(威尼斯商人人物分析)09-15
非标设备槽罐制作安装方案04-17
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 答案
- 11会计成本会计B卷
- 视频中物体检测方法研究
- 计算机组成原理 - 教材习题解答 - (1)
- 冷轧带钢生产工艺及发展状况
- 学校家长会实施方案
- 带看前针对业主铺垫的话术
- 师训工作总结
- 尔雅中国哲学课后题
- (部编版)2020高中数学3.3三角函数的积化和差与和差化积同步训练新人教B版必修1
- 2018年二级健康管理师试题及答案十套汇总
- 廉洁文化进校园教案(修改版) - 蔡艳
- 苏教版小学五年级数学教案
- XX区XX小学工会创先争优活动实施方案
- 英语口语话题素材
- 体育舞蹈概论 - 图文
- 最新精品教师招聘考试中学数学教材教法精选试题及答案
- 上海市浦东区2016学年高二第二学期教学质量检测地理试卷
- 2018届高考化学一轮复习 铝及其重要化合物练习
- 城市热岛效应 - 图文
- 广告创意的原则