-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from HarryDulaney/chapter-16-solutions
Chapter 16 solution Ex 11
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ch_16.exercise16_11; | ||
|
||
import javafx.scene.Scene; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.layout.VBox; | ||
import javafx.stage.Stage; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* **16.11 (Create a histogram for occurrences of letters) Write a program that reads a | ||
* file and displays a histogram to show the occurrences of each letter in the file, | ||
* as shown in Figure 16.40b. The file name is entered from a text field. Pressing | ||
* the Enter key on the text field causes the program to start to read and process | ||
* the file and displays the histogram. The histogram is displayed in the center of the | ||
* window. Define a class named Histogram that extends Pane. The class contains the property counts that is an array | ||
* of 26 elements. counts[0] stores the | ||
* number of A, counts[1] the number of B, and so on. The class also contains a | ||
* setter method for setting a new counts and displaying the histogram for the new | ||
* counts. | ||
* <p> | ||
* Example Filename input: | ||
* C:\Users\Harry\IdeaProjects\intro-to-java-programming\ch_16\exercise16_11\testFile.txt | ||
* (Right-click on the testFile.txt and choose 'copy absolute path') | ||
*/ | ||
public class Exercise16_11 extends javafx.application.Application { | ||
private int[] counts = new int[26]; | ||
|
||
@Override | ||
public void start(Stage primaryStage) throws Exception { | ||
Histogram histogram = new Histogram(new int[26]); | ||
Label label = new Label("Filename:"); | ||
TextField fileInputField = new TextField(); | ||
Button viewHistogramButton = new Button("View"); | ||
VBox parentBox = new VBox(); | ||
double WIDTH = 800; | ||
histogram.setPrefWidth(WIDTH); | ||
double HEIGHT = 500; | ||
histogram.setPrefHeight(HEIGHT); | ||
parentBox.setPrefWidth(WIDTH); | ||
parentBox.setPrefHeight(HEIGHT); | ||
VBox.setVgrow(histogram, javafx.scene.layout.Priority.ALWAYS); | ||
parentBox.getChildren().add(histogram); | ||
parentBox.getChildren().addAll(label, fileInputField, viewHistogramButton); | ||
viewHistogramButton.setOnMousePressed(event -> { | ||
String filePath = fileInputField.getText(); | ||
if (filePath.isEmpty()) { | ||
return; | ||
} | ||
File file = new File(filePath); | ||
if (!file.exists()) { | ||
System.out.println("File does not exist"); | ||
return; | ||
} | ||
histogram.setCounts(countLetterFromFile(file, counts)); | ||
}); | ||
Scene scene = new Scene(parentBox, WIDTH, HEIGHT); | ||
primaryStage.setResizable(false); | ||
primaryStage.setScene(scene); | ||
primaryStage.show(); | ||
} | ||
|
||
public static int[] countLetterFromFile(File file, int[] counts) { | ||
String s = ""; | ||
try (java.util.Scanner input = new java.util.Scanner(file)) { | ||
while (input.hasNext()) { | ||
s = input.nextLine(); | ||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
if (Character.isLetter(c)) { | ||
int index = Character.toUpperCase(c) - 'A'; | ||
counts[index]++; | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
System.out.println("Error reading file"); | ||
System.out.println("Error Message: " + e.getMessage()); | ||
} | ||
|
||
return counts; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ch_16.exercise16_11; | ||
|
||
import javafx.scene.layout.Pane; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.shape.Rectangle; | ||
import javafx.scene.text.Text; | ||
|
||
public class Histogram extends Pane { | ||
private int[] counts; | ||
|
||
|
||
public Histogram(int[] counts) { | ||
this.counts = counts; | ||
getChildren().clear(); | ||
paint(); | ||
} | ||
|
||
void paint() { | ||
getChildren().clear(); | ||
double frameWidth = getWidth(); | ||
double frameHeight = getHeight(); | ||
double unitWidth = frameWidth / counts.length - 10; | ||
|
||
for (int i = 0; i < counts.length; i++) { | ||
double labelX = i * unitWidth; | ||
double labelY = frameHeight; | ||
double unitHeight = counts[i]; | ||
double unitY = frameHeight - unitHeight; | ||
double unitX = i * unitWidth; | ||
Text label = new Text(labelX + 5, labelY - 5, (char) (65 + i) + ""); | ||
Rectangle chartShape = new Rectangle(unitX, unitY - label.getLayoutBounds().getHeight(), unitWidth, unitHeight); | ||
chartShape.setFill(Color.WHITE); | ||
chartShape.setStroke(Color.BLACK); | ||
getChildren().add(chartShape); | ||
getChildren().add(label); | ||
} | ||
} | ||
|
||
public void setCounts(int[] counts) { | ||
this.counts = counts; | ||
// redraw the histogram | ||
paint(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The jewelry store in Saratoga Springs where the first case was identified -- a man in his 60s -- has been closed since just before Christmas, | ||
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. | ||
|
||
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. | ||
The CDC says the strain had been circulating in the U.K. since September, meaning it likely had been in the U.S. via | ||
travel for some time before it was detected in Colorado. | ||
|
||
Cuomo said Wednesday evidence appears to show the confirmed upstate case was connected to UK travel, | ||
despite no recent travel on behalf of the man. | ||
He called once again on the feds to mandate testing for all international travelers. | ||
Hospitals have become increasingly taxed over the last six weeks, a direct consequence of more infections from people's behavior, | ||
Cuomo has said. New York state hospitalizations are at 8,665, the same total admitted on May 6. | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
aa 2 | ||
bbb 3 | ||
zzzz 4 |