Angel丶奏弦。 2022-05-15 09:16 采纳率: 42.9%
浏览 931
已结题

JAVA编程,定义一个Customer类

JAVA定义一个Customer类,包含姓名(name)、身高(height)、体重(weight),分数(score)以及speak()方法,该方法的功能是,输出自己的相关信息。 Customer类实现Comparable接口,实现比较两个Customer对象的大小,比较规则是:身高占20%权重,体重占30%权重,分数占50%权重,以综合计算三项权重和之后的值作为判断对象大小的依据。
import java.util.Arrays;

class Customer implements Comparable {
private String name;
private float height, weight, score;

    public  Customer(String  name,  float  height,  float  weight,  float  score)  {
            super();
            this.name  =  name;
            this.height  =  height;
            this.weight  =  weight;
            this.score  =  score;
    }

    public  String  getName()  {
            return  name;
    }

    public  void  setName(String  name)  {
            this.name  =  name;
    }

    public  float  getHeight()  {
            return  height;
    }

    public  void  setHeight(float  height)  {
            this.height  =  height;
    }

    public  float  getWeight()  {
            return  weight;
    }

    public  void  setWeight(float  weight)  {
            this.weight  =  weight;
    }

    public  float  getScore()  {
            return  score;
    }

    public  void  setScore(float  score)  {
            this.score  =  score;
    }

    public  void  speak()  {
            System.out.println("I  am  "  +  name  +  ",my  height  "  +  height  +  ",my  weight  "  +  weight  +  ",my  score  "  +  score);
    }

    public  int  compareTo(Customer  o)  {
                            

float s1=this.height+this.weight;
float s2=o.height+o.weight;
if(s1>s2) {
return 1;
}
if(s1<s2) {
return -1;
}
return 0;
}

    }

    public  String  toString()  {
            

return "Customer [name="+name+",height="+height+",weight="+weight+"]";

    }

}

public class TestCompare {
public static void main(String[] args) {
int i;
Customer ps[] = new Customer[6];
ps[0] = new Customer("zhangsan", 170, 110, 95);
ps[1] = new Customer("lisi", 168, 120, 75);
ps[2] = new Customer("wangwu", 165, 115, 88);
ps[3] = new Customer("zhaoliu", 172, 121, 90);
ps[4] = new Customer("zhouqi", 160, 100, 85);
ps[5] = new Customer("zhengba", 166, 119, 70);
System.out.println("array sort before:");
for (i = 0; i < ps.length; i++) {
ps[i].speak();
}
Arrays.sort(ps); // call sort method
System.out.println("\narray sort after:");
for (i = 0; i < ps.length; i++) {
System.out.println(ps[i]);
}
}
}

TestCompare.java:65: error: class, interface, or enum expected
public String toString() {
 ^

1 error

怎么解决啊, public String toString() {

return "Customer [name="+name+",height="+height+",weight="+weight+"]";

    }

}
这一段错误

该如何正确得到结果
  • 写回答

3条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-05-15 11:49
    关注

    给你改好了,采纳一下,亲

    img

    
    import java.util.*;
    import java.io.*;
    
    public class Test {
    
        public static void main(String[] args) {
            int i;
            Customer ps[] = new Customer[6];
            ps[0] = new Customer("zhangsan", 170, 110, 95);
            ps[1] = new Customer("lisi", 168, 120, 75);
            ps[2] = new Customer("wangwu", 165, 115, 88);
            ps[3] = new Customer("zhaoliu", 172, 121, 90);
            ps[4] = new Customer("zhouqi", 160, 100, 85);
            ps[5] = new Customer("zhengba", 166, 119, 70);
            System.out.println("array sort before:");
            for (i = 0; i < ps.length; i++) {
                ps[i].speak();
            }
            Arrays.sort(ps); // call sort method
            System.out.println("\narray sort after:");
            for (i = 0; i < ps.length; i++) {
                System.out.println(ps[i]);
            }
        }
    
    
    
    
    }
    
    class Customer implements Comparable{
        private String name;
        private float height, weight, score;
        public  Customer(String  name,  float  height,  float  weight,  float  score)  {
            super();
            this.name  =  name;
            this.height  =  height;
            this.weight  =  weight;
            this.score  =  score;
        }
    
        public  String  getName()  {
            return  name;
        }
    
        public  void  setName(String  name)  {
            this.name  =  name;
        }
    
        public  float  getHeight()  {
            return  height;
        }
    
        public  void  setHeight(float  height)  {
            this.height  =  height;
        }
    
        public  float  getWeight()  {
            return  weight;
        }
    
        public  void  setWeight(float  weight)  {
            this.weight  =  weight;
        }
    
        public  float  getScore()  {
            return  score;
        }
    
        public  void  setScore(float  score)  {
            this.score  =  score;
        }
    
        public  void  speak()  {
            System.out.println("I  am  "  +  name  +  ",my  height  "  +  height  +  ",my  weight  "  +  weight  +  ",my  score  "  +  score);
        }
    
        public  int  compareTo(Customer  o)  {
            float s1=this.height+this.weight;
            float s2=o.height+o.weight;
            if(s1>s2) {
                return 1;
            }
            if(s1<s2) {
                return -1;
            }
            return 0;
        }
    
    
        public  String  toString()  {
            return "Customer [name="+name+",height="+height+",weight="+weight+"]";
        }
    
        @Override
        public int compareTo(Object o1) {
            float s1=this.height+this.weight;
            Customer o=(Customer)o1;
            float s2=o.height+o.weight;
            if(s1>s2) {
                return 1;
            }
            if(s1<s2) {
                return -1;
            }
            return 0;
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月11日
  • 已采纳回答 7月3日
  • 修改了问题 5月15日
  • 创建了问题 5月15日

悬赏问题

  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统