




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
四、编程(共 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) = name;public String getCatalog() return catalog;public void setCatalog(String catalog) this.catalog = catalog;public Capture() = Red Algae;this.catalog = Algae;public String toString() return name= + name + tcatalog= + catalog;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 表示外观:import java.io.IOException;import java.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() return super.toString() +tweight= + weight + tcolor= + color;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(Blue Aglae);System.out.println(a.print();3、设计上题 2 中的 Seaweed 类对象数据存储方法: (1)设计 Comparator 接口类的子类 MyComparator,实现对 Seaweed 类对象的比较,比较的规 则是 name 的字典顺序(升或降)(其他代码不需要列出); import java.util.Comparator;public class MyComparator implements Comparator public int 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 类 import java.util.Iterator;import java.util.TreeSet;public class TreeSetDemo public static void main(String args) TreeSet seaweed = new TreeSet(new MyComparator();Seaweed s1 = new Seaweed();s1.setName(Red);Seaweed s2 = new Seaweed();s2.setName(Blue);Seaweed s3 = new Seaweed();s3.setName(Yellow);Seaweed s4 = new Seaweed();s4.setName(Black);seaweed.add(s1);seaweed.add(s2);seaweed.add(s3);seaweed.add(s4);Iterator it = seaweed.iterator();while(it.hasNext() Seaweed temp = it.next();System.out.println(temp.print();4、设应用程序中有一个数据录入界面,如下图所示。程序中有一个上题 3 中 seaweedSet 类对 象 set 属性,保存用户输入的所有 Seaweed 类信息。试完成: (1)当用户单击“添加(Append)”按钮时,创建 Seaweed 类的对象 seaweed,添加的数据插 入到图中的表格中,并将相关信息存入该对象,将其添加到 set 对象中; JButton btnNewButton = new JButton(Append);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 table.getRowCount() table.setValueAt(name, appendIndex, 0);table.setValueAt(catalog, appendIndex, 1);table.setValueAt(weight, appendIndex, 2);table.setValueAt(color, appendIndex, 3);appendIndex +;seaweedSet.add(newsea););btnNewButton.setBounds(179, 288, 93, 23);contentPane.add(btnNewButton);(2)当用户单击“关闭(Close)”按钮时,将 set 中的 Seaweed 类的对象信息通过控制台显示 出来。请完成“添加(Append)”、“关闭(Close)”按钮单击事件。JButton btnNewButton_1 = new JButton(Close);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(ObjectOutputStream outObj) throws IOException private void readObject(ObjectInputStream inObj) throws IOException 来实现。import java.io.ObjectInputStream;import java.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(data.dat);ObjectInputStream ois = null;try ois = new ObjectInputStream(new FileInputStream(f);while(true) seaweedSet.add(Seaweed)ois.readObject(); catch (FileNotFoundException e) try ObjectOutputStream oos = 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(data.dat); try ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f);Iterator it = seaweedSet.iterator();while(it.hasNext() Seaweed temp = it.next();oos.writeObject(temp);oos.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); 6、修改上题4,实现数据库来实现持久化,数据库系统为MySQL,数据库为test,表名为 seaweed, 表中的字段与类的属性相同。写出实现 Seaweed 对象数据的增、删、改、查,即实现以下方法: boolean add(Seaweed seaweed); boolean delete(Seaweed seaweed); boolean Update(Seaweed seaweed); Seaweed list(String condition); (condition 是查询条件) public boolean add(Seaweed seaweed) Connection conn = null; Statement st = null; try Class.forName(com.mysql.jdbc.Driver);conn = DriverManager.getConnection(jdbc:mysql:/localhosh:3306/test , root , );st = conn.createStatement();String sql = INSERT INTO seaweed VALUES(+seaweed.getName() + , + seaweed.getCatalog() + , + seaweed.getWeight() + , + seaweed.getColor() + ); ;int effectedRows = st.executeUpdate(sql);if(effectedRows 0 ) return true;elsereturn false; catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();return false; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return false;finallytry st.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); public boolean delete(Seaweed seaweed) Connection conn = null; Statement st = null; try Class.forName(com.mysql.jdbc.Driver);conn = DriverManager.getConnection(jdbc:mysql:/localhost:3306/test?user=root&password=);st = conn.createStatement();String sql = DELETE FROM seaweed WHERE name= + seaweed.getName() + ; ;int rows = st.executeUpdate(sql);if(rows 0 )return true;elsereturn false; catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();return false; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return false;finallytry st.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); public boolean Update(Seaweed seaweed) Connection conn = null; Statement st = null; try Class.forName(com.mysql.jdbc.Driver);conn = DriverManager.getConnection(jdbc:mysql:/localhost:3306/test , root , );st = conn.createStatement();String sql = UPDATE seaweed SET weight = + seaweed.getWeight() + WHERE name = +seaweed.getName()+;int rows = st.executeUpdate(sql);if(rows 0 )return true;elsereturn false; catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();return false; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return false;finallytry st.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); public Seaweed list(String condition) Connection conn = null; ResultSet rs = null; Statement st = null; try Cla
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 梁弯曲时的强度计算
- 2025年麻醉科临床麻醉应急处理模拟测试卷答案及解析
- 民族复兴是我的责任课件
- 2025年肝脏疾病乙肝病毒感染的防治与护理模拟考试卷答案及解析
- 民族团结课件教育
- 民族团结花课件
- 2025年普外科急性胃粘膜损伤的处理模拟考试卷答案及解析
- 2025年精神科护理技巧与团体治疗模拟测试卷答案及解析
- 2025年精神科常见病例诊疗技术考核答案及解析
- 2025年放射电影学临床诊断综合考试答案及解析
- 酒店实美学 课件全套 杨卉 第1-13章 酒店美学概述-酒店服务之美
- 2024年秋季新人教版九年级上册化学全册教案
- 液碱卸车安全操作规程
- 甲醇含量测定方法
- 建筑用砂石料采购 投标方案(技术方案)
- 中华护理学会成人肠内营养支持护理团标解读
- 医疗器械质量安全风险会商管理制度
- Unit2-The-fun-they-had市公开课一等奖省赛课微课金奖课件
- 110kV变电站及110kV输电线路运维投标技术方案(第一部分)
- 确保工期的资源保障措施
- 项目时间安排
评论
0/150
提交评论