• 20155318Java课堂实践20170510


    20155318Java课堂实践20170510

    修改教材P98 Score2.java

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

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

    在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);
         }
        }
    
    
    截图

    模拟实现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);
          }
      }
    
    代码
    package exam20170510;
    
    /**
     * Created by Administrator on 2017/5/10.
     */
    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]);
                    }
                }
        }
    }
    
    截图

  • 相关阅读:
    python关于字符串数据类型的方法
    python流程控制
    python解释器下载
    (转)蓝牙无线技术配置文件
    Android 编译选项user、userdebug和eng的区别
    (转)android4.0蓝牙使能的详细解析
    (转)Android Bluetooth opp package 学习笔记
    《深入理解Android(卷1)》笔记 1.第二章 深入理解JNI
    (转)Vim知识
    Android之Preference
  • 原文地址:https://www.cnblogs.com/lxy1997/p/6835308.html
Copyright © 2020-2023  润新知