Advertisement
rjsantiago0001

Ugly Numbers

Sep 15th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. /*
  2. * Name: Ricardo Santiago
  3. * Date: 9/10/16
  4. * Course Number: CSC-220
  5. * Course Name: Data Structures
  6. * Problem Number: NA
  7. * Email: rjsantiago0001@student.stcc.edu
  8.  */
  9.  
  10. import javafx.application.Application;
  11. import javafx.geometry.Insets;
  12. import javafx.geometry.Pos;
  13. import javafx.scene.Scene;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.TextArea;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.layout.HBox;
  19. import javafx.scene.layout.VBox;
  20. import javafx.scene.text.Text;
  21. import javafx.stage.Stage;
  22.  
  23. import java.util.regex.Pattern;
  24.  
  25. public class UglyNumbers extends Application {
  26.  
  27.     private TextField tfN;
  28.     private TextField tfI;
  29.     private TextField tfStatus;
  30.     private TextArea taUglyResults;
  31.     private TextArea taNonUglyResults;
  32.  
  33.  
  34.     private int[] initializeArrayWithRange(int a, int b) {
  35.         int[] arrNumbers = new int[b];
  36.         int c = 0;
  37.  
  38.         for (int j = a; j <= b; j++) {
  39.             arrNumbers[c] = j;
  40.             c++;
  41.         }
  42.         return arrNumbers;
  43.     }
  44.  
  45.     private void processArray(int[] arr) {
  46.  
  47.         int[] arrUglyNumbers = new int[arr.length];
  48.         int c = 0;
  49.  
  50.         for (int i = 0; i < arr.length; i++) {
  51.             if (isUgly(arr[i]) == true) {
  52.                 arrUglyNumbers[c] = arr[i];
  53.                 this.taUglyResults.appendText("\n          " + arrUglyNumbers[c]);
  54.                 c++;
  55.             } else this.taNonUglyResults.appendText("\n          " + arr[i]);
  56.         }
  57.     }
  58.  
  59.     private boolean isUgly(int n) {
  60.         while (n % 2 == 0)
  61.             n /= 2;
  62.         while (n % 3 == 0)
  63.             n /= 3;
  64.         while (n % 5 == 0)
  65.             n /= 5;
  66.         return n == 1 ? true : false;
  67.     }
  68.  
  69.     private void btCalculateOnClick() {
  70.         taNonUglyResults.setText("");
  71.         taUglyResults.setText("");
  72.  
  73.         try {
  74.             int i = Integer.parseInt(this.tfI.getText());
  75.             int n = Integer.parseInt(this.tfN.getText());
  76.  
  77.             if (Pattern.matches("^[0-9]*[1-9][0-9]*$",
  78.                     Integer.toString(i) + Integer.toString(n))) ;
  79.             {
  80.                 tfStatus.setText("Input is valid");
  81.  
  82.                 processArray(initializeArrayWithRange(Integer.parseInt(tfI.getText()), Integer.parseInt(tfN.getText())));
  83.             }
  84.         } catch (NumberFormatException e) {
  85.             e.printStackTrace();
  86.             this.tfStatus.setText("A number format exception has occured");
  87.         }
  88.     }
  89.  
  90.     private VBox getTextPane() {
  91.         VBox textPane = new VBox(30);
  92.         textPane.setPadding(new Insets(30, 30, 30, 30));
  93.         textPane.setAlignment(Pos.CENTER);
  94.         textPane.setStyle("-fx-font-weight: bold;-fx-font-size: 1.3em;");
  95.  
  96.         Text txtDescription = new Text("Find ugly numbers in the range of i through n");
  97.         Text txtInstruction = new Text("Enter values for i and n :");
  98.         textPane.getChildren().addAll(txtDescription, txtInstruction);
  99.  
  100.         return textPane;
  101.     }
  102.  
  103.     private HBox getUserInputPane() {
  104.         HBox userInputPane = new HBox(10);
  105.         userInputPane.setAlignment(Pos.CENTER);
  106.         userInputPane.setStyle("-fx-font-weight: bold;-fx-font-size: 1.3em;");
  107.  
  108.         Label lblI = new Label("i =");
  109.         this.tfI = new TextField();
  110.         this.tfI.setMaxWidth(90);
  111.         Label lblN = new Label(", n =");
  112.         this.tfN = new TextField();
  113.         this.tfN.setMaxWidth(90);
  114.  
  115.         userInputPane.getChildren().addAll(lblI, tfI, lblN, tfN);
  116.  
  117.         return userInputPane;
  118.     }
  119.  
  120.     private HBox getButtonPane() {
  121.         HBox buttonPane = new HBox();
  122.         buttonPane.setPadding(new Insets(10, 10, 10, 10));
  123.         buttonPane.setAlignment(Pos.CENTER);
  124.  
  125.         Button btOk = new Button("Calculate");
  126.         btOk.setOnAction(e -> this.btCalculateOnClick());
  127.  
  128.         buttonPane.getChildren().addAll(btOk);
  129.  
  130.         return buttonPane;
  131.     }
  132.  
  133.     private HBox getResultsPane() {
  134.         HBox resultsPane = new HBox(30);
  135.         resultsPane.setPadding(new Insets(10, 10, 10, 10));
  136.         resultsPane.setAlignment(Pos.CENTER);
  137.  
  138.         resultsPane.getChildren().addAll(getUglyResultsPane(), getNonUglyResultsPane());
  139.  
  140.         return resultsPane;
  141.     }
  142.  
  143.     private VBox getUglyResultsPane() {
  144.         VBox uglyResultsPane = new VBox(10);
  145.         uglyResultsPane.setAlignment(Pos.CENTER);
  146.         uglyResultsPane.setPrefWidth(150);
  147.  
  148.         Label lbUgly = new Label(" Ugly Numbers");
  149.         lbUgly.setStyle("-fx-font-weight: bold;-fx-font-size: 1.3em;");
  150.  
  151.         this.taUglyResults = new TextArea();
  152.         this.taUglyResults.setEditable(false);
  153.         this.taUglyResults.setPrefRowCount(100);
  154.  
  155.         uglyResultsPane.getChildren().addAll(lbUgly, taUglyResults);
  156.         return uglyResultsPane;
  157.     }
  158.  
  159.     private VBox getNonUglyResultsPane() {
  160.         VBox nonUglyResultsPane = new VBox(10);
  161.         nonUglyResultsPane.setAlignment(Pos.CENTER);
  162.         nonUglyResultsPane.setPrefWidth(150);
  163.  
  164.         Label lbNonUgly = new Label(" Non-Ugly Numbers");
  165.         lbNonUgly.setStyle("-fx-font-weight: bold;-fx-font-size: 1.3em;");
  166.  
  167.         this.taNonUglyResults = new TextArea();
  168.         this.taNonUglyResults.setEditable(false);
  169.         this.taNonUglyResults.setPrefRowCount(100);
  170.  
  171.         nonUglyResultsPane.getChildren().addAll(lbNonUgly, taNonUglyResults);
  172.  
  173.         return nonUglyResultsPane;
  174.     }
  175.  
  176.     private HBox getStatusPane() {
  177.         HBox statusPane = new HBox(10);
  178.         statusPane.setPadding(new Insets(10, 10, 10, 10));
  179.         statusPane.setAlignment(Pos.CENTER);
  180.         statusPane.setStyle("-fx-font-weight: bold;-fx-font-size: 1.3em;");
  181.  
  182.         Label lblStatus = new Label("Status:");
  183.         this.tfStatus = new TextField();
  184.         this.tfStatus.setPrefColumnCount(20);
  185.         this.tfStatus.setEditable(false);
  186.         this.tfStatus.setAlignment(Pos.CENTER);
  187.  
  188.         statusPane.getChildren().addAll(lblStatus, this.tfStatus);
  189.         return statusPane;
  190.     }
  191.  
  192.     @Override
  193.     public void start(Stage primaryStage) throws Exception {
  194.         VBox gui = new VBox(10);
  195.         gui.getChildren().addAll(getTextPane(), getUserInputPane(), getButtonPane(), getResultsPane(), getStatusPane());
  196.  
  197.         Scene scene = new Scene(gui, 400, 600);
  198.         primaryStage.setResizable(false);
  199.         primaryStage.setTitle("Ugly Numbers");
  200.         primaryStage.setScene(scene);
  201.         primaryStage.show();
  202.     }
  203.  
  204.     public static void main(String[] args) {
  205.         launch(args);
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement