2020-08-03日报博客

2020-08-03日报博客

1.完成的事情:

  • 完成CodeGym Java核心level 12部分,并完成练习题

2.遇到的问题:

  • 接口的定义和使用。

3.明日计划:

  • 继续学习Java。
/*taskKey="zh.codegym.task.task12.task1233"\n\n同形体即将来临

编写返回数组的最小值及其位置(索引)的方法。


Requirements:
1.	Solution 类必须包含 Pair 类。
2.	Solution 类必须包含两个方法。
3.	Solution 类必须包含 getMinimumAndIndex() 方法。
4.	getMinimumAndIndex() 方法必须返回数组的最小值及其位置(索引)。




package zh.codegym.task.task12.task1233;*/

/* 
同形体即将来临
*/

public class Solution {

    public static void main(String[] args) {
        int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5};

        Pair<Integer, Integer> result = getMinimumAndIndex(data);

        System.out.println("最小值为 " + result.x);
        System.out.println("最小元素的索引为 " + result.y);
    }

    public static Pair<Integer, Integer> getMinimumAndIndex(int[] array) {
        if (array == null || array.length == 0) {
            return new Pair<Integer, Integer>(null, null);
        }

        int min = array[0],t = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] < min){
                min = array[i];
                t = i;
            }
        }


        return new Pair<Integer, Integer>(min, t);
    }

    public static class Pair<X, Y> {
        public X x;
        public Y y;

        public Pair(X x, Y y) {
            this.x = x;
            this.y = y;
        }
    }
}

/*taskKey="zh.codegym.task.task12.task1228"\n\nHuman 类的接口

在 Human 类中尽可能多添加接口,但要确保它不会成为一个抽象类。
你不能在 Human 类中添加方法。


Requirements:
1.	Solution 类必须包含具有 void workLazily() 方法的 Employee 接口。
2.	Solution 类必须包含具有 void workHard() 方法的 Businessman 接口。
3.	Solution 类必须包含具有 void workLazily() 方法的 Secretary 接口。
4.	Solution 类必须包含具有 void workVeryHard() 方法的 Miner 接口。
5.	Solution 类必须包含具有如下方法的 Human 类:void workHard() 和 void workLazily()。
6.	Human 类必须实现三个接口。*/

package zh.codegym.task.task12.task1228;

/* 
Human 类的接口
*/

public class Solution {
    public static void main(String[] args) {
        Human human = new Human();
        System.out.println(human);
    }

    public static interface Employee {
        public void workLazily();
    }

    public static interface Businessman {
        public void workHard();
    }

    public static interface Secretary {
        public void workLazily();
    }

    public static interface Miner {
        public void workVeryHard();
    }

    public static class Human implements Secretary,Businessman,Employee{

        public void workHard() {
        }

        public void workLazily() {
        }
    }
}
posted @ 2020-08-03 20:55  巩云龙  阅读(92)  评论(0编辑  收藏  举报