Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit add6448

Browse files
Chap 21, Ex: 14,15.
1 parent 20da60e commit add6448

File tree

4 files changed

+146
-12
lines changed

4 files changed

+146
-12
lines changed

‎ch_07/Exercise07_02.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static void main(String[] args) {
1313

1414
Scanner input = new Scanner(System.in);
1515
System.out.println("Enter ten space-separated integers: ");
16+
1617
int[] arr = new int[10];
1718

1819
for (int i = 0; i < 10; i++) {

‎ch_08/Exercise08_36.java‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static void main(String[] args) {
3939
char[][] chars = new char[numRows][numRows];
4040

4141
int upperBoundChar = minChar + numRows - 1; //Upper Bound for the range from 'A' -> ('A' + userInput)
42-
4342
System.out.println("\nEnter " + numRows + " rows of capital letters separated by spaces: ");
4443
try {
4544
for (int r = 0; r < chars.length; r++) {
@@ -54,7 +53,7 @@ public static void main(String[] args) {
5453
} catch (Exception e) {
5554
e.printStackTrace();
5655
System.out.println(e.getLocalizedMessage());
57-
notALSq();
56+
System.out.println("The input array is not a Latin Square.");
5857
System.exit(0);
5958
}
6059

@@ -78,7 +77,7 @@ public static void main(String[] args) {
7877

7978
} else if (rowTracker.size() != 0) {
8079
if (rowTracker.contains(charValue)) {
81-
notALSq();
80+
System.out.println("The input array is not a Latin Square.");
8281
result = false;
8382
break ROW;
8483
}
@@ -99,7 +98,7 @@ public static void main(String[] args) {
9998

10099
if (colTracker.size() > 0) {
101100
if (colTracker.contains(value)) {
102-
notALSq();
101+
System.out.println("The input array is not a Latin Square.");
103102
break OUTER;
104103
}
105104
}
@@ -109,12 +108,4 @@ public static void main(String[] args) {
109108
}
110109
in.close();
111110
}
112-
113-
static void notALSq() {
114-
System.out.println("The input array is not a Latin Square.");
115-
}
116-
117-
static void aLsq() {
118-
System.out.println("The input array is a Latin Square.");
119-
}
120111
}

‎ch_21/Exercise21_14.java‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package ch_21;
2+
3+
import java.net.URL;
4+
import java.util.*;
5+
6+
/**
7+
* *21.14 (Web crawler) Rewrite Listing 12.18, WebCrawler.java,
8+
* to improve the performance by using appropriate
9+
* new data structures for listOfPendingURLs and listofTraversedURLs.
10+
*/
11+
public class Exercise21_14 {
12+
public static void main(String[] args) {
13+
java.util.Scanner input = new java.util.Scanner(System.in);
14+
System.out.print("Enter a URL: ");
15+
String url = input.nextLine();
16+
crawler(url); // Traverse the Web from the a starting url
17+
}
18+
19+
public static void crawler(String startingURL) {
20+
/* Exercise 21.14
21+
...improve the performance by using appropriate new data structures for listOfPendingURLs and
22+
listofTraversedURLs.
23+
*/
24+
TreeSet<CrawlUrl> listOfPendingURLs = new TreeSet<>();
25+
HashSet<CrawlUrl> listOfTraversedURLs = new HashSet<>();
26+
listOfPendingURLs.add(new CrawlUrl(startingURL));
27+
while (!listOfPendingURLs.isEmpty() &&
28+
listOfTraversedURLs.size() <= 100) {
29+
CrawlUrl crawUrl = listOfPendingURLs.first();
30+
listOfPendingURLs.remove(crawUrl);
31+
if (!listOfTraversedURLs.contains(crawUrl)) {
32+
listOfTraversedURLs.add(crawUrl);
33+
System.out.println("Crawl: " + crawUrl);
34+
for (CrawlUrl crawlUrl : getSubURLs(crawUrl.getUrlString())) {
35+
if (!listOfTraversedURLs.contains(crawlUrl))
36+
listOfPendingURLs.add(crawlUrl);
37+
}
38+
}
39+
}
40+
}
41+
42+
public static Set<CrawlUrl> getSubURLs(String urlString) {
43+
Set<CrawlUrl> set = new HashSet<>(); // Exercise 21.14
44+
try {
45+
URL url = new URL(urlString);
46+
Scanner input = new Scanner(url.openStream());
47+
int current = 0;
48+
while (input.hasNext()) {
49+
String line = input.nextLine();
50+
current = line.indexOf("http:", current);
51+
while (current > 0) {
52+
int endIndex = line.indexOf("\"", current);
53+
if (endIndex > 0) { // Ensure that a correct URL is found
54+
set.add(new CrawlUrl(line.substring(current, endIndex)));
55+
current = line.indexOf("http:", endIndex);
56+
} else
57+
current = -1;
58+
}
59+
}
60+
} catch (Exception ex) {
61+
System.out.println("Error: " + ex.getMessage());
62+
}
63+
return set;
64+
}
65+
}
66+
67+
/**
68+
* Implements Comparable for default sorting when used in a TreeSet
69+
*/
70+
class CrawlUrl implements Comparable<CrawlUrl> {
71+
private String urlString;
72+
73+
public CrawlUrl(String urlString) {
74+
this.urlString = urlString;
75+
}
76+
77+
@Override
78+
public int compareTo(CrawlUrl that) {
79+
// Implement to sort by the length of the url
80+
return Integer.compare(this.urlString.length(), that.urlString.length());
81+
}
82+
83+
public String getUrlString() {
84+
return urlString;
85+
}
86+
87+
public CrawlUrl setUrlString(String urlString) {
88+
this.urlString = urlString;
89+
return this;
90+
}
91+
92+
93+
@Override
94+
public String toString() {
95+
return "CrawlUrl{" +
96+
"urlString='" + urlString + '\'' +
97+
'}';
98+
}
99+
}

‎ch_21/Exercise21_15.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ch_21;
2+
3+
import java.util.HashSet;
4+
import java.util.Scanner;
5+
import java.util.Set;
6+
7+
/**
8+
* *21.15 (Addition quiz) Rewrite Programming Exercise 11.16 to store the answers in a
9+
* set rather than a list.
10+
* <p>
11+
* {@linkplain ch_11.Exercise11_16}
12+
*/
13+
public class Exercise21_15 {
14+
public static void main(String[] args) {
15+
Set<Integer> guesses = new HashSet<>();
16+
17+
Scanner input = new Scanner(System.in);
18+
19+
int number1 = (int) (Math.random() * 10);
20+
int number2 = (int) (Math.random() * 10);
21+
22+
System.out.print("What is " + number1 + " + " + number2 + "? ");
23+
int answer = input.nextInt();
24+
25+
while (answer != number1 + number2) {
26+
27+
if (guesses.contains(answer)) {
28+
System.out.println("You've already entered " + answer);
29+
System.out.print("Try again. What is " + number1 + " + " + number2 + "? ");
30+
answer = input.nextInt();
31+
32+
} else {
33+
guesses.add(answer);
34+
System.out.print("Wrong answer. Try again. What is " + number1 + " + " + number2 + "? ");
35+
answer = input.nextInt();
36+
}
37+
38+
}
39+
40+
input.close();
41+
System.out.println("You got it!");
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /