Advertisement
philcrafts

Luis Calvao / Phil Crafts Ugly# HW

Sep 15th, 2017
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. /*
  2.  * Name: Luis Calvao / Philip Crafts
  3.  * Date: 9/15/17
  4.  * Course Number: CSC220
  5.  * Course Name: Data Structures
  6.  * Problem Number: N/A
  7.  * Email:  lccalvao@student.stcc.edu / prcrafts@student.stcc.edu
  8.  * Ugly Number Table
  9.  */
  10. import javafx.application.Application;
  11. import javafx.geometry.Insets;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.Separator;
  16. import javafx.scene.control.TextArea;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.input.KeyCode;
  19. import javafx.scene.layout.GridPane;
  20. import javafx.stage.Stage;
  21.  
  22. public class UglyNumbers extends Application {
  23.  
  24.     Stage window;
  25.  
  26.     TextField firstInput = new TextField();
  27.     TextField secondInput = new TextField();
  28.     TextArea outputBox = new TextArea();
  29.     Button enterButton = new Button("enter");
  30.     Button resetButton = new Button("reset");
  31.  
  32.     @Override
  33.     public void start(Stage primaryStage) throws Exception {
  34.  
  35.         // gui javafx
  36.         window = primaryStage;
  37.         window.setTitle("UGLY NUMBERS GENERATOR");
  38.         outputBox.setPrefRowCount(20);
  39.         outputBox.setPrefColumnCount(60);
  40.         outputBox.setStyle("-fx-font-family: monospace");
  41.  
  42.         Label oneLabel = new Label("enter first number:");
  43.         Label twoLabel = new Label("enter second number:");
  44.         Label outputLabel = new Label("output:");
  45.         Separator line = new Separator();
  46.  
  47.         GridPane grid = new GridPane();
  48.         grid.setPadding(new Insets(50, 50, 100, 100));
  49.         grid.setVgap(20);
  50.         grid.setHgap(10);
  51.         GridPane.setConstraints(oneLabel, 0, 0);
  52.         GridPane.setConstraints(firstInput, 1, 0);
  53.         GridPane.setConstraints(twoLabel, 0, 1);
  54.         GridPane.setConstraints(secondInput, 1, 1);
  55.         GridPane.setConstraints(line, 0, 2);
  56.         GridPane.setConstraints(outputLabel, 0, 3);
  57.         GridPane.setConstraints(outputBox, 1, 3);
  58.         GridPane.setConstraints(enterButton, 3, 0);
  59.         GridPane.setConstraints(resetButton, 3, 1);
  60.         grid.getChildren().addAll(oneLabel, firstInput, twoLabel, secondInput, enterButton, resetButton, line,
  61.                 outputLabel, outputBox);
  62.  
  63.         Scene scene = new Scene(grid, 900, 600);
  64.         window.setScene(scene);
  65.         window.show();
  66.  
  67.         // event handling
  68.         firstInput.setOnKeyPressed(event -> {
  69.             if (event.getCode() == KeyCode.ENTER) {
  70.                 getUgly();
  71.             }
  72.         });
  73.  
  74.         secondInput.setOnKeyPressed(event -> {
  75.             if (event.getCode() == KeyCode.ENTER) {
  76.                 getUgly();
  77.             }
  78.         });
  79.  
  80.         enterButton.setOnAction(e -> getUgly());
  81.         resetButton.setOnAction(e -> reset());
  82.  
  83.     }
  84.  
  85.     // assign user input
  86.     private void getUgly() {
  87.  
  88.         int first = Integer.parseInt(firstInput.getText());
  89.         int second = Integer.parseInt(secondInput.getText());
  90.         process(first, second);
  91.  
  92.     }
  93.  
  94.     // goes through input range and outputs any ugly numbers
  95.     private void process(int x, int y) {
  96.  
  97.         int count = 0;
  98.  
  99.         String output = "";
  100.         while (x <= y) {
  101.  
  102.             if (isUgly(x)) {
  103.  
  104.                 output += String.format("%10d|", x);
  105.                 count++;
  106.  
  107.                 if (count % 5 == 0)
  108.                     output += "\n";
  109.  
  110.             }
  111.             x++;
  112.         }
  113.  
  114.         outputBox.setText(output);
  115.        
  116.  
  117.     }
  118.  
  119.     // checks for ugly numbers
  120.     private static boolean isUgly(int z) {
  121.  
  122.         while (z % 5 == 0) {
  123.             z /= 5;
  124.         }
  125.  
  126.         while (z % 3 == 0) {
  127.             z /= 3;
  128.         }
  129.  
  130.         while (z % 2 == 0) {
  131.             z /= 2;
  132.         }
  133.  
  134.         if (z >= 7) {
  135.             return false;
  136.         }
  137.  
  138.         return true;
  139.     }
  140.    
  141.     // reset fields
  142.     private void reset() {
  143.         firstInput.setText("");
  144.         secondInput.setText("");
  145.         outputBox.setText("");
  146.     }
  147.  
  148.     public static void main(String[] args) {
  149.  
  150.         launch(args);
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement