Java语言程序设计 必实验3 接口回调及集合类应用

实验目的与要求:

    实验目的:熟悉集合类的应用,掌握接口的定义、实现类的编写和接口回调等技术。

实验要求:

(1). 张三、李四等人是A社团成员,李四、王五等人是B社团成员,编写一个Java应用程序(要求使用集合类),输出参加A社团的人、参加B社团的人、以及同时参加两个社团的人。在报告中附上程序截图、完整的运行结果和详细的文字说明。(10分)

(2). 有8个显示器,其属性有尺寸和价格。编写一个Java应用程序,使用TreeMap<K,V>,按照价格从大到小排序输出所有显示器的信息,要求通过两种方式实现:通过实现Comparator接口和通过实现Comparable接口。在报告中附上程序截图、完整的运行结果和详细的文字说明。(30分)

(3). 抽象类和接口的实验。(60分)

(3.1) 定义一个抽象类Human:包含一个成员变量String name;构造方法Human(String name),用于初始化姓名name;一个抽象方法double sayHello()。在报告中附上程序截图和详细的文字说明。(10分)

(3.2) 定义三个继承抽象类Human的类,分别命名为Chinese、Japanese和English,在这三个类中重写sayHello()方法,分别输出一句中文、日文和英文的问候;在报告中附上程序截图、运行结果和详细的文字说明。(10分)

(3.3) 定义一个测试类HumanTest:创建一个包含3个Human对象的数组,3个Human对象来自Chinese、Japanese和English类,循环调用该数组中的元素的sayHello()方法。在报告中附上程序截图、运行结果和详细的文字说明。(10分)

(3.4) 通过一个接口(命名为Human)和三个实现类(命名为Chinese、Japanese和English)来达到如上类似的效果。在报告中附上程序截图、运行结果和详细的文字说明。(30分)

(1). 张三、李四等人是A社团成员,李四、王五等人是B社团成员,编写一个Java应用程序(要求使用集合类),输出参加A社团的人、参加B社团的人、以及同时参加两个社团的人。在报告中附上程序截图、完整的运行结果和详细的文字说明。(10分)

设计:封装两个社团类,类中使用集合的向上转型来节约不必要的空间

package Hw6;

import java.util.*;

public class Association {
    public static void main(String[] args) {
        String[] A_members = {"Tom", "Cat", "Apache"};
        String[] B_members = {"Tom", "Nginx", "Apache"};
        A_association a_association = new A_association();
        B_association b_association = new B_association();
        a_association.setMembers(A_members);
        b_association.setMembers(B_members);
        System.out.println("参加A社团的人有:");
        System.out.println(a_association);
        System.out.println("参加A社团的人有:");
        System.out.println(b_association);
        System.out.println("参加AB社团的人有:");
        System.out.println(b_association.union(a_association));
    }
}

class A_association {

    private Set<String> members;

    A_association() {
        members = new HashSet<>();
    }

    public void setMembers(String... strings) {
        Collections.addAll(members, strings);
    }

    public Set<String> getMembers() {
        return members;
    }

    @Override
    public String toString() {
        return "A_association{" +
                members +
                '}';
    }
}

class B_association {
    private Set<String> members;

    B_association() {
        members = new HashSet<>();
    }

    public void setMembers(String... strings) {
        Collections.addAll(members, strings);
    }

    public String union(A_association a_association) {
        Set<String> res = new HashSet<>(this.members);
        res.retainAll(a_association.getMembers());
        return res.toString();
    }

    @Override
    public String toString() {
        return "B_association{" +
                members +
                '}';
    }
}

(2). 有8个显示器,其属性有尺寸和价格。编写一个Java应用程序,使用TreeMap<K,V>,按照价格从大到小排序输出所有显示器的信息,要求通过两种方式实现:通过实现Comparator接口和通过实现Comparable接口。在报告中附上程序截图、完整的运行结果和详细的文字说明。(30分)

设计:设计一个Monitor类用于实现Comparable接口,同时采用匿名类Lambda表达式重写TreeMaptoStringCompare方法

package Hw6;

import java.util.*;

//comparable方式实现
public class Monitor implements Comparable<Monitor> {
    static int cnt = 0;
    public double size;
    public double price;

    Monitor() {
        cnt++;
        this.size = cnt;
        this.price = cnt;
    }

    @Override
    public int compareTo(Monitor monitor) {
        return Double.compare(monitor.price, this.price);
    }
}

