Omar_Natour

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

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