《Java语言程序设计与数据结构》编程练习答案(第二十一章)(一)

《Java语言程序设计与数据结构》编程练习答案(第二十一章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

21.1

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        LinkedHashSet<String> set1 = new LinkedHashSet<>();
        LinkedHashSet<String> set2 = new LinkedHashSet<>();

        set1.add("George");
        set1.add("Jim");
        set1.add("John");
        set1.add("Blake");
        set1.add("Kevin");
        set1.add("Michael");
        set2.add("George");
        set2.add("Katie");
        set2.add("Kevin");
        set2.add("Michelle");
        set2.add("Ryan");

        LinkedHashSet<String> union = new LinkedHashSet<>(set1);
        for(String str: set2){
            if(!set1.contains(str)){
                union.add(str);
            }
        }

        LinkedHashSet<String> intersect = new LinkedHashSet<>();
        for(String str : set1){
            if(set2.contains(str)){
                intersect.add(str);
            }
        }

        LinkedHashSet<String> diff = new LinkedHashSet<>();
        for(String str : set1){
            if(!set2.contains(str)){
                diff.add(str);
            }
        }

        System.out.println(Arrays.toString(union.toArray()));
        System.out.println(Arrays.toString(intersect.toArray()));
        System.out.println(Arrays.toString(diff.toArray()));
    }


}

21.2

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        LinkedHashSet<String> words = new LinkedHashSet<>();
        System.out.println("Please enter words");
        while(true){
            String tmp = input.nextLine();
            if(tmp.equals("&&")){
                System.out.println("Input finish");
                break;
            }
            else{
                words.add(tmp);
            }
        }

        System.out.println(Arrays.toString(words.toArray()));
    }

}

21.3

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        LinkedHashSet<String> keywords = new LinkedHashSet<>();
        keywords.add("int");
        keywords.add("double");
        keywords.add("char");
        keywords.add("float");
        keywords.add("String");
        System.out.println("Please enter the code");

        boolean valid = true;
        int count = 0;
        while(input.hasNext()){
            String tmp = input.next();
            if(tmp.equals("//")){
                valid = false;
            }else if(tmp.equals("\"")){
                valid = !valid;
            }else if(tmp.equals("\n")){
                valid = true;
            }else{
                if(keywords.contains(tmp)){
                    count++;
                }
            }
            if(tmp.equals("**")){
                break;
            }
        }

        System.out.println("The number of key words is "+count);
    }
}

21.4

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the xxx");
        String xxx = input.nextLine();

        LinkedHashSet<Character> characters = new LinkedHashSet<>();
        characters.add('A');
        characters.add('E');
        characters.add('I');
        characters.add('O');
        characters.add('U');
        characters.add('a');
        characters.add('e');
        characters.add('i');
        characters.add('o');
        characters.add('u');

        int yCount = 0;
        int fCount = 0;
        for(int i=0;i<xxx.length();i++){
            if(characters.contains(xxx.charAt(i))){
                yCount++;
            }else{
                fCount++;
            }
        }

        System.out.println("元音:"+yCount);
        System.out.println("辅音:"+fCount);
    }
}

21.5

🐎

21.6

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        HashMap<Integer, Integer> map = new HashMap<>();
        System.out.println("Please enter ints: ");
        while(true){
            int tmp = input.nextInt();
            if(tmp == 0){
                break;
            }else{
                if(!map.containsKey(tmp)){
                    map.put(tmp,1);
                }else{
                    int num = map.get(tmp);
                    num++;
                    map.put(tmp, num);
                }
            }
        }

        int max = 0;
        for (int value: map.values()){
            if(value > max){
                max = value;
            }
        }
        ArrayList<Integer> maxInts = new ArrayList<>();
        for(Map.Entry<Integer, Integer> entry: map.entrySet()){
            if(entry.getValue() == max){
                maxInts.add(entry.getKey());
            }
        }

        System.out.println(Arrays.toString(maxInts.toArray()));
    }
}

21.7

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String txt = "Good morning. Have a good class. "+
                "Have a good visit. Have fun!";

        HashMap<String, Integer> map = new HashMap<>();
        String[] words = txt.split("[\\s+\\p{P}]");
        for (String word : words) {
            String key = word.toLowerCase();
            if (key.length() > 0) {
                if (!map.containsKey(key)) {
                    map.put(key, 1);
                } else {
                    int occur = map.get(key);
                    occur++;
                    map.put(key, occur);
                }
            }
        }

        ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
        Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        };

        entries.sort(comparator);
        for(Map.Entry<String, Integer> entry : entries){
            System.out.println(entry.getKey()+"\t"+entry.getValue());
        }
    }
}
  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值