class Tester {
    public static void main(String[] args) {
        TreeMap<Double, Monitor> treeMap1 = new TreeMap<>() {
            @Override
            public String toString() {
                String temp = "";
                for (Monitor monitor : this.values()) {
                    temp += "(" + monitor.size + ", " + monitor.price + "), ";
                }
                return temp;
            }
        };
        //lambda表达式实现comparator的重写
        TreeMap<Double, Monitor> treeMap2 = new TreeMap<>((o1, o2) -> Double.compare(o2, o1)) {
            @Override
            public String toString() {
                String temp = "";
                for (Monitor monitor : this.values()) {
                    temp += "(" + monitor.size + ", " + monitor.price + "), ";
                }
                return temp;
            }
        };
        Monitor[] monitors = new Monitor[8];
        for (int i = 0; i < 8; ++i) {
            monitors[i] = new Monitor();
        }
        for (Monitor monitor : monitors) {
            treeMap1.put(monitor.price, monitor);
            treeMap2.put(monitor.price, monitor);
        }
        System.out.println("comparator方式");
        System.out.println("排序前:");
        System.out.println(treeMap1);
        System.out.println("排序后:");
        System.out.println(treeMap2);

        TreeMap<Monitor, Double> treeMap3 = new TreeMap<>() {
            @Override
            public String toString() {
                String temp = "";
                for (Monitor monitor : this.keySet()) {
                    temp += "(" + monitor.size + ", " + monitor.price + "), ";
                }
                return temp;
            }
        };
        for (Monitor monitor : monitors) {
            treeMap3.put(monitor, monitor.price);
        }
        System.out.println("comparable方式");
        System.out.println("排序前:");
        System.out.println(treeMap1);
        System.out.println("排序后:");
        System.out.println(treeMap3);
    }
}




(3). 抽象类和接口的实验。(60分)

(3.1) 定义一个抽象类Human:包含一个成员变量String name;构造方法Human(String name),用于初始化姓名name;一个抽象方法double sayHello()。在报告中附上程序截图和详细的文字说明。(10分)

(3.2) 定义三个继承抽象类Human的类,分别命名为Chinese、Japanese和English,在这三个类中重写sayHello()方法,分别输出一句中文、日文和英文的问候;在报告中附上程序截图、运行结果和详细的文字说明。(10分)

(3.3) 定义一个测试类HumanTest:创建一个包含3个Human对象的数组,3个Human对象来自Chinese、Japanese和English类,循环调用该数组中的元素的sayHello()方法。在报告中附上程序截图、运行结果和详细的文字说明。(10分)

(3.4) 通过一个接口(命名为Human)和三个实现类(命名为Chinese、Japanese和English)来达到如上类似的效果。在报告中附上程序截图、运行结果和详细的文字说明。(30分)

    设计:利用接口回调实现同样的效果

 

package Hw6;

public abstract class Human {
    String name;
    public Human(String name) {
        this.name = name;
    }
    abstract double sayHello();
}

class Chinese extends Human{

    public Chinese(String name) {
        super(name);
    }

    @Override
    double sayHello() {
        System.out.println("你好!");
        return 0;
    }
}

class Japanese extends Human{
    public Japanese(String name) {
        super(name);
    }

    @Override
    double sayHello() {
        System.out.println("こんにちは!");
        return 0;
    }
}

class English extends Human{
    public English(String name) {
        super(name);
    }

    @Override
    double sayHello() {
        System.out.println("Hello!");
        return 0;
    }
}

class Tester1{
    public static void main(String[] args) {
        Chinese chinese = new Chinese("李伟力");
        English english = new English("李伟力");
        Japanese japanese = new Japanese("李伟力");
        chinese.sayHello();
        english.sayHello();
        japanese.sayHello();
    }
}
package Hw6;

public class HumanTest {
    public static void main(String[] args) {
        Human[] humans = new Human[3];
        humans[0] = new Chinese("lwl");
        humans[1] = new English("lwl");
        humans[2] = new Japanese("lwl");
        for (Human human : humans) {
            human.sayHello();
        }
    }
}

 

package Hw6;

public class HumanInterface {
    interface Human1{
        double sayHello();
    }
    private static class Chinese implements Human1{
        public double sayHello() {
            System.out.println("你好!");
            return 0;
        }
    }

    private static class Japanese implements Human1{
        public double sayHello() {
            System.out.println("こんにちは!");
            return 0;
        }
    }

    private static class English implements Human1{

        public double sayHello() {
            System.out.println("Hello!");
            return 0;
        }
    }

    public static void main(String[] args) {
        Human1[] human1s = new Human1[3];
        human1s[0] = new Chinese();
        human1s[1] = new English();
        human1s[2] = new Japanese();
        for (Human1 human1 : human1s) {
            human1.sayHello();
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值