编程的练习

编程练习

编程题目

1.编写一个方法,传入一个整数,完成对于这个整数的乘法表打印
2.三种方式完成一个数组的扩容功能
3.新增一个Teacher(tid, name, major, course)类,使用集合框架在控制台完成对于Teacher的CRUD功能
4.编写一个方法,完成对于给定目录下指定文件格式的查找
5.使用循环或者递归完成一个对于给定数据计算其阶乘的结果
6.描述你对于面向对象的理解
7.分别写String类和File类中你所知道的方法。
8.完成一个文本文件的拷贝功能

1.整数的乘法表打印

public class Exercise01 {
    public static void main(String[] args) {
        printMultiplicationTable(9);
    }
    //1.编写一个方法,传入一个整数,完成对于这个整数的乘法表打印
    public static void printMultiplicationTable(int num){
        for (int i = 1; i <= num ; i++) {
            for (int j = i; j <= num ; j++) {
                System.out.print(i+"*" +j+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

2.数组扩容功能

//三种方式完成一个数组的扩容功能
public class Exercise02 {
    public static void main(String[] args) {
        int []arrays = {1,2,3,4,5,6,4,8,6};

        //方式一测试
        int[] ints = dilatation01(arrays);
        for (int anInt : ints) {
            System.out.print(anInt + " ");
        }
        System.out.println();

        //方式二测试
        int[] ints2 = dilatation02(arrays);
        String string = Arrays.toString(ints2);
        System.out.println(string);


        //方式三测试
        int[] ints3 = dilatation03(arrays);
        String string3 = Arrays.toString(ints3);
        System.out.println(string3);

    }
    public static int[]  dilatation01(int []arrays){
        //使用遍历方式扩容
        //新建一个数组
        int length = arrays.length;
        int []arraysCopy = new int[length];
        //对arrays进行遍历传入arraysCopy中
        for (int i = 0; i < arraysCopy.length; i++) {
            arraysCopy[i] = arrays[i];
        }

        return arraysCopy;
    }

    public static int[]  dilatation02(int []arrays){
        //使用第二种方式扩容
        int length = arrays.length;
        int []arraysCopy = new int[length];
        System.arraycopy(arrays,0,arraysCopy,0,length);
        return  arraysCopy;
    }

    public static int[]  dilatation03(int []arrays){
        //使用第三种方式进行扩容
        int length = arrays.length;
        int []arraysCopy = Arrays.copyOf(arrays,length);
        return arraysCopy;
    }

}

4.文件格式查找

//编写一个方法,完成对于给定目录下指定文件格式的查找
public class Exercise04 {
    public static void main(String[] args) {
        String path = "D:\\";
        List<String> list = new ArrayList<>();
        list = searchFile(path,"md");
        for (String s : list) {
            System.out.println(s);
        }

    }
    static List<String> list = new ArrayList<>();
    public static List<String> searchFile(String path,String ...args){


        File f = new File(path);
        if (f.isFile()){
            for (String arg : args) {
                if(f.toString().toLowerCase().endsWith(arg.toLowerCase())){
                    list.add(f.getAbsolutePath());
                }
            }
        }else {
            File[] files = f.listFiles();
            if(files != null) {
                for (File file : files) {
                    searchFile(file.getAbsolutePath(), args);
                }
            }

        }
        return list;
    }
}

5.阶乘

public class Exercise05 {
    public static void main(String[] args) {
        System.out.println(factorial01(3));
        System.out.println(factorial02(3));

    }
    public static int factorial01(int num){
        int sum =1;
        if(num ==0 || num ==1){
            return 1;
        }else {
            return num * factorial01(num - 1);
        }
    }

    public static int factorial02(int num){
        int sum = 1;
        for (int i = 0; i <= num; i++) {
             if(i == 0 || i ==1){
                 sum = 1;
             }
            sum *= i;
        }
        return sum;
    }

}

6.String类和File类常用方法

String类
在这里插入图片描述
在这里插入图片描述

File类
在这里插入图片描述

8.文件拷贝

public class Exercise08 {
    public static void main(String[] args) {
        //由题意知道完成文本文件的拷贝 用字符流处理效率高,如果使用字节流文本中有汉字会出现乱码
        //如果使用字节流InputSteam会出现中文乱码现象可通过转换流进行操作
        String srcPath = "D:\\a.txt";
        String destPath = "D:\\b.txt";

       File file = new File(srcPath);

       if (!(file.exists())){
           try {
               file.createNewFile();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

        BufferedReader bf = null;
        BufferedWriter bw = null;

        int readLine = 0;
        char []chars = new char[10];

        try {
            bf = new BufferedReader(new FileReader(srcPath));
            bw = new BufferedWriter(new FileWriter(destPath));
            while ((readLine = bf.read(chars)) != -1){
                System.out.print(new String(chars, 0, readLine));
                bw.write(chars);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值