20155302 第十二周课堂实践

20155302 第十二周课堂实践

第一题

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

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

运行截图如下:

第二题:

在IDEA中以TDD的方式对String类和Arrays类进行学习:测试相关方法的正常,错误和边界情况String类-charAt-splitArrays-sort-binarySearch

代码如下:
import java.lang.reflect.Array;
import java.util.Arrays;
/**
* Created by lenovo on 2017/5/14.
*/
public class test {
String string = new String();
int i;
public char charAt (int i){
return string.charAt(i);
}

        public String[] split(String a){
            return string.split(a);
        }

        public void sort(int[] a){
            Arrays.sort(a);
        }

        public int binarySearch(int[] a,int key){
            return Arrays.binarySearch(a,key);
        }

    }

测试代码:

import junit.framework.TestCase;
import java.lang.*;
import java.util.Arrays;

/**
 * Created by lenovo on 2017/5/14.
 */

    public class testTest extends TestCase {
        String string = new String("Hello:World!");
        int[] a = {7,6,5,4,3,2,1};
        public void testCharAt() throws Exception {
            assertEquals('H',string.charAt(0));
        }

        public void testSplit() throws Exception {
            String[] s = string.split(":");
            assertEquals("Hello",s[0]);
        }

        public void testSort() throws Exception {
            Arrays.sort(a);
            assertEquals(5,a[4]);
        }

        public void testBinarySearch() throws Exception {
            assertEquals(-8,Arrays.binarySearch(a,7));
        }

    }

第三题:

模拟实现Linux下Sort -t : -k 2的功能。参考 Sort的实现。提交码云链接和代码运行截图。

  1 import java.util.*;
  2
  3 public class MySort1 {
  4     public static void main(String [] args) {
  5         String [] toSort = {"aaa:10:1:1",
  6                             "ccc:30:3:4",
  7                             "bbb:50:4:5",
  8                             "ddd:20:5:3",
  9                             "eee:40:2:20"};
 10
 11         System.out.println("Before sort:");
 12         for (String str: toSort)
 13                     System.out.println(str);
 14
 15         Arrays.sort(toSort);
 16
 17         System.out.println("After sort:");
 18         for( String str : toSort)
 19             System.out.println(str);
 20     }
 21 }

改后代码:
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:5:3",
                "eee:40:2:20"};
        System.out.println("Before sort:");//打印输入的数组
        for (String str : toSort)
            System.out.println(str);
        System.out.println();

        int[] arr = new int[toSort.length];
        for (int i = 0; i < toSort.length; i++) {//将原数组每行以 : 分隔开,存放于数组tmp中
            String[] tmp = toSort[i].split(":");
            arr[i] = Integer.parseInt(tmp[3]);//将第四列放到先前建立的数组 arr 中
}

            Arrays.sort(arr);//对第四列数字进行排序

            System.out.println("After sort:");
            for (int i = 0; i < arr.length; i++) {//遍历arr中元素
                for (int j = 0; j < toSort.length; j++)//遍历原数组各行
                    if (arr[i] == Integer.parseInt((toSort[j].split(":"))[3])) {//当数组tosort中第四列元素与arr中元素相等时,输出 tosort
                        System.out.println(toSort[j]);//输出以arr中元素为顺序的tosort数组
                    }
                }

            }
        }

运行截图如下:

原文地址:https://www.cnblogs.com/STILLlover521/p/6854251.html