Advertisement
philcrafts

Luis Calvao / Phil Crafts Ugly# HW

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