Skip to content

Commit 1ff614f

Browse files
committed
Day 10
1 parent 0fc1160 commit 1ff614f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1966
-4
lines changed

ch-21/.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

ch-21/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-21/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-21/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-21/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-21/ch-21.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

ch-21/src/CountKeywords.java

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1-
package PACKAGE_NAME;public class CountKeywords {
1+
import java.io.File;
2+
import java.io.FileNotFoundException;
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Scanner;
6+
import java.util.Set;
7+
8+
/**
9+
*
10+
* Case Study: Count Keywords
11+
*
12+
*/
13+
public class CountKeywords {
14+
public static void main(String[] args) {
15+
String filename = args[0];
16+
File file = new File(args[0]);
17+
18+
if (file.exists()) {
19+
try {
20+
int keywordCount = countKeywords(file);
21+
System.out.println("The number of keywords in " + filename + " is " + keywordCount);
22+
} catch (FileNotFoundException e) {
23+
System.out.println("Error reading the file: " + e.getMessage());
24+
}
25+
} else {
26+
System.out.println("File " + filename + " does not exist");
27+
}
28+
}
29+
30+
public static int countKeywords(File file) throws FileNotFoundException {
31+
String[] keywordString = {
32+
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
33+
"continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float",
34+
"for", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
35+
"new", "package", "private", "protected", "public", "return", "short", "static", "strictfp",
36+
"super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void",
37+
"volatile", "while", "true", "false", "null"
38+
};
39+
40+
Set<String> keywordSet = new HashSet<>(Arrays.asList(keywordString));
41+
int count = 0;
42+
43+
try (Scanner input = new Scanner(file)) {
44+
while (input.hasNext()) {
45+
String word = input.next();
46+
if (keywordSet.contains(word)) {
47+
count++;
48+
}
49+
}
50+
}
51+
52+
return count;
53+
}
254
}

ch-21/src/DisplayNonDuplicate.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
1+
import java.io.File;
2+
import java.io.FileNotFoundException;
3+
import java.util.Scanner;
4+
import java.util.TreeSet;
5+
6+
/**
7+
*
8+
* 21.2 (Display nonduplicate words in ascending order)
9+
10+
* Write a program that reads
11+
* words from a text file and displays all the nonduplicate words in ascending order.
12+
* The text file is passed as a command-line argument
13+
*
14+
*/
115
public class DisplayNonDuplicate {
16+
public static void main(String[] args) throws FileNotFoundException {
17+
File file = new File("C:\\Users\\Zig Zag\\Desktop\\java-fundamentals\\ch-21\\src\\file.txt");
18+
TreeSet<String> set = new TreeSet<>();
19+
20+
Scanner scanner = new Scanner(file);
21+
while(scanner.hasNext()) {
22+
String word = scanner.next();
23+
set.add(word);
24+
}
25+
26+
System.out.println(set);
27+
}
228
}

ch-21/src/GuessCapitalsGame.java

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
1-
package PACKAGE_NAME;public class GuessCapitalsGame {
1+
import com.sun.source.tree.Tree;
2+
3+
import java.util.*;
4+
5+
/**
6+
*
7+
* **21.9 (Guess the capitals using maps)
8+
9+
* Rewrite Programming Exercise 8.37 to store
10+
* pairs of each state and its capital in a map. Your program should prompt the user
11+
* to enter a state and should display the capital for the state
12+
*
13+
*/
14+
public class GuessCapitalsGame {
15+
public static void main(String[] args) {
16+
TreeMap<String, String> statesAndCapitals = new TreeMap<>() {
17+
{
18+
put("California", "Sacramento");
19+
put("Montana", "Helena");
20+
put("Idaho", "Boise");
21+
put("Texas", "Austin");
22+
put("Oregon", "Salem");
23+
put("Washington", "Olympia");
24+
put("Florida", "Tallahassee");
25+
put("Oklahoma", "Oklahoma City");
26+
put("New York", "Albany");
27+
put("Ohio", "Columbus");
28+
}
29+
};
30+
31+
List<String> statesList = new ArrayList<>(statesAndCapitals.keySet());
32+
String randomState = statesList.get(new Random().nextInt(statesList.size()));
33+
String randomCapital = statesAndCapitals.get(randomState);
34+
Scanner scanner = new Scanner(System.in);
35+
System.out.println("Enter the capital of: " + randomState);
36+
String answer = scanner.nextLine();
37+
38+
if(answer.equalsIgnoreCase(randomCapital)) {
39+
System.out.println("Correct !");
40+
System.out.println("The capital of " + randomState + " is " + randomCapital);
41+
scanner.close();
42+
return;
43+
}
44+
45+
System.out.println("Incorrect !");
46+
System.out.println("The capital of " + randomState + " is " + randomCapital);
47+
scanner.close();
48+
}
249
}

ch-21/src/NumberOfOccurrences.java

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
1-
package PACKAGE_NAME;public class NumberOfOccurrences {
1+
import java.util.*;
2+
3+
/**
4+
*
5+
* *21.6 (Count the occurrences of numbers entered)
6+
7+
* Write a program that reads an
8+
* unspecified number of integers and finds the one that has the most occurrences.
9+
* The input ends when the input is 0. For example, if you entered 2 3 40 3 5 4 –3
10+
* 3320, the number 3 occurred most often. If not one but several numbers have
11+
* the most occurrences, all of them should be reported. For example, since 9 and 3
12+
* appear twice in the list 9 30 3 9 3 2 4, both occurrences should be reported.
13+
*
14+
*/
15+
public class NumberOfOccurrences {
16+
public static void main(String[] args) {
17+
Scanner scanner = new Scanner(System.in);
18+
System.out.println("Enter numbers (input ends with 0): ");
19+
Map<Integer, Integer> hashMap = new HashMap<>();
20+
21+
while (scanner.hasNextInt()) {
22+
int num = scanner.nextInt();
23+
24+
if(num == 0) {
25+
break;
26+
}
27+
28+
if(!hashMap.containsKey(num)) {
29+
hashMap.put(num, 1);
30+
continue;
31+
}
32+
33+
int currValue = hashMap.get(num);
34+
currValue++;
35+
hashMap.put(num, currValue);
36+
}
37+
38+
HashMap<Integer, Integer> mostRepeated = new HashMap<>();
39+
Map.Entry<Integer, Integer> current = new AbstractMap.SimpleEntry<Integer, Integer>(0,0);;
40+
41+
for(Map.Entry<Integer, Integer> o : hashMap.entrySet()) {
42+
if (o.getValue() > current.getValue()) {
43+
current = o;
44+
mostRepeated.clear();
45+
continue;
46+
}
47+
48+
if (Objects.equals(o.getValue(), current.getValue())){
49+
mostRepeated.put(o.getKey(), o.getValue());
50+
}
51+
}
52+
53+
mostRepeated.put(current.getKey(), current.getValue());
54+
55+
System.out.println(hashMap);
56+
System.out.println("Top repeated numbers: ");
57+
System.out.println(mostRepeated);
58+
}
259
}

ch-21/src/file.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
mosaic
2+
ambiguity
3+
palace
4+
visit
5+
congress
6+
hold
7+
copper
8+
suntan
9+
bowel
10+
photograph
11+
whisper
12+
stage
13+
premature
14+
morsel
15+
mist
16+
researcher
17+
pedestrian
18+
condition
19+
notorious
20+
sign
21+
touch
22+
problem
23+
pleasant
24+
bucket
25+
separate
26+
restless
27+
wake
28+
sum
29+
favourite
30+
performer
31+
aid
32+
doubt
33+
offense
34+
immune
35+
tasty
36+
literature
37+
white
38+
cherry
39+
relation
40+
rotten
41+
origin
42+
surprise
43+
bake
44+
eyebrow
45+
brave
46+
tray
47+
threat
48+
import
49+
due
50+
trend

ch-22/PrimeNumbers.dat

40.9 MB
Binary file not shown.

ch-22/src/PrimeNumbers.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import java.io.*;
2-
import java.math.BigInteger;
32
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.Scanner;
@@ -55,6 +54,7 @@ public static List<Long> loadStoredPrimes() {
5554
}
5655
} catch (FileNotFoundException ex) {
5756
System.out.println("File was not found");
57+
primes.add(1L);
5858
}
5959
return primes;
6060
}

ch-24/.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

ch-24/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-24/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-24/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)