样卷答案4
- 格式:docx
- 大小:24.30 KB
- 文档页数:8
四、编程(共 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 = "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 表示外观: 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() +"\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 的字典顺序(升或降)(其他代码不需要列出);
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("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 对象中;
JButtonbtnNewButton = 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(appendIndextable.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);