某大学化学实验室里, 科学家进行铜离子的高分子聚合物的研究. 在实验室合成了10种聚合物,  名字和分子量分别是 CuPdt1 分子量542, CuPdt2 分子量 347, CuPdt3 分子量 239, CuPdt4 分子量 478, CuPdt5 分子量783, CuPdt6 分子量 672, CuPdt7 分子量 429, CuPdt8 分子量235. 
1. 建立铜聚合物类 class CuPolymer. 这个类需要含有聚合物的名字和分子量两个数据, 
其构造函数为CuPolymer(String polymerName, int weight). 该类实现Compareble接口(提示, 实现CompareTo()函数使其能按照分子量大小对CuPolymer类的对象进行比较). 
CuPolymer有一个方法printPolymer(), 可以打印出聚合物的名称和分子量. 2. 建立PolymerList类, 这个类中含有一个长度为20的CuPolymer类的数组,
编写并使用addPolymer(CuPolymer p)可以将聚合物依次插入进这个数组, 
在主函数建立该类对象, 插入这8种聚合物, 然后依次打印出来. 3.  科学家发现了成功合成了两种新的超大分子量聚合物, 
分别是 CuPdC, 分子量1290, 和CuPdN, 分子量 1572, 
试在PolymerList类里编写一个方法public void insertFromHead(CuPolymer polymer), 
并在主函数里将这两种新的聚合物利用该方法插入polymerList对象的前部, 并打印出数组里所有聚合物的名字及其分子量. 4. 请选择一种排序算法, 这个算法可以使用CompareTo()函数将聚合物按照分子量进行排序(从大到小, 从小到大均可), 并在程序的注释里简述该函数的时间复杂度(提示, 注释语句以//开头). 
在主函数里将聚合物排序后, 依次打印出排序之后的聚合物名称及其分子量. 5. 请找出分子量是429的聚合物, 并打印输出名称和分子量. 
如果选用效率较高的算法, 将获得更高分数, 在注释里简述这个算法的时间复杂度. 

解决方案 »

  1.   


    public class CuPolymer {
    private String polymerName;
    private  int weight;public CuPolymer(){
    }public CuPolymer(String polymerName, int weight){
    this.polymerName = polymerName;
    this.weight = weight;
    }public String getPolymerName(){
    return polymerName;
    }public void setPolymerName(String polymerName){
    this.polymerName = polymerName;
    }public int getWeight(){
    return weight;
    }public void setWeight(int weight){
    this.weight= weight;
    }第一题是一个很简单的写出一个JavaBean类
      

  2.   

    interface Compareble{
    CuPolymer CompareTo(CuPolymer cuPolymer1, CuPolymer cuPolymer2);
    }public class CuPolymer implements Compareble {
    private String polymerName;
    private int weight; public CuPolymer() { } public CuPolymer(String polymerName, int weight) {
    this.polymerName = polymerName;
    this.weight = weight;
    } public String getPolymerName() {
    return polymerName;
    } public void setPolymerName(String polymerName) {
    this.polymerName = polymerName;
    } public int getWeight() {
    return weight;
    } public void setWeight(int weight) {
    this.weight = weight;
    }

    public void printPolymer(){
    System.out.println("聚合物的名称为:" + polymerName + ",分子量为:" + weight);
    }
    @Override
    public CuPolymer CompareTo(CuPolymer cuPolymer1, CuPolymer cuPolymer2) {
    int oneWeight = cuPolymer1.getWeight();
    int twoWeight = cuPolymer2.getWeight();
    if(oneWeight != twoWeight){
    return oneWeight > twoWeight? cuPolymer1 : cuPolymer2;
    }else {
    return cuPolymer1;
    }
    }}
    一楼代码有问题 题目没做完   =。=   上面这个是修改后的
      

  3.   

    public class CuPolymer { private String polymerName;
    private static  int weight; public CuPolymer(){
    } public CuPolymer(String polymerName, int weight){
    this.polymerName = polymerName;
    this.weight = weight;
    } public String getPolymerName(){
    return polymerName;
    } public void setPolymerName(String polymerName){
    this.polymerName = polymerName;
    } public static int getWeight(){
    return weight;
    } public void setWeight(int weight){
    this.weight= weight;
    }

    class ByWeight implements Comparable<Integer> {
    public int compareTo(Integer c1){
    return CuPolymer.getWeight() - c1;
    } private Integer getWeigth() {

    return null;
    } }}