Wednesday 28 December 2016

Chapter 16 Exercise 11, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

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 con- tains 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.

import javafx.geometry.VPos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Histogram extends Pane {

    private char[] chars = new char[26];
    private int counts[] = new int[26];
    private Rectangle[] bars = new Rectangle[26];
    private File file;
    GridPane pane;

    double w = 350;
    double h = 350;

    public Histogram(String filename) {

        this.file = new File(filename.trim());

        setWidth(w);
        setHeight(h);
        readFile();
        draw();
    }

    private void readFile() {

        Scanner scanner;
        String s = "";
        try {
            scanner = new Scanner(file);
            while (scanner.hasNextLine()) {

                s += scanner.nextLine();
            }
        } catch (IOException ex) {
        }

        s = s.toUpperCase();
        for (int i = 0; i < s.length(); i++) {
            char character = s.charAt(i);
            if (Character.isLetter(character)) {
                counts[character - 'A']++;
            }
        }

    }
    private double getTotal() {
        double total = 0;
        for (int count : counts) {
            total += count;
        }
        return total;
    }

    private void draw() {

        pane = new GridPane();
        double barW = w / chars.length;
        double total = getTotal();

        for (int i = 0; i < counts.length; i++) {
            chars[i] = (char) ('A' + i);

            double percentage = counts[i] / total;
            double barH = h * percentage;

            bars[i] = new Rectangle(barW, barH);
            Label label = new Label(chars[i] + "", bars[i]);
            label.setContentDisplay(ContentDisplay.TOP);

            pane.add(label, i, 0);
            GridPane.setValignment(label, VPos.BASELINE);

        }
        getChildren().addAll(pane);

    }

    public int[] getCounts() {
        return counts;
    }

    public void setCounts(int[] counts) {
        this.counts = counts;
        draw();
    }

}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Exercise_11 extends Application {

    Pane pane = new Pane();
    TextField textField = new TextField();
    VBox box = new VBox();
    @Override
    public void start(Stage primaryStage) throws Exception {

        Label lblFilename = new Label("Filename:", textField);
        lblFilename.setContentDisplay(ContentDisplay.RIGHT);
        textField.setPrefColumnCount(20);
        Button btView = new Button("View");

        HBox hBox = new HBox(lblFilename, btView);

        box.getChildren().addAll(pane, hBox);
        Scene scene = new Scene(box);
        primaryStage.setScene(scene);
        btView.setOnAction(e-> {
            update();
            box.setTranslateY(10);
            primaryStage.sizeToScene();

        });
        primaryStage.show();
    }

    private void update() {
        Histogram graph = new Histogram(textField.getText());
        pane.getChildren().add(graph);
    }


    public static void main(String[] args) {
        Application.launch(args);
    }
}

No comments :

Post a Comment