Omar_Natour

Natour, O. 9/23/16 Csc-220 isEquivalent

Sep 23rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 9/23/2016
  4.  * Csc-220 Data Structures
  5.  * Hw2 isEquivalent Method
  6.  * Check to see if two arrays have equivalent values within them
  7.  * Ojnatour0001@student.stcc.edu
  8.  */
  9.  
  10. import java.util.Arrays;
  11. import javafx.application.Application;
  12. import javafx.event.ActionEvent;
  13. import javafx.event.EventHandler;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.Node;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.Button;
  18. import javafx.scene.control.TextArea;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.layout.BorderPane;
  21. import javafx.scene.layout.HBox;
  22. import javafx.scene.layout.VBox;
  23. import javafx.scene.text.Text;
  24. import javafx.stage.Stage;
  25.  
  26. public class ArrayEquivalency extends Application{
  27.     public static void main(String[] args ){
  28.         launch(args);
  29.     }
  30.    
  31.     TextArea taLeft = new TextArea();
  32.     TextArea taRight = new TextArea();
  33.     TextField tfStatus = new TextField();
  34.    
  35.     public void start(Stage primaryStage){
  36.        
  37.         BorderPane bpGui = new BorderPane();
  38.        
  39.         bpGui.setTop(intro());
  40.         bpGui.setCenter(middle());
  41.         bpGui.setBottom(bottom());
  42.        
  43.         Scene sce = new Scene(bpGui, 550, 200);
  44.  
  45.         primaryStage.setTitle("Equivalent Arrays");
  46.         primaryStage.setScene(sce);
  47.         primaryStage.show();
  48.         primaryStage.setResizable(false);
  49.     }
  50.    
  51.     private Node intro(){
  52.        
  53.         HBox hbIntro = new HBox();
  54.        
  55.         hbIntro.setPrefHeight(60);
  56.        
  57.         Text tIntro = new Text(
  58.                 "Enter a list of numbers seperated by spaces in each box to compair the two.");
  59.        
  60.         hbIntro.setAlignment(Pos.CENTER);
  61.         hbIntro.getChildren().add(tIntro);
  62.        
  63.         return hbIntro;
  64.     }
  65.    
  66.     private Node middle(){
  67.         //structure
  68.         HBox hbCenter = new HBox(20);
  69.         VBox vbLeft = new VBox(5);
  70.         VBox vbRight = new VBox(5);
  71.         HBox hbLeft = new HBox(5);
  72.         HBox hbRight = new HBox(5);
  73.        
  74.         hbCenter.setAlignment(Pos.CENTER);
  75.         hbLeft.setAlignment(Pos.CENTER);
  76.         hbRight.setAlignment(Pos.CENTER);
  77.        
  78.         vbLeft.setPrefWidth(200);
  79.         vbRight.setPrefWidth(200);
  80.         vbLeft.setAlignment(Pos.CENTER);
  81.         vbRight.setAlignment(Pos.CENTER);
  82.        
  83.         //elements
  84.         Button bLeft = new Button("Randomize");
  85.         Button bCLeft = new Button("Clear");
  86.         Button bRight = new Button("Randomize");
  87.         Button bCRight = new Button("Clear");
  88.        
  89.         taLeft.setWrapText(true);
  90.         taRight.setWrapText(true);
  91.    
  92.         bLeft.setOnAction(e-> taLeft.setText(getRandom()));
  93.         bRight.setOnAction(e-> taRight.setText(getRandom()));
  94.         bCLeft.setOnAction( e-> taLeft.setText(""));
  95.         bCRight.setOnAction( e-> taRight.setText(""));
  96.        
  97.         hbLeft.getChildren().addAll(bLeft, bCLeft);
  98.         hbRight.getChildren().addAll(bRight,bCRight);
  99.        
  100.         vbLeft.getChildren().addAll(taLeft, hbLeft);
  101.         vbRight.getChildren().addAll(taRight, hbRight );
  102.         hbCenter.getChildren().addAll(vbLeft, vbRight);
  103.        
  104.         return hbCenter;   
  105.     }
  106.    
  107.     private Node bottom(){
  108.         //structure
  109.         VBox vbGo = new VBox();
  110.         HBox hbCompare = new HBox();
  111.         HBox hbStatus = new HBox(5);
  112.        
  113.         hbCompare.setAlignment(Pos.CENTER);
  114.         hbStatus.setAlignment(Pos.CENTER);
  115.         hbStatus.setPrefHeight(50);
  116.        
  117.         //elements
  118.         Button bCompare = new Button("Compare");
  119.        
  120.         Text tStatus = new Text("Status:");
  121.        
  122.         bCompare.setPrefWidth(75);
  123.         tfStatus.setPrefColumnCount(25);
  124.         tfStatus.setEditable(false);
  125.        
  126.         EventHandler<ActionEvent> Compare = e ->{
  127.            
  128.             String sLeft = taLeft.getText();
  129.             String sRight = taRight.getText();
  130.            
  131.             if(sLeft.matches("(\\s*\\d+\\s*)+") && sRight.matches("(\\s*\\d+\\s*)+")){
  132.                
  133.                 String[] saLeft = sLeft.split("(\\s+)");
  134.                 String[] saRight = sRight.split("(\\s+)");
  135.                
  136.                 if(isEquivalent(saLeft, saRight)){
  137.                     tfStatus.setText("Equivalent Arrays");
  138.                 }
  139.                 else{
  140.                     tfStatus.setText("Not Eqivalent Arrays");
  141.                 }
  142.             }
  143.             else{
  144.                 tfStatus.setText("Error with inputs");
  145.             }
  146.         };
  147.        
  148.         bCompare.setOnAction(Compare);
  149.        
  150.         hbCompare.getChildren().add(bCompare);
  151.         hbStatus.getChildren().addAll(tStatus, tfStatus);
  152.         vbGo.getChildren().addAll(hbCompare, hbStatus);
  153.         return vbGo;
  154.     }
  155.    
  156.     private boolean isEquivalent(String[] saA, String[] saB ){
  157.        
  158.         if(saA.length != saB.length || saA.length == 0 || saB.length == 0)
  159.             return false;
  160.        
  161.         int[] iaA = new int[saA.length];
  162.         int[] iaB = new int[saB.length];
  163.        
  164.         for(int i =0; i < saA.length; i++){
  165.             if(!(saA[i].isEmpty()))
  166.                 iaA[i] = Integer.parseInt(saA[i]);
  167.         }
  168.         for(int i = 0; i< saB.length; i++){
  169.             if(!(saB[i].isEmpty()))
  170.                 iaB[i] = Integer.parseInt(saB[i]);
  171.         }
  172.        
  173.         Arrays.sort(iaA);
  174.         Arrays.sort(iaB);
  175.        
  176.         for(int i = 0; i < saA.length;i++){
  177.             if(iaA[i] != iaB[i])
  178.                 return false;
  179.         }
  180.         return true;
  181.     }
  182.    
  183.     private String getRandom(){
  184.        
  185.         String sRandom = "";
  186.        
  187.         for(int i = 0; i < (int)(Math.random()* 50 + 1); i++){
  188.             sRandom += ((int)(Math.random()* 100 + 1) + " ");
  189.         }
  190.         return sRandom;
  191.     }
  192. }
Add Comment
Please, Sign In to add comment