android-nuc / 17_Java_Train

17级 Android 实验室 Java 培训
12 stars 2 forks source link

1614010405乔丽萍 #33

Open qlpdream opened 6 years ago

qlpdream commented 6 years ago

public class text {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int a[] = new int[10];
        System.out.println("请输入10个数字:");
        for(int i = 0;i < 10;i++){
           a[i] = scanner.nextInt();
        }
        Factory factory = new Factory();
        BaseSort select_sort = new SelectSort();
        factory.seteSort(select_sort);
        factory.doSort(a);
        BaseSort insert_sort = new InsertSort();
        factory.seteSort(insert_sort);
        factory.doSort(a);
        BaseSort quick_sort = new QuickSort();
        factory.seteSort(quick_sort);
        factory.doSort(a);
    }
}
public class BaseSort {
    public void sort(int a[]){
        System.out.println("排序算法");
    }
}
package qlp_code;

public class Factory {
    private BaseSort sort;
    public void seteSort(BaseSort sort){
        this.sort = sort;
    }
    public void doSort(int []a){
        sort.sort(a);
    }
}
package qlp_code;

public class InsertSort extends BaseSort{
    public void sort(int a[]){

        System.out.println("插入排序");

          int temp,i,j;
          for(j=1;j<a.length;j++) { //插入排序方法
           temp = a[j];
           i = j;
           while(i > 0 && a[i-1] >= temp) {
            a[i] = a[i-1];
            i--;
           }
           a[i] = temp;
          }
          for(i = 0;i < a.length;i ++)
              System.out.print(a[i]+" ");
          System.out.println();
    }
}
package qlp_code;

public class QuickSort extends BaseSort{
    public void sort(int a[]){
        System.out.println("快速排序");
        qsort(a,0,a.length-1);
        for(int i = 0;i < a.length;i ++)
              System.out.print(a[i]+" ");
        System.out.println();
    }
    public static void qsort(int a[], int low, int hight) {
        int i, j, index;
        if (low > hight) {
            return;
        }
        i = low;
        j = hight;
        index = a[i]; // 用子表的第一个记录做基准
        while (i < j) { // 从表的两端交替向中间扫描
            while (i < j && a[j] >= index)
                j--;
            if (i < j)
                a[i++] = a[j];// 用比基准小的记录替换低位记录
            while (i < j && a[i] < index)
                i++;
            if (i < j) // 用比基准大的记录替换高位记录
                a[j--] = a[i];
        }
        a[i] = index;// 将基准数值替换回 a[i]
        qsort(a, low, i - 1); // 对低子表进行递归排序
        qsort(a, i + 1, hight); // 对高子表进行递归排序
    }

}
package qlp_code;

public class SelectSort extends BaseSort{
    public void sort(int a[]){
        super.sort(a);
        System.out.println("选择排序");
        int minIndex=0;
        int temp=0;
        if((a==null)||(a.length==0))
            return;
        for(int i=0;i<a.length-1;i++)
        {
            minIndex=i;//无序区的最小数据数组下标
            for(int j=i+1;j<a.length;j++)
            {
                //在无序区中找到最小数据并保存其数组下标
                if(a[j]<a[minIndex])
                {
                    minIndex=j;
                }
            }
            if(minIndex!=i)
            {
                //如果不是无序区的最小值位置不是默认的第一个数据,则交换之。
                temp=a[i];
                a[i]=a[minIndex];
                a[minIndex]=temp;
            }
        }
        for(int i = 0;i < a.length;i ++)
              System.out.print(a[i]+" ");
        System.out.println();
    }

}
```java
DreamYHD commented 6 years ago

末尾只需要 ``` 不需要java

qlpdream commented 6 years ago

第二次作业

简单工厂模式

工厂模式就是建立一个工厂类。到底要实例化谁,将来会不会增加实例化对象,考虑用一个单独的类来做这个创造实例的过程,这就是工厂。

Operation运算类

public class Operation {
    private double _numberA = 0;
    private double _numberB = 0;

    public double get_numberA(){
        return _numberA;
    }

    public void set_numberA(double _numberA) {
        this._numberA = _numberA;
    }

    public double get_numberB() {
        return _numberB;
    }

    public void set_numberB(double _numberB) {
        this._numberB = _numberB;
    }

    public double GetResult()  {
        double result = 0;
        return result;
    }

}

加减乘除类

//创建不同的类实现低耦合
public class OperationAdd extends Operation{
    public double GetResult(){
        double result = 0;
        result = get_numberA() +get_numberB();
        return result;
    }
}
public class OperationSub extends Operation {
    public double GetResult(){
        double result = 0;
        result =get_numberA() - get_numberB();
        return result;
    }
}
public class OperationMul extends Operation {
    public double GetResult(){
        double result = 0;
        result = get_numberA() *get_numberB();
        return result;
    }
}
public class OperationDiv extends Operation {
    public double GetResult() {
        double result = 0;
        if(get_numberB()==0){
            try
            {
                throw new Exception("除数不能为零。");
            }catch (Exception e){
                System.out.println(e);
            }

        }
        result = get_numberA()/get_numberB();
        return result;
    }
}

简单运算工厂类

//只需要输入运算符号,工厂就实例化出合适的对象,通过多态,返回父类的方法实现了计算器的结果
public class OperationFactory {
    public static Operation createOperate(String operate)
    {
        Operation oper = null;
        switch (operate)
        {
            case "+":
                oper = new OperationAdd();
                break;
            case "-":
                oper = new OperationSub();
                break;
            case "*":
                oper = new OperationMul();
                break;
            case "/":
                oper = new OperationDiv();
                break;
        }
        return oper;
    }
}

客户端代码

public class text {
    public static void main(String[] args){
        Operation oper;
        oper = OperationFactory.createOperate("+");
        oper.set_numberA(1) ;
        oper.set_numberB(2);
        double result = oper.GetResult();
        System.out.println(result);
    }
}

Java 8 之后有了接口为什么还要用抽象类?

    java8引入了函数式接口,可以在接口当中提供默认的default关键字修饰的方法实现,和抽象类界限不明显了。但是抽象类和接口还存在着差别:1)类可以实现无限个接口,但仅能从一个抽象(或任何其他类型)类继承,从抽象类派生的类仍可实现接口,从而得出接口是用来解决多重继承问题的。2)抽象类当中可以存在非抽象的方法使子类实现相同的方法,接口不能它里面的方法必须用public修饰的声明没有具体实现方法。3)抽象类中的成员变量可以被不同的修饰符来修饰,接口中默认的都是静态常量。4)抽象类是对象的抽象,接口是一种行为规范。
qlpdream commented 6 years ago

第三次作业

import java.util.*;
import java.util.stream.Collectors;

public class text {
    public static void main(String[] args){
        int i;
        List<String> a = new ArrayList<String>();
        a.add("Hello");
        a.add("World");
        a.add("I");
        a.add("am");
        a.add("coming");
        //for循环输出
        System.out.println("for循环输出");
        for( i = 0;i < a.size();i ++)
        {
            System.out.println(a.get(i));
        }
        //while循环输出
        System.out.println("\nwhile循环输出");
        i = 0;
        while(i<a.size()){
            System.out.println(a.get(i));
            i++;
        }
        //foreach循环输出
        System.out.println("\nforeach循环输出");
        for (String x: a
             ) {
            System.out.println(x);
        }
        //stream流输出
        System.out.println("\nstream流输出");
        List<String> s = a.stream().collect(Collectors.toList());
        System.out.println(s);
    }
}

输出结果

for循环输出 Hello World I am coming

while循环输出 Hello World I am coming

foreach循环输出 Hello World I am coming

stream流输出 [Hello, World, I, am, coming]