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;
    }
}

 posted on 2022-09-24 16:35  ZZC12312321  阅读(15)  评论(0编辑  收藏  举报  来源