【JAVA】【继承&接口多实现&异常】实现数组的排序、打印、求和以及异常发现(含代码优化)

要求

1:

定义三个接口,分别为sortable,printable,addable,
要求其中分别有一个方法叫sort()print()add();

2:

自定义MyException类,根据3的需要定义相应函数。

3:

编写三个类Select,Quick,Bubble,要求它们均有一成员变量 int arr[],
且其构造方法Select(int arr[]),Quick(int arr[]),Bubble(int arr[])
能够接收整数数组赋值给成员变量arr
并要求他们各自实现sortable,printable,addable,三个接口
sort方法用来实现排序功能
print用来实现将arr内容输出
add用来将arr的成员求和并返回结果
如果add的结果是负数,则抛出MyException类型的例外
要求用到语句throwsthrow

4:

main函数中生成一个整数数组
分别调用Select,Quick,Bubble进行演示排序,打印,求和与例外捕捉
要求使用 try catch语句对例外进行捕捉

分析

本题的特点在于多实现,即一个类要实现三个接口
而具体的功能实际定义在被实现的类中

同时由于求和时可能出现例外,所以要在add() 方法使用 throws语句

同时在类中的add() 方法需要使用 throw 语句

已给出的代码

public class Day09 {
    public static void main(String[] args) throws Exception {
        final int NUM = 10;
        int[] arr = new int[NUM], brr = new int[NUM], crr = new int[NUM];
        java.util.Random r = new java.util.Random();
        for (int i = 0; i < NUM; i++) {
            crr[i] = brr[i] = arr[i] = r.nextInt() - 1000000;
        }

        sortable[] tools1 = { null, null, null };
        printable[] tools2 = { null, null, null };
        addable[] tools3 = { null, null, null };

        Bubble b = new Bubble(arr);
        tools1[0] = b;
        tools2[0] = b;
        tools3[0] = b;

        Select s = new Select(brr);
        tools1[1] = s;
        tools2[1] = s;
        tools3[1] = s;

        Quick q = new Quick(crr);
        tools1[2] = q;
        tools2[2] = q;
        tools3[2] = q;

        for (int i = 2; i >= 0; i--) {
            tools1[i].sort();
            tools2[i].print();
            try {
                tools3[i].add();
            } catch (MyException me) {
                System.out.println(me.toString());
                System.out.println("Error negative finger is :" + me.getValue());
            }
        }
    }
}

接口的定义

interface sortable {
    public abstract void sort();
}

interface printable {
    public abstract void print();
}

interface addable {
    public abstract void add() throws MyException;
}

Select、Bubble、Quick类的定义

//add()方法的详细代码仅在Select中列出
class Select implements sortable, printable, addable {
    public int[] arr = null;

    Select(int[] arr) {
        this.arr = arr;
    }

    public void sort() {
        /*
        排序
        */
    }

    public void swap(int[] arr, int i, int j) {
        /*
        交换
        */
    }

    public void print() {
        /*
        打印
        */
    }

    public void add() throws MyException {
        int sum = 0;
        for (int i = 0; i < arr.length; ++i) {
            sum += arr[i];
        }
        if (sum < 0) {
            throw new MyException("number is negative!", sum);
        } else {
            System.out.println("sum:" + sum);
        }
    }
}

class Bubble implements sortable, printable, addable {
    public int[] arr = null;

    Bubble(int[] arr) {
        this.arr = arr;
    }

    public void sort() {
        /*
        排序
        */
    }

    public void swap(int[] arr, int i, int j) {
        /*
        交换
        */
    }

    public void print() {
        /*
        打印
        */
    }

    public void add() throws MyException {
        /*
        求和
        */
}

class Quick implements sortable, printable, addable {
    public int[] arr = null;

    Quick(int[] arr) {
        this.arr = arr;
    }

    public void sort() {
        QuickSort(0, arr.length - 1);
    }

    public void QuickSort(int left, int right) {
        /*
        排序
        */
    }

    public void swap(int[] arr, int i, int j) {
        /*
        交换
        */
    }

    public void print() {
        /*
        打印
        */
    }

    public void add() throws MyException {
        /*
        求和
        */
    }
}

MyException类的定义

class MyException extends Exception {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private int value;

    MyException(String msg, int value) {
        super(msg);
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

测试结果

-1361095769,-1340741330,-1304637140,-1199867124,-986358351,-578006868,416533965,749651829,1704616977,2100715794
MyException: number is negative!
Error negative finger is :-1799188017
-1361095769,-1340741330,-1304637140,-1199867124,-986358351,-578006868,416533965,749651829,1704616977,2100715794
MyException: number is negative!
Error negative finger is :-1799188017
-1361095769,-1340741330,-1304637140,-1199867124,-986358351,-578006868,416533965,749651829,1704616977,2100715794
MyException: number is negative!
Error negative finger is :-1799188017

成功完成三种排序以及打印、求和
同时成功捕捉例外

代码优化

在以上代码中,可以看出,print()add()swap()
三个完全相同的方法重复出现了三次,因此可以将这三个方法封装到一个新类
然后让Select,Quick,Bubble 类继承它

这里将新类定义为ToolTool类只需要实现printable,addable 两个接口
Select,Quick,Bubble 在继承Tool类的同时实现sortable接口

Tool类的定义

class Tool implements printable, addable {
    public int[] arr = null;

    Tool(int[] arr) {
        this.arr = arr;
    }

    public void swap(int[] arr, int i, int j) {
        /*
        交换
        */
    }

    public void print() {
        /*
        打印
        */
    }

    public void add() throws MyException {
        /*
        求和
        */
    }
}

优化后的Select、Bubble、Quick类

class Select extends Tool implements sortable {
    Select(int[] arr) {
        super(arr);
    }

    public void sort() {
        /*
        排序
        */
    }
}

class Bubble extends Tool implements sortable {
    Bubble(int[] arr) {
        super(arr);

    }

    public void sort() {
        /*
        排序
        */
    }
}

class Quick extends Tool implements sortable {
    Quick(int[] arr) {
        super(arr);

    }

    public void sort() {
        QuickSort(0, arr.length - 1);
    }

    public void QuickSort(int left, int right) {
        /*
        排序
        */
    }
}

测试结果

-1999368824,-1718825310,-1042260117,-893502441,-384032140,-126569285,-98211487,-84206065,216277259,712595420
MyException: number is negative!
Error negative finger is :-1123135694
-1999368824,-1718825310,-1042260117,-893502441,-384032140,-126569285,-98211487,-84206065,216277259,712595420
MyException: number is negative!
Error negative finger is :-1123135694
712595420,216277259,-84206065,-98211487,-126569285,-384032140,-893502441,-1042260117,-1718825310,-1999368824
MyException: number is negative!
Error negative finger is :-1123135694

可以看到同样成功完成了三种排序以及打印、求和
并且成功捕捉到异常

代码量对比

优化前代码

原代码量在VS Code中共234行

优化后代码
优化后代码量在VS Code中为182行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值