Skip to content

Commit 89fcbf1

Browse files
authored
Merge pull request #83 from HarryDulaney/chapter-16-solutions
Chapter 16 solution Ex 11
2 parents 6759ed6 + 7f1bb6f commit 89fcbf1

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Indicates 100% completion of all exercises for that chapter
166166
16</strong></a> - JavaFx UI Controls and Multimedia
167167

168168
<h6>
169-
Exercises Needed: 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31
169+
Exercises Needed: 13, 15, 17, 19, 21, 23, 25, 27, 29, 31
170170
</h6>
171171
</li><br>
172172
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_17"><strong>Chapter
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ch_16.exercise16_11;
2+
3+
import javafx.scene.Scene;
4+
import javafx.scene.control.Button;
5+
import javafx.scene.control.Label;
6+
import javafx.scene.control.TextField;
7+
import javafx.scene.layout.VBox;
8+
import javafx.stage.Stage;
9+
10+
import java.io.File;
11+
12+
/**
13+
* **16.11 (Create a histogram for occurrences of letters) Write a program that reads a
14+
* file and displays a histogram to show the occurrences of each letter in the file,
15+
* as shown in Figure 16.40b. The file name is entered from a text field. Pressing
16+
* the Enter key on the text field causes the program to start to read and process
17+
* the file and displays the histogram. The histogram is displayed in the center of the
18+
* window. Define a class named Histogram that extends Pane. The class contains the property counts that is an array
19+
* of 26 elements. counts[0] stores the
20+
* number of A, counts[1] the number of B, and so on. The class also contains a
21+
* setter method for setting a new counts and displaying the histogram for the new
22+
* counts.
23+
* <p>
24+
* Example Filename input:
25+
* C:\Users\Harry\IdeaProjects\intro-to-java-programming\ch_16\exercise16_11\testFile.txt
26+
* (Right-click on the testFile.txt and choose 'copy absolute path')
27+
*/
28+
public class Exercise16_11 extends javafx.application.Application {
29+
private int[] counts = new int[26];
30+
31+
@Override
32+
public void start(Stage primaryStage) throws Exception {
33+
Histogram histogram = new Histogram(new int[26]);
34+
Label label = new Label("Filename:");
35+
TextField fileInputField = new TextField();
36+
Button viewHistogramButton = new Button("View");
37+
VBox parentBox = new VBox();
38+
double WIDTH = 800;
39+
histogram.setPrefWidth(WIDTH);
40+
double HEIGHT = 500;
41+
histogram.setPrefHeight(HEIGHT);
42+
parentBox.setPrefWidth(WIDTH);
43+
parentBox.setPrefHeight(HEIGHT);
44+
VBox.setVgrow(histogram, javafx.scene.layout.Priority.ALWAYS);
45+
parentBox.getChildren().add(histogram);
46+
parentBox.getChildren().addAll(label, fileInputField, viewHistogramButton);
47+
viewHistogramButton.setOnMousePressed(event -> {
48+
String filePath = fileInputField.getText();
49+
if (filePath.isEmpty()) {
50+
return;
51+
}
52+
File file = new File(filePath);
53+
if (!file.exists()) {
54+
System.out.println("File does not exist");
55+
return;
56+
}
57+
histogram.setCounts(countLetterFromFile(file, counts));
58+
});
59+
Scene scene = new Scene(parentBox, WIDTH, HEIGHT);
60+
primaryStage.setResizable(false);
61+
primaryStage.setScene(scene);
62+
primaryStage.show();
63+
}
64+
65+
public static int[] countLetterFromFile(File file, int[] counts) {
66+
String s = "";
67+
try (java.util.Scanner input = new java.util.Scanner(file)) {
68+
while (input.hasNext()) {
69+
s = input.nextLine();
70+
for (int i = 0; i < s.length(); i++) {
71+
char c = s.charAt(i);
72+
if (Character.isLetter(c)) {
73+
int index = Character.toUpperCase(c) - 'A';
74+
counts[index]++;
75+
}
76+
}
77+
}
78+
} catch (Exception e) {
79+
System.out.println("Error reading file");
80+
System.out.println("Error Message: " + e.getMessage());
81+
}
82+
83+
return counts;
84+
}
85+
86+
87+
}

ch_16/exercise16_11/Histogram.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ch_16.exercise16_11;
2+
3+
import javafx.scene.layout.Pane;
4+
import javafx.scene.paint.Color;
5+
import javafx.scene.shape.Rectangle;
6+
import javafx.scene.text.Text;
7+
8+
public class Histogram extends Pane {
9+
private int[] counts;
10+
11+
12+
public Histogram(int[] counts) {
13+
this.counts = counts;
14+
getChildren().clear();
15+
paint();
16+
}
17+
18+
void paint() {
19+
getChildren().clear();
20+
double frameWidth = getWidth();
21+
double frameHeight = getHeight();
22+
double unitWidth = frameWidth / counts.length - 10;
23+
24+
for (int i = 0; i < counts.length; i++) {
25+
double labelX = i * unitWidth;
26+
double labelY = frameHeight;
27+
double unitHeight = counts[i];
28+
double unitY = frameHeight - unitHeight;
29+
double unitX = i * unitWidth;
30+
Text label = new Text(labelX + 5, labelY - 5, (char) (65 + i) + "");
31+
Rectangle chartShape = new Rectangle(unitX, unitY - label.getLayoutBounds().getHeight(), unitWidth, unitHeight);
32+
chartShape.setFill(Color.WHITE);
33+
chartShape.setStroke(Color.BLACK);
34+
getChildren().add(chartShape);
35+
getChildren().add(label);
36+
}
37+
}
38+
39+
public void setCounts(int[] counts) {
40+
this.counts = counts;
41+
// redraw the histogram
42+
paint();
43+
}
44+
}

ch_16/exercise16_11/testFile.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
The jewelry store in Saratoga Springs where the first case was identified -- a man in his 60s -- has been closed since just before Christmas,
2+
it said. Gov. Andrew Cuomo has asked anyone who possibly may have been in contact with that man or even in contact with someone exposed to him to come forward.
3+
4+
The man had not traveled recently, just like the man in the first identified U.S. case in Colorado, which suggests community spread has already happened.
5+
The CDC says the strain had been circulating in the U.K. since September, meaning it likely had been in the U.S. via
6+
travel for some time before it was detected in Colorado.
7+
8+
Cuomo said Wednesday evidence appears to show the confirmed upstate case was connected to UK travel,
9+
despite no recent travel on behalf of the man.
10+
He called once again on the feds to mandate testing for all international travelers.
11+
Hospitals have become increasingly taxed over the last six weeks, a direct consequence of more infections from people's behavior,
12+
Cuomo has said. New York state hospitalizations are at 8,665, the same total admitted on May 6.
13+
Single-day death tolls are at mid-May levels. And weekly case averages are up 37 percent in New York over the past 14 days, according to New York Times data.

ch_16/exercise16_11/testFile2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aa 2
2+
bbb 3
3+
zzzz 4

0 commit comments

Comments
 (0)