• 20155323课堂实践20170510


    20155323课堂实践20170510

    第一题:修改教材P98 Score2.java

    让执行结果数组填充是自己的学号:提交在IDEA或命令行中运行结查截图,加上学号水印,没学号的不给成绩

    代码:

    import java.util.Arrays;
    public class score2 {
        public static void main(String[] args) {
            int[] scores = new int [1];
            for(int score :scores){
                System.out.printf("%2d",score);
            }
            System.out.println();
            Arrays.fill(scores,20155323);
            for(int score : scores){
                System.out.printf("%3d",score);
            }
        }
    }
    

    代码截图:

    遇到的问题

    • 这道题就是简单的数组填充。Arrays.fill()是一个二维数组的填充方法,这道题要求输出自己的学号,所以我只需要在Arrays.fill()内打入自己的学号,再进行输出就可以完成。
    • 这题未提交的原因是之前IDEA出现了丢包的问题,代码全部不能运行,为了贪图省事我把前面的代码全部删除了,所以电脑里面没有代码,需要重新打。另外我之前的git记录在前几周的博客里都有,绝不是弄虚作假。

    第二题:在IDEA中以TDD的方式对String类和Arrays类进行学习

    测试相关方法的正常,错误和边界情况

    • String类
    • charAt
    • split
    • Arrays类
    • sort
    • binarySearch

    代码:

    import junit.framework.TestCase;
    import java.util.Arrays;
    public class lxTest extends TestCase {
        String string = new String("World!");
        int[] a = {7,6,5,4,3,2,1};
        public void testCharAt() throws Exception {
            assertEquals('W',string.charAt(0));
        }
    
        public void testSplit() throws Exception {
            String[] s = string.split("o");
            assertEquals("rld!",s[1]);
        }
    
        public void testSort() throws Exception {
            Arrays.sort(a);
            assertEquals(5,a[4]);
        }
    
        public void testBinarySearch() throws Exception {
            assertEquals(-8,Arrays.binarySearch(a,7));
        }
    
    }
    

    遇到的问题:

    • 这道题我先建了一个空的lx类,然后建它的测试类,在测试类中定义变量,然后可以利用实验二的代码,再加入两个test进行测试,string.charAt(0)是取字符串string="World!"的第一个字符,预期得到的结果为字符'W'。如果 assertEquals()内逗号前预期的值和逗号后实际的值相符,则测试成功。这道题我遇到的问题是对assertEquals()语句不理解,不能够进行熟练运用。

    代码截图:

    第三题:模拟实现Linux下Sort -t : -k 2的功能。

    参考 Sort的实现。提交码云链接和代码运行截图。

    代码:

    import java.util.*;
    public class MySort1 {
        public static void main(String[] args) {
            String[] toSort = {"aaa:10:1:1",
                    "ccc:30:3:4",
                    "bbb:50:4:5",
                    "ddd:20:6:3",
                    "eee:40:2:20"};
    
            System.out.println("Before sort:");
    
            for (String str : toSort)
                System.out.println(str);
    
            int[] temp = new int[toSort.length];
            for (int i = 0; i < toSort.length; i++) {
                String[] s = toSort[i].split(":");
                temp[i] = Integer.parseInt(s[3]);
            }
    
            Arrays.sort(toSort);
            System.out.println("After sort:");
            for (String str: toSort)
                System.out.println(str);
    
            }
    
        }
    

    代码截图:

    遇到的问题

    这道题是要在老师给的代码中加上几行,模拟实现sort的排序功能。我加入的是

    int[] temp = new int[toSort.length];
    	        for (int i = 0; i < toSort.length; i++) {
    	            String[] s = toSort[i].split(":");
    	            temp[i] = Integer.parseInt(s[3]);
    	        }
    

    这段代码,先建立一个和toSort相同长度的空数组temp,由于-t的意思是指定排序时所用的栏位分隔字符,所以我根据";"号把字符串分割,并转成整形赋值给temp数组。遇到的困难是不清楚sort命令的常用语法,不知道sort -t;-k 2的意思。最后我查看了狄惟佳同学关于这个实践的博客,问题得到了解决。

    附:码云链接

  • 相关阅读:
    C++ 三数之和
    C++ 保存读取二进制
    流媒体 Ubuntu部署srs、windows部署nginx
    sql函数(三)MERGE INTO函数
    sql函数(二)LISTAGG()函数(列转行函数)
    sql函数(一)ROW_NUMBER() OVER()--表内分组并排序
    java注释(类、构造函数、方法、代码块、单行、属性注释)
    springBoot注解整理
    @DateTimeFormat与@JsonFormat作用
    DataJPA简述与注解
  • 原文地址:https://www.cnblogs.com/fixedl/p/6854171.html
Copyright © 2020-2023  润新知