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
+ * 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