Java封装一个接口Sortable,封装一个接口Instrument

9:在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),按照选择方法进行降或升序排序。 封装执行主类,测试程序。
10:封装一个接口Instrument

9,10题放到同一个主类中测试

主类

import cn.com.my.*;

public class Main {
    public static void main(String[] args) {
        Student[] s = new Student[10];
        for (int i = 0; i < s.length; i++) {
            s[i] = new Student();
            s[i].setScore((int) (Math.random() * 100));
        }
        System.out.println("成绩升序");
        Sort.selectSort(s);
        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();
            r[i].setLength((int) (Math.random() * 100));
            r[i].setWidth((int) (Math.random() * 100));
        }
        System.out.println("面积升序");
        Sort.selectSort(r);
        for (int i = 0; i < r.length; i++) {
            System.out.println(r[i]);
        }
        MyInstrument my = new MyInstrument();
        my.playInstrument(new Instrument() {
            public void play() {
                System.out.println("匿名类:Play");
            }
        });


    }

}
package cn.com.my;

public interface Instrument {
     public abstract void play();
}
package cn.com.my;

public class MyInstrument {
    public void playInstrument(Instrument ins){
    ins.play();
    }

}
package cn.com.my;

public class Rectangle implements Sortable{
    double length;
    double width;

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

    public double getLength() {
        return length;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getWidth() {
        return width;
    }

    public int area(){
        int area;
        area=(int)(width*length);
        return area;
    }
    public int compare(Sortable s){
        Rectangle a=(Rectangle) s;
        if(a.area()>area())
            return 1;
        else if (a.area()==area()) {
            return 0;
        }
        else
            return -1;
    }
    public  String toString(){

        return ""+area();
    }
}
package cn.com.my;

public class Sort {
    public static void selectSort(Sortable[] a) {
        Sortable temp;
        int j = 0;
        int i = 0;
        for (i = 0; i < a.length - 1; i++) {
            for (j = i + 1; j < a.length; j++) {
                if (a[j].compare(a[i]) > 0) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }

            }
        }
    }
    public static void selectSort(Sortable[] a,int b) {
        Sortable temp;
        int j = 0;
        int i = 0;
        for (i = 0; i < a.length - 1; i++) {
            for (j = i + 1; j < a.length; j++) {
                if (a[j].compare(a[i]) < 0) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }

            }
        }
    }

}
package cn.com.my;

public interface Sortable {
    int compare(Sortable s);

}
package cn.com.my;

public class Student implements Sortable {
    double score;

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

    public double getScore() {
        return score;
    }

    public int compare(Sortable s) {
        Student f = (Student) s;
        if (f.getScore() > getScore())
            return 1;
        else if (f.getScore() == getScore())
            return 0;
        else
            return -1;
    }

    public String toString() {
        return ""+score;
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值