在cn.com.my包中,封装一个接口Sortable,包括一个抽象方法int compare( Sortable s),表示需要进行比较大小

软件NetBeans IDE 7.0.1,需要单独写主类。

在cn.com.my包中,封装一个接口Sortable,包括一个抽象方法int compare( Sortable s),表示需要进行比较大小,返回正数则表示大于,返回负数则表示小于,返回0则表示等于。封装一个类Student,要求实现此接口,必须重写接口中的抽象方法。Student 类中包括score属性,重写public String toString()方法,在比较大小时按照成绩的高低比较。封装一个类Rectangle,要求实现此接口,必须重写接口中的抽象方法。Rectange 类中包括length、width属性,同时包括相应的成员方法int area(),重写public String toString()方法,在比较大小时按照面积的大小进行比较。封装一类Sort类,其中定义方法public static void selectSort(Sortable []a),按照选择方法进行降或升序排序。 封装执行主类,测试程序。

主类:

   Student[] s=new Student[10];
        for(int i=0;i<s.length;i++){
            s[i]=new Student((int)(Math.random()*100));
        }
        Sort.selectSort(s);
        System.out.println("成绩升序");
        for(int i=0;i<s.length;i++){
            System.out.println(s[i]);
        }
        
        Rectangle[] r=new Rectangle[10];
        for(int i=0;i<r.length;i++){
            r[i]=new Rectangle((int)(Math.random()*100),(int)(Math.random()*100));
        }
        Sort.selectSort(r);
        System.out.println("面积升序");
        for(int i=0;i<r.length;i++){
            System.out.println(r[i]);
        }

接口Sortable:

public interface Sortable {
    public abstract int compare(Sortable s);
}

Student类:

public class Student implements Sortable{
    private double score;
    public Student(double score){
        this.score=score;
    }
   
    public int compare(Sortable s){
       Student f=(Student)s;
       if(f.getScore()>getScore()){
           return 1;
       }
       else if(f.getScore()<getScore()){
           return -1;
       }
       else{
           return 0;
       }
    }
    public String toString(){
        return " "+score;
    }

    
    public double getScore() {
        return score;
    }

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

Rectange类:

public class Rectangle implements Sortable{
    private int length;
    private int width;
    public int area(){
        return length*width;
    }
    public int compare(Sortable s){
        Rectangle r=(Rectangle)s;
        if(area()>r.area()){
            return -1;
        }
        else if(area()<r.area()){
            return 1;
        }
        else{
            return 0;
        }
    }
    public Rectangle(int length,int width){
        this.length=length;
        this.width=width;
    }
    public Rectangle(){
        
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }
    
    public String toString(){
        return " "+area();
    }

    

   
}

Sort类:

public class Sort {

    public static void selectSort(Sortable[] a) {
        Sortable d;
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].compare(a[i]) > 0) {
                    d = a[i];
                    a[i] = a[j];
                    a[j] = d;
                }
            }
        }
    }
}
发布了14 篇原创文章 · 获赞 14 · 访问量 651

猜你喜欢

转载自blog.csdn.net/weixin_46292455/article/details/104886742