大一Java程序设计题

1.Student:name—String courses—Course[] 3-5门 score—int[]2.Course:courseName3.TestDemo:测试员 main方法至少三个学生,每个学生至少三门课要求按照课程平均分降序排序

//Student 文件
public class Student implements Comparable<Student> {
    private String name;
    private Course[] courses;
    private int[] sorce;
    public int[] getSorce() {
        return sorce;
    }
    public void setSorce(int... sorce) {
        this.sorce = sorce;
    }
    private double avg;
    public  Student(String name) {
        this.name = name;
    }
    public double getAvg() {
        int sum = 0;
        for (int i = 0; i < sorce.length; i++) {
            sum += sorce[i];
        }
        avg =1.0 * sum / sorce.length;
        avg = (int)(avg*100)/100.0;
        return avg;
    }
    public void setCourses(Course... courses) {
        this.courses = courses;
    }
    
    //降序排列
    @Override
    public int compareTo(Student o) {
        if(getAvg() > o.getAvg())
            return -1;           
        else if(getAvg() == o.getAvg()) {
            return 0;
        }else 
            return 1;
    }
    public String toString(){
        return this.name + " 平均分 " + this.getAvg();
    }
}
//Course类
public class Course {
    String courseName;
    public Course(String courseName){
        this.courseName = courseName;
    }
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
}
// TestDemo类
import java.util.TreeSet;
public class TestDemo {
    public static void main(String[] args) {
        Course[] courses = {new Course("语文"),new Course("数学"),new Course("英语")};
        TreeSet<Student> ts = new TreeSet<Student>();
        Student s1 = new Student("张三");
        Student s2 = new Student("李四");
        Student s3 = new Student("王五");
        s1.setCourses(courses);
        s1.setSorce(1,1,1);
        ts.add(s1);
        s2.setCourses(courses);
        s2.setSorce(2,3,6);
        ts.add(s2);
        s3.setCourses(courses);
        s3.setSorce(3,4,5);
        ts.add(s3);
        System.out.println(ts);
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-31
帮你写了一点代码,看看吧。publicclassGenerateRectangle{publicstaticvoidmain(String[]args){Rectangle[]rects=newRectangle[10];intx,y;doublewidth,height;for(inti=0;i<10;i++){Rectangler=newRectangle();x=(int)(Math.random()*100);y=(int)(Math.random()*100);r.setCoordinate(x,y);width=(int)(Math.random()*1000);height=(int)(Math.random()*1000);r.setWidthAndHeight(width,height);rects[i]=r;System.out.println(r.toString());}}}classRectangle{privateintx;privateinty;privatedoublewidth;privatedoubleheight;/***设置坐标**@since2017-5-27*/publicvoidsetCoordinate(intx,inty){this.x=x;this.y=y;}publicvoidsetWidthAndHeight(doublewidth,doubleheight){this.width=width;this.height=height;}@OverridepublicStringtoString(){return"矩形坐标[x="+x+",y="+y+",宽度="+width+",高度="+height+"]";}}追问

你tm在逗我,根本不是一个东西

本回答被网友采纳
相似回答