样卷答案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 seaweed = new TreeSet(new MyComparator()); Seaweed s1 = new Seaweed(); s1.setName(\ Seaweed s2 = new Seaweed(); s2.setName(\ Seaweed s3 = new Seaweed(); s3.setName(\ Seaweed s4 = new Seaweed(); s4.setName(\ 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 对象中;

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 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 是查询条件)

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();

本文来源:https://www.bwwdw.com/article/lsnf.html

Top