Advertisement
CE_162

Compare Arrays

Sep 23rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. /*
  2.  * Name: Collin Emond
  3.  * Date: 9-23-16
  4.  * Course Number: CSC-220
  5.  * Course Name: Intro to Data Structures
  6.  * Problem Number: CSC-220 HW2 One Dimensional Arrays
  7.  * Email: cremond0001@student.stcc.edu
  8.  * Write a method, isEquivalent, that checks whether two int arrays are equivalent.
  9.  * Two arrays are equivalent if they have the same length and contain the same values in any order.
  10.  */
  11.  
  12. package Homeworks;
  13.  
  14. import java.util.Arrays;
  15.  
  16. import javafx.application.Application;
  17. import javafx.geometry.*;
  18. import javafx.scene.*;
  19. import javafx.scene.control.*;
  20. import javafx.scene.layout.*;
  21. import javafx.stage.*;
  22.  
  23. public class CompareArrays extends Application {
  24.  
  25.     static TextArea firstArrayTxt;
  26.     static TextField secondArrayTxt;
  27.     static TextField statusTxt;
  28.     Button compareArrayBtn;
  29.  
  30.     Integer[] intarray;
  31.     Integer[] intarraytwo;
  32.     static String[] elementsinarray;
  33.     static String[] elementsinarray2;
  34.     static String stringone;
  35.     static String stringtwo;
  36.  
  37.     @Override
  38.     public void start(Stage primaryStage) {
  39.         BorderPane borderPane = new BorderPane();
  40.         borderPane.setTop(getArrayInputBox());
  41.         borderPane.setCenter(getArrayButton());
  42.         borderPane.setBottom(getArrayStatusBox());
  43.         borderPane.setPadding(new Insets(20, 20, 40, 20));
  44.         compareArrayBtn.setOnAction(e -> mainLogic());
  45.         Scene scene = new Scene(borderPane, 400, 500);
  46.         primaryStage.setTitle("Compare Two Arrays");
  47.         primaryStage.setScene(scene);
  48.         primaryStage.show();
  49.  
  50.     }
  51.  
  52.     // **********************************************
  53.     private static void mainLogic() {
  54.         stringone = firstArrayTxt.getText().trim();
  55.         stringtwo = secondArrayTxt.getText().trim();
  56.  
  57.         elementsinarray = stringone.split(" ");
  58.         elementsinarray2 = stringtwo.split(" ");
  59.  
  60.         int[] intarray1 = new int[elementsinarray.length];
  61.         int[] intarray2 = new int[elementsinarray2.length];
  62.         for (int i = 0; i < elementsinarray.length; i++) {
  63.             intarray1[i] = Integer.parseInt(elementsinarray[i]); //Creates Array of Int w/ same values from the string above
  64.         }
  65.         for (int i = 0; i < elementsinarray2.length; i++) {
  66.             intarray2[i] = Integer.parseInt(elementsinarray2[i]); //Creates Array of Int w/ same values from the string above
  67.         }
  68.         isEqual(intarray1, intarray2);
  69.     }
  70.  
  71.     // **********************************************
  72.  
  73.     private static void isEqual(int[] arrayone, int[] arraytwo) {
  74.         Arrays.sort(arrayone);
  75.         Arrays.sort(arraytwo);
  76.  
  77.         if (arrayone.length == arraytwo.length) {
  78.             statusTxt.appendText("The Arrays are EQUAL");
  79.         } else {
  80.             statusTxt.appendText("The Arrays are NOT EQUAL");
  81.         }
  82.     }
  83.  
  84.     // **********************************************
  85.  
  86.     public VBox getArrayInputBox() {
  87.         VBox vBox = new VBox(20);
  88.         firstArrayTxt = new TextArea();
  89.         firstArrayTxt.setPrefHeight(40);
  90.         firstArrayTxt.setPromptText("◎◎◎ Enter number of elements in first array ◎◎◎");
  91.         secondArrayTxt = new TextField();
  92.         secondArrayTxt.setPromptText("◎◎◎ Enter number of elements in second array ◎◎◎");
  93.         vBox.getChildren().addAll(firstArrayTxt, secondArrayTxt);
  94.         vBox.setAlignment(Pos.CENTER);
  95.         return vBox;
  96.     }
  97.     // **********************************************
  98.  
  99.     public VBox getArrayButton() {
  100.         VBox vBox = new VBox();
  101.         compareArrayBtn = new Button("♛ Determine the equivalence of the two arrays ♛");
  102.         vBox.getChildren().add(compareArrayBtn);
  103.         vBox.setAlignment(Pos.CENTER);
  104.         return vBox;
  105.     }
  106.     // **********************************************
  107.  
  108.     public VBox getArrayStatusBox() {
  109.         VBox vBox = new VBox();
  110.         statusTxt = new TextField();
  111.         statusTxt.setPromptText("OUTCOME PRINTS HERE");
  112.         statusTxt.setEditable(false);
  113.         statusTxt.setMouseTransparent(true);
  114.         vBox.getChildren().add(statusTxt);
  115.         vBox.setAlignment(Pos.CENTER);
  116.         return vBox;
  117.     }
  118.     // **********************************************
  119.  
  120.     public static void main(String[] args) {
  121.         launch(args);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement