![cover](/upload/Java%20高级程序设计.jpg)
Java 高级程序设计:作业 08
Java 高级程序设计:实验八 规则集和映射
代码地址:Github
实验目的和要求
-
能够使用HashSet、LinkedHashSet或TreeSet来存储元素;
-
区分Collection与Map,能够理由合适的类来存储带键值的值。
实验内容
-
按升序显示不重复的单词,编写一个程序,从控制台读取英文段落,以新的一行
****
作为结束行,将所有不重复的单词按升序显示。 -
读取个数不定的整数,查找其中出现频率最高的数字,当输入为
0
时,表示输入结束。如果输入的频率最高的数字不是一个,而是多个,则把多个数字都显示出来。 -
统计 java 源代码中的关键字。如果关键字在注释或者字符串中,则不统计。将java源代码从控制台输入,以新的一行
****
作为结束行。String[] keywordString = {"abstract", "finally", "public", "boolean", "float", "return", "break", "for", "short", "byte", "goto", "static", "case", "if", "super", "catch", "implements", "switch", "char", "import", "synchronized", "class", "instanceof", "this", "const", "int", "throw", "continue", "interface", "throws", "default", "long", "transient", "do", "native", "try", "double", "new", "void", "else", "package","volatile", "extends", "private", "while", "final", "protected", "true", "null"};
实验结果
Answer01
package com.owem.answer01;
import java.util.*;
/**
* @author Owem
* @date 2022/11/13 16:13
* @description TODO
**/
public class CountWords {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder sentences = new StringBuilder();
while (true) {
String s = scanner.nextLine();
if (s.equals("****")) {
break;
} else {
sentences.append(s);
}
}
Map<String, Integer> wordsHashMap = new HashMap<>();
for (String s : sentences.toString().split("[,.; !]")) {
int v = 1;
if (wordsHashMap.containsKey(s)) {
v += wordsHashMap.get(s);
}
wordsHashMap.put(s, v);
}
wordsHashMap.remove(" ");
wordsHashMap.remove("");
List<Map.Entry<String, Integer>> list = new ArrayList<>(wordsHashMap.entrySet());
list.sort(Map.Entry.comparingByKey());
for (Map.Entry<String, Integer> entry : list) {
System.out.println(entry.getKey());
}
}
}
![image-20221113161502192](https://owen-resource.oss-cn-hangzhou.aliyuncs.com/images/image-20221113161502192.png)
Answer02
package com.owem.answer02;
import java.util.*;
/**
* @author Owem
* @date 2022/11/13 16:15
* @description TODO
**/
public class CountNumberMaxFrequency {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> numbers = new ArrayList<>();
while (true) {
int i = Integer.parseInt(scanner.next());
if (i == 0) {
break;
} else {
numbers.add(i);
}
}
Map<Integer, Integer> numbersHashMap = new HashMap<>();
for (Integer i : numbers) {
int v = 1;
if (numbersHashMap.containsKey(i)) {
v += numbersHashMap.get(i);
}
numbersHashMap.put(i, v);
}
int maxNumber = (int)getMaxValue(numbersHashMap);
List<Integer> outputList = new ArrayList<>();
Set<Integer> keySet = numbersHashMap.keySet();
for (Integer key : keySet) {
if (maxNumber == numbersHashMap.get(key)) {
outputList.add(key);
}
}
outputList.sort(Comparator.comparingInt(o -> o));
for (Integer key : outputList) {
System.out.println(key);
}
}
public static Object getMaxValue(Map<Integer, Integer> map) {
if (map == null)
return null;
int length =map.size();
Collection<Integer> c = map.values();
Object[] obj = c.toArray();
Arrays.sort(obj);
return obj[length-1];
}
}
![image-20221113161727693](https://owen-resource.oss-cn-hangzhou.aliyuncs.com/images/image-20221113161727693.png)
Answer03
package com.owem.answer03;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
/**
* @author Owem
* @date 2022/11/13 16:19
* @description TODO
**/
public class CountKeyWords {
private static final HashMap<String, Integer> records = new HashMap<>();
static {
getKeywords();
}
/**
* 获取关键字
*/
private static void getKeywords() {
String[] keywordString = {"abstract", "finally", "public", "boolean", "float", "return", "break", "for",
"short", "byte", "goto", "static", "case", "if", "super", "catch", "implements", "switch", "char",
"import", "synchronized", "class", "instanceof", "this", "const", "int", "throw", "continue",
"interface", "throws", "default", "long", "transient", "do", "native", "try", "double", "new", "void",
"else", "package", "volatile", "extends", "private", "while", "final", "protected", "true", "null"};
for (String string : keywordString) {
records.put(string, 0);
}
}
public static void main(String[] args) {
String path = new Scanner(System.in).nextLine();
File file = new File(path);
if (!file.exists()) {
System.err.println("File or directory does not exist!");
System.exit(0);
} else {
checkAndCountFromFile(path);
}
Set<String> keywordsSet = records.keySet();
for (String keyword : keywordsSet) {
System.out.println(keyword + " : " + records.get(keyword));
}
}
/**
* 统计.java文件中各个关键字的个数
*/
private static void checkAndCountFromFile(String path) {
File[] files;
File file = new File(path);
//若是目录则获取目录下的所有子文件或子目录的绝对路径递归调用该方法,对.java文件和子目录中.java进行统计
if (file.isDirectory()) {
files = file.listFiles();
assert files != null;
for (File value : files) {
checkAndCountFromFile(value.getAbsolutePath());
}
} else if (file.isFile()) { //若是.java文件则直接统计
if (file.getName().endsWith(".java")) {
try (Scanner scanner = new Scanner(file)) {
boolean isAdd = true;
while (scanner.hasNextLine()) {
isAdd = checkAndCountFromStr(scanner.nextLine(), isAdd);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
/**
* 检查从Java源文件中逐个读取的字符串并且对关键字逐个统计数目
*/
private static boolean checkAndCountFromStr(String string, boolean isAdd) {
String subString[] = string.split(" ");
for (String s : subString) {
if (s.equals("/**")) return false;
if (s.equals("**/")) return true;
if (s.startsWith("\"")) return false;
if (s.endsWith("\"")) return true;
if (s.equals("//")) return isAdd;
if (isAdd) {
if (records.containsKey(s)) {
records.put(s, records.get(s) + 1);
}
switch (s) {
case "(true)" -> records.put("true", records.get("true") + 1);
case "break;" -> records.put("break", records.get("break") + 1);
case "continue;" -> records.put("continue", records.get("continue") + 1);
}
if (string.length() > 6) {
if (string.startsWith("super.")) records.put("super", records.get("super") + 1);
if (string.startsWith("this.")) records.put("this", records.get("super") + 1);
}
}
}
return isAdd;
}
}
![image-20221113164617052](https://owen-resource.oss-cn-hangzhou.aliyuncs.com/images/image-20221113164617052.png)
test.java
package com.owem.answer03;
/**
* @author Owem
* @date 2022/11/13 16:31
* @description TODO
* synchronized
**/
public class test {
public static void main(String[] args) {
System.out.println(" synchronized");
}
}
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Owen
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果