zoukankan      html  css  js  c++  java
  • 20155310 2016-2017-2《Java程序设计》课堂实践补交

    20155310 2016-2017-2《Java程序设计》课堂实践补交

    第九周

    程序设计中临时变量的使用

    public class linshibianliang {

    public static int[] insert(int a[], int index, int value) {
    
        int b[] = new int[a.length + 1];
    
        for (int i = 0; i < b.length; i++) {
    
            if (i < index - 1) {
    
                b[i] = a[i];
    
            }
    
            if (i == index - 1) {
    
                b[i] = value;
    
            }
    
            if (i > index - 1) {
    
                b[i] = a[i - 1];
    
            }
    
        }
    
        return b;
    
    }
    
    
    
    public static void printArray(int a[]) {
    
        for (int i = 0; i < a.length; i++) {
    
            System.out.print(a[i] + " ");
    
        }
    
        System.out.println();
    
    }
    
    
    
    public static int[] delete(int a[], int index) {
    
        int b[] = new int[a.length - 1];
    
        for (int i = 0; i < b.length; i++) {
    
            if (i < index - 1) {
    
                b[i] = a[i];
    
            } else {
    
                b[i] = a[i + 1];
    
            }
    
        }
    
        return b;
    
    }
    
    public static void main(String args[]) {
    
        int[] a = { 1, 2, 3, 4, 5, 6 ,7,8};
    
        int[]b = delete(a, 5);
    
        printArray(b);
    
        b = insert(a, 4, 5);
    
        printArray(b);
    
    }
    

    }

    我记得当时电脑上课时候没电了,下课之后就忘记了这个事情。现在来看比较简单

    第十周

    递归

    代码

    public class TestArgs2 {

    static int j=0,k;
    
    static int sum = 0;
    
    public static void main(String [] args) {
    
    
    
        Integer num[] = new Integer[args.length];
    
        int[] num1 = new int[args.length];
    
        k = args.length;
    
        for(int i=0;i<args.length;i++) {
    
            num[i] =Integer.parseInt(args[i]);
    
            num1[i] = num[i].intValue();
    
        }
    
        clSum(num1);
    
        System.out.println(sum);
    
    }
    
    public static void  clSum(int [] arr) {
    
        if(j<k) {
    
            sum += arr[j];
    
            j++;
    
            clSum(arr);
    
        }
    
    }
    

    }

    在学霸的带领下一步步学明白的,真的是费劲。还好最后是通过的。

    命令行参数

    代码:

    package cc.openhome;

    public class TestArgs {

    public static void main(String [] args) {
    
        int sum = 0;
    
        int arr[]=new int[args.length];
    
        for(int i=0;i<args.length;i++){
    
            arr[i]=Integer.parseInt(args[i]);
    
            sum+=arr[i];
            
        }
        System.out.println(sum);
    
    }
    

    第十一周

    代码检查-P74

    public class a
    {
        public static void main(String[] args) {
            for(int i=1;i<10;i++)
            {
                for(int j=2;j<i;j++)
                {
                    System.out.printf("%d*%d=%2d " ,j,i , j*i);
                }
                System.out.println();
            }
        }
    }//
    

    这个代码为下三角,如果要改为上三角只需要将“j<i”改为"j<10-i"即可。

    我发现懂得怎么改之后,敲代码还是有些趣味的,有种豁然开朗的感觉~

    表达式后缀表达式

    a b * c d e / - f * +

    猛地发现自己当时连个后缀表达式都没交。。很懊恼

    简易计算器

    代码

    public class Calc {
    
        public static void main(String[] args) {
    
            int[] arr = new int[2];
    
            char op;
    
            int result = 0;
    
            if(args.length==3){
    
                arr[0] = Integer.parseInt(args[0]);
    
                arr[1] = Integer.parseInt(args[2]);
    
                op = args[1].charAt(0);
    
                switch(op) {
    
                    case '+':
    
                        result = arr[0] + arr[1];
    
                        break;
    
                    case '-':
    
                        result = arr[0] - arr[1];
    
                        break;
    
                    case '*':
    
                        result = arr[0] * arr[1];
    
                        break;
    
                    case '/':
    
                        result = arr[0] / arr[1];
    
                        break;
    
                    case '%':
    
                        result = arr[0] % arr[1];
    
                        break;
    
                    default:
    
                        System.out.println("error!");
    
                }
    
                System.out.println(args[0] + " " + args[1] + " " + args[2] + " = " + result );
    
            }
    
        }
    
    }
    


    这个代码找了好久,真的是不容易。看到简易计算器名称时候我就很感兴趣,所以这个实验是我当时做出来的。后来问到是需要当面找您验收,就有点怕,就没交,现在想想很蠢...

    第十二周

    教材代码检查-p98

    代码:

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

    so essay~感觉自己好强!我是不是有点太膨胀了...

    第十三周

    教材代码检查-p145
    代码:

    import junit.framework.TestCase;
    import org.testng.annotations.Test;
    
    /**
     * Created by myl on 2017/6/11.
     */
    public class MathToolTest extends TestCase {
        @Test
        public void testSum() throws Exception {
            assertEquals(7,7);
            assertEquals(5,MathTool.sum(2,3));
            assertEquals(14,MathTool.sum(2,3,4,5));
            assertEquals(2,MathTool.sum(0,0,2));
            assertEquals(21,MathTool.sum(1,2,3,4,5,6));
            assertEquals(-10,MathTool.sum(-4,-6));
        }
    
    }
    

    代码是我的idea是别人的很气,idea真的坏了只能用着别人的idea流着自己的泪,

    第十四周

    没有~

    第十五周

    Myod

    代码:
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    public class MyOD {
    public static String format(byte []bt){
    int line=0 ;
    StringBuilder buf=new StringBuilder() ;
    for(byte d:bt){
    if(line%160)
    buf.append(String.format("%05x: ", line)) ;
    buf.append(String.format("%02x ", d)) ;
    line++ ;
    if(line%16
    0)
    buf.append(" ");
    }
    buf.append(" ") ;
    return buf.toString();
    }
    public static byte[] readFile(String file) throws IOException{
    InputStream is=new FileInputStream(file) ;
    int length=is.available() ;
    byte bt[]=new byte[length] ;
    is.read(bt) ;
    return bt;
    }
    public static void main(String[]agrs) throws IOException{
    byte []bt=MyOD.readFile("~//20155310srcMyOD.txt");
    String hexData=MyOD.format(bt) ;
    System.out.println(hexData);
    }
    }

    Mycp

    代码:

    import java.io.*;
    
    
    
    public class CharUtilxt {
    
        public static void dump(Reader src, Writer dest) throws IOException {
    
            try(Reader input = src; Writer output = dest) {
    
                char[] data = new char[8];
    
                char []ch = new char[1];
    
    
    
                while((input.read(data)) != -1) {
    
                    String str="";
    
                    for(int i=0;i<8;i++)
    
                    {
    
                        str+=data[i];
    
                    }
    
                    int ten=Integer.valueOf(str,2);
    
                    ch[0]=(char)ten;
    
                    output.write(ch, 0, 1);
    
                    output.write(" ");
    
                }
    
            }
    
        }
    
    }
    

    第十六周

    数据库-3,-4,-5

    这三个真的不会弄,周二还有一场考试,感觉时间不够用了,很无奈。
    我真的是很想学会这个数据库调用,可是我抱的学霸也不会...这就很尴尬了...

    6月11日 代码检查补交

    1.代码如下:

    import java.util.*;
    
    public class MySort {
    
    
        public static void main(String[] args) {
            int[] arr = new int[10];
            for(int i=0;i<args.length;i++)
            {
                arr[i]=Integer.parseInt(args[i]);
            }
    
            for(int i=0;i<args.length-1;i++){
                           for(int j=0;j<args.length-i-1;j++){
                                    if(arr[j]>arr[j+1]){
                                             int temp=arr[j];
                                        arr[j]=arr[j+1];
                                        arr[j+1]=temp;
                                    }
                                }
                         }
            for(int i=0;i<args.length;i++)
            {
                System.out.printf("%d ",arr[i]);
            }
        }
    }
    

    这个代码我是真的会做,但是无奈idea被我搞坏了,上午就没办法了。但是老师请相信我,代码我是真的会的!!!
    回去立刻拿同学的电脑做出来,现在补交上去...

    代码如下:

    package cc.openhome;

    public class SimpleLinkedList {
    private class Node {
    Node(Object o) {
    this.o = o;
    }
    Object o;
    Node next;
    }

    private Node first;
    
    public void add(Object elem) {
        Node node = new Node(elem);        
        if(first == null) {
            first = node;
        }
        else {
            append(node);
        }
    }
    
    private void append(Node node) {
        Node last = first;
        while(last.next != null) {
            last = last.next;
        }
        last.next = node;
    }
    
    public int size() {
        int count = 0;
        Node last = first;
        while(last != null) {
            last = last.next;
            count++;
        }
        return count;
    }
    
    public Object get(int index) {
        checkSize(index);
        return findElemOf(index);        
    }
    
    private void checkSize(int index) throws IndexOutOfBoundsException {
        int size = size();
        if(index >= size) {
            throw new IndexOutOfBoundsException(
                    String.format("Index: %d, Size: %d", index, size));
        }
    }
    
    private Object findElemOf(int index) {
        int count = 0;
        Node last = first;
        while(count < index) {
            last = last.next;
            count++;
        }
        return last.elem;
    }
    public static void main(String[] args) {
        SimpleLinkedList Sim=new SimpleLinkedList();
        System.out.println(Sim.size());
    }
    

    }

    这道题简化之后还是很好做的~但是让插入1再插2再将3插入两者之间我是真的不太会,但是简化之后还算轻松。

    写在最后

    其实这篇博客是我很早以前就有的想法,从上午检查完代码我就一直在完善这篇博客,当然这篇博客的初衷只是想看看自己有多少不会的题,但也确实没想到会落得个不及格的成绩。感觉很懊悔,当初没有足够重视这门课程,对当初的自己很生气!!现在的悔改看起来有点晚,但是我保证下学期您的课程我一定会好好学!这篇博客的补交也只是想证明一下有些东西是我会的,有些东西是我学到的。我已经知道了同学之间的差距,现在的学霸真的把我甩一条街...但我不会气馁的!在下个学期我会尽力去追赶!学习为本,勿忘本心。

  • 相关阅读:
    深入解析Hibernate核心接口
    Hibernate基本原理
    深入hibernate的三种状态
    Hibernate commit() 和flush() 的区别
    Hibernate中的merge使用详情解说
    Record is locked by another user
    Vue路由router-link的使用
    Vue-router的基本使用
    Vue把父组件的方法传递给子组件调用(评论列表例子)
    Vue中子组件调用父组件的方法
  • 原文地址:https://www.cnblogs.com/m20155310/p/6986976.html
Copyright © 2011-2022 走看看