《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());
}
}
}