From 8be5ed6e5dcb85b8272db64cf0f31a9762b6ec4c Mon Sep 17 00:00:00 2001 From: Harry Dulaney Date: Sun, 31 Dec 2023 08:26:08 -0600 Subject: [PATCH 1/2] ex 11 --- ch_16/exercise16_11/Exercise16_11.java | 87 ++++++++++++++++++++++++++ ch_16/exercise16_11/Histogram.java | 44 +++++++++++++ ch_16/exercise16_11/testFile.txt | 13 ++++ ch_16/exercise16_11/testFile2.txt | 3 + 4 files changed, 147 insertions(+) create mode 100644 ch_16/exercise16_11/Exercise16_11.java create mode 100644 ch_16/exercise16_11/Histogram.java create mode 100644 ch_16/exercise16_11/testFile.txt create mode 100644 ch_16/exercise16_11/testFile2.txt diff --git a/ch_16/exercise16_11/Exercise16_11.java b/ch_16/exercise16_11/Exercise16_11.java new file mode 100644 index 0000000..b318a31 --- /dev/null +++ b/ch_16/exercise16_11/Exercise16_11.java @@ -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. + *

+ * 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; + } + + +} diff --git a/ch_16/exercise16_11/Histogram.java b/ch_16/exercise16_11/Histogram.java new file mode 100644 index 0000000..bda47f7 --- /dev/null +++ b/ch_16/exercise16_11/Histogram.java @@ -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(); + } +} diff --git a/ch_16/exercise16_11/testFile.txt b/ch_16/exercise16_11/testFile.txt new file mode 100644 index 0000000..dd85e19 --- /dev/null +++ b/ch_16/exercise16_11/testFile.txt @@ -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. diff --git a/ch_16/exercise16_11/testFile2.txt b/ch_16/exercise16_11/testFile2.txt new file mode 100644 index 0000000..7e946e3 --- /dev/null +++ b/ch_16/exercise16_11/testFile2.txt @@ -0,0 +1,3 @@ +aa 2 +bbb 3 +zzzz 4 \ No newline at end of file From 7f1bb6f3326b64a71c43c28827b6833f59edbb4c Mon Sep 17 00:00:00 2001 From: Harry Dulaney Date: Sun, 31 Dec 2023 08:27:09 -0600 Subject: [PATCH 2/2] readme update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ec2dd58..40ead10 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ Indicates 100% completion of all exercises for that chapter 16 - JavaFx UI Controls and Multimedia

-Exercises Needed: 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 +Exercises Needed: 13, 15, 17, 19, 21, 23, 25, 27, 29, 31

  • Chapter