Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Luis Calvao / Philip Crafts
- * Date: 9/15/17
- * Course Number: CSC220
- * Course Name: Data Structures
- * Problem Number: N/A
- * Email: lccalvao@student.stcc.edu / prcrafts@student.stcc.edu
- * Ugly Number Table
- */
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.Separator;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- public class UglyNumbers extends Application {
- Stage window;
- TextField firstInput = new TextField();
- TextField secondInput = new TextField();
- TextArea outputBox = new TextArea();
- Button enterButton = new Button("enter");
- Button resetButton = new Button("reset");
- @Override
- public void start(Stage primaryStage) throws Exception {
- // gui javafx
- window = primaryStage;
- window.setTitle("UGLY NUMBERS GENERATOR");
- outputBox.setPrefRowCount(20);
- outputBox.setPrefColumnCount(60);
- outputBox.setStyle("-fx-font-family: monospace");
- Label oneLabel = new Label("enter first number:");
- Label twoLabel = new Label("enter second number:");
- Label outputLabel = new Label("output:");
- Separator line = new Separator();
- GridPane grid = new GridPane();
- grid.setPadding(new Insets(50, 50, 100, 100));
- grid.setVgap(20);
- grid.setHgap(10);
- GridPane.setConstraints(oneLabel, 0, 0);
- GridPane.setConstraints(firstInput, 1, 0);
- GridPane.setConstraints(twoLabel, 0, 1);
- GridPane.setConstraints(secondInput, 1, 1);
- GridPane.setConstraints(line, 0, 2);
- GridPane.setConstraints(outputLabel, 0, 3);
- GridPane.setConstraints(outputBox, 1, 3);
- GridPane.setConstraints(enterButton, 3, 0);
- GridPane.setConstraints(resetButton, 3, 1);
- grid.getChildren().addAll(oneLabel, firstInput, twoLabel, secondInput, enterButton, resetButton, line,
- outputLabel, outputBox);
- Scene scene = new Scene(grid, 900, 600);
- window.setScene(scene);
- window.show();
- // event handling
- firstInput.setOnKeyPressed(event -> {
- if (event.getCode() == KeyCode.ENTER) {
- getUgly();
- }
- });
- secondInput.setOnKeyPressed(event -> {
- if (event.getCode() == KeyCode.ENTER) {
- getUgly();
- }
- });
- enterButton.setOnAction(e -> getUgly());
- resetButton.setOnAction(e -> reset());
- }
- // assign user input
- private void getUgly() {
- int first = Integer.parseInt(firstInput.getText());
- int second = Integer.parseInt(secondInput.getText());
- process(first, second);
- }
- // goes through input range and outputs any ugly numbers
- private void process(int x, int y) {
- int count = 0;
- String output = "";
- while (x <= y) {
- if (isUgly(x)) {
- output += String.format("%10d|", x);
- count++;
- if (count % 5 == 0)
- output += "\n";
- }
- x++;
- }
- outputBox.setText(output);
- }
- // checks for ugly numbers
- private static boolean isUgly(int z) {
- while (z % 5 == 0) {
- z /= 5;
- }
- while (z % 3 == 0) {
- z /= 3;
- }
- while (z % 2 == 0) {
- z /= 2;
- }
- if (z >= 7) {
- return false;
- }
- return true;
- }
- // reset fields
- private void reset() {
- firstInput.setText("");
- secondInput.setText("");
- outputBox.setText("");
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement