实验十 异常处理和泛型初步的学习

本文介绍了一个使用Java实现的BankAccount类,包括内部类Action的使用,以及异常处理机制。同时,深入探讨了泛型方法在折半搜索和二维数组最大值查找的应用。
  1. 内部类复习和异常处理学习
  1. 创建一个BankAccount类,该类有一个内部类Action。
  2. 内部类Action私有数据成员有:用于描述存取款的动作String型变量,描述存取款金额的long型变量。公有函数有:带这两个参数的构造函数。
  3. 外部类BankAccount私有数据成员有: 2个long型私有数据成员描述账号号码和账号余额,内部类Action类型的数据成员lastaction。公有成员函数有:带这两个参数的构造函数,存款函数(带一个参数存款额),该函数实现修改余额,并用“存款”字符串和存款额构造Action变量(示例代码见下);取款函数,该函数带一个参数取款额,该函数实现修改余额,并用“取款”字符串和取款额构造Action变量,当余额不够取款时,抛出异常;一个show函数显示信息。
public void depisit(long amount){

    balance+=amount;

    lastaction=new Action("存款",amount);

}
  1. 在main函数中创建2个账号,分别进行存取款操作,并显示行为动作,注意存款、取款字符串是从Action中得到的,显示参考结果见下图

 

 

  1.  泛型学习一
  1. (泛型折半搜索)为折半搜索实现以下泛型方法

public static<T extends Comparable<T>>  int binarySearch(T[] list, T key)

  1. 分别创建String和Integer数组测试并显示结果。函数返回要查找的位置,如果没找到返回-1,参考显示结果如下:

 

  1.  泛型学习二
  1. 编写下面的泛型方法,返回二维数组的最大值

public static<T extends Comparable<T>> T max(T[][] list)

  1. 分别创建String和Integer数组测试并显示结果。参考显示结果如下
public class BankAcount {
    public static void main(String[] args) {
        BankAcount a = new BankAcount(1001, 10000);
        System.out.println("账号"+a.account+" 原有金额:"+a.balance);
        BankAcount b = new BankAcount(1002, 10000);
        System.out.println("账号"+b.account+" 原有金额:"+b.balance);
        a.depisit(1000);
        a.show();
        try {
            a.withdraw(800);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        a.show();
        b.depisit(800);
        b.show();
        try {
            b.withdraw(20000);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        b.show();
    }
    private long account;
    private long balance;
    private Action lastaction;
    public BankAcount(long account, long balance) {
        this.account = account;
        this.balance = balance;
    }
    public class Action{
        private String act;
        private long amount;

        public Action(String act, long amount) {
            this.act = act;
            this.amount = amount;
        }
    }
    public void depisit(long amount){
        balance+=amount;
        lastaction=new Action("存款",amount);
    }
    public void withdraw(long amount) throws Exception{
        lastaction=new Action("取款",amount);
        if (balance < amount) {
            throw new Exception("\n余额不够!");
        }
        balance-=amount;
    }
    public void show(){
        System.out.println("账号" + account + " " + lastaction.act + lastaction.amount + " 剩余金额:" + balance);
    }
}
import java.util.Arrays;

public class Search {
    public static void main(String[] args) {
        String [] l={"helen","ally","skevin"};
        int pot=binarySearch(l,"ally");
        System.out.println("被检索字符串:"+ Arrays.toString(l));
        System.out.println("检索的字符串位置在:"+pot+" 检索内容为"+l[pot-1]);
        Integer []a={78,45,67,45};
        pot=binarySearch(a,67);
        System.out.println("被检索整形数组:"+Arrays.toString(a));
        System.out.println("检索的整形数位置在:"+pot+"检索内容为"+a[pot-1]);

    }
    public static<T extends Comparable<T>>  int binarySearch(T[] list, T key) {
        int low = 0;
        int high = list.length - 1;

        while (high >= low) {
            int mid = (low + high) / 2;
            if (key.compareTo(list[mid]) < 0) {
                high = mid - 1;
            }
            else if (key.equals(list[mid])) {
                return mid+1;
            }
            else {
                low = mid + 1;
            }
        }

        return  - 1;
    }
}
import java.util.Arrays;

public class Search {
    public static void main(String [] args){
        Integer[][] numbers = { {1, 2, 3}, {4, 8, 6}};
        System.out.println("二维数组:"+ Arrays.deepToString(numbers));
        System.out.println("最大值:"+max(numbers));
        String[][] strings = { {"ally","skevin","bob"}, {"welly", "davia", "Mary"}};
        System.out.println("二维数组:"+ Arrays.deepToString(strings));
        System.out.println("最大值:"+max(strings));
    }
    public static<T extends Comparable<T>> T max(T[][] list){
        T max=list[0][0];
        for(int i=0;i<list.length;i++ ){
            for(int j=0;j>list[i].length;j++){
                if(max.compareTo(list[i][j])<0)
                    max=list[i][j];
            }
        }
        return max;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值