20155336 第十二周课堂总结 20155336虎光元

修改教材P98 Score2.java

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

代码

    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,20155339);
          for(int score : scores)
     {
          System.out.printf("%9d",score);
     }
     }
     }

截图

模拟实现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:5:3",
            "eee:40:2:20"};

    System.out.println("Before sort:");
    for (String str: toSort)
        System.out.println(str);

    Arrays.sort(toSort);

    System.out.println("After sort:");
    for( String str : toSort)
        System.out.println(str);
}
}

代码

    import java.util.*;
    import java.lang.*;
    public class Mysort {
    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"};

    int [] tmp = new int [toSort.length];
    int val;
    String [] s;

    System.out.println("Before sort:");
    for (String i : toSort) {
        System.out.println(i);

    }

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

    System.out.println("After sort:");
    for(int i = 0; i < toSort.length; i++)
        for(int j = 0; j < toSort.length; j++){
            s = toSort[j].split(":");
            val = Integer.parseInt(s[3]);
            if(val == tmp[i]) {
                System.out.println(toSort[j]);
            }
        }
    }
    }

截图

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

  • 测试相关方法的正常,错误和边界情况
  • String类
  • charAt
  • split
  • Arrays类
  • sort
  • binarySearch

代码

package exp2;
import org.junit.Test;
import junit.framework.TestCase;
import junit.framework.TestResult;

 import java.util.Arrays;

 public class MyUtil1Test extends TestCase {
 String s1 = "abcde";
 String s2 = "aaa:bbb:ccc";
 String[] a1 = {"aaa","bbb","ccc"};
 int [] c1 = {2,5,3,4};
 char [] c2 = {'a','b','c','d'};

 @org.junit.Test
    public void testNormal() {
        assertEquals("不及格", MyUtil1.percentage2fivegrade(55));
        assertEquals("及格", MyUtil1.percentage2fivegrade(65));
        assertEquals("中等", MyUtil1.percentage2fivegrade(75));
        assertEquals("良好", MyUtil1.percentage2fivegrade(85));
        assertEquals("优秀", MyUtil1.percentage2fivegrade(95));
    }

    @org.junit.Test
    public void testException() {
        //测试出错情况
        assertEquals("错误", MyUtil1.percentage2fivegrade(-10));
        assertEquals("错误", MyUtil1.percentage2fivegrade(105));

    }

    @org.junit.Test
    public void testBoundary() {
        //测试边界情况
        assertEquals("不及格", MyUtil1.percentage2fivegrade(0));
        assertEquals("及格", MyUtil1.percentage2fivegrade(60));
        assertEquals("中等", MyUtil1.percentage2fivegrade(70));
        assertEquals("良好", MyUtil1.percentage2fivegrade(80));
        assertEquals("优秀", MyUtil1.percentage2fivegrade(90));
        assertEquals("优秀", MyUtil1.percentage2fivegrade(100));

    }
 @Test
 public void charAt() throws Exception {
     assertEquals('a',s1.charAt(0));
     assertEquals('e',s1.charAt(4));
 }

 @Test
 public void split() throws Exception {
     assertEquals(a1,s2.split(":"));
 }

 @Test
 public void sort() throws Exception{
     Arrays.sort(c1);
     assertEquals(5,c1[3]);
 }

 @Test
 public void binarySearch() throws Exception{
     int c;
     c = Arrays.binarySearch(c2,'c');
     assertEquals(2,c);
 }
}

截图

posted on 2017-05-13 10:33  丿尛丶熊  阅读(148)  评论(0编辑  收藏  举报