Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 9/23/2016
- * Csc-220 Data Structures
- * Hw2 isEquivalent Method
- * Check to see if two arrays have equivalent values within them
- * Ojnatour0001@student.stcc.edu
- */
- import java.util.Arrays;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class ArrayEquivalency extends Application{
- public static void main(String[] args ){
- launch(args);
- }
- TextArea taLeft = new TextArea();
- TextArea taRight = new TextArea();
- TextField tfStatus = new TextField();
- public void start(Stage primaryStage){
- BorderPane bpGui = new BorderPane();
- bpGui.setTop(intro());
- bpGui.setCenter(middle());
- bpGui.setBottom(bottom());
- Scene sce = new Scene(bpGui, 550, 200);
- primaryStage.setTitle("Equivalent Arrays");
- primaryStage.setScene(sce);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- private Node intro(){
- HBox hbIntro = new HBox();
- hbIntro.setPrefHeight(60);
- Text tIntro = new Text(
- "Enter a list of numbers seperated by spaces in each box to compair the two.");
- hbIntro.setAlignment(Pos.CENTER);
- hbIntro.getChildren().add(tIntro);
- return hbIntro;
- }
- private Node middle(){
- //structure
- HBox hbCenter = new HBox(20);
- VBox vbLeft = new VBox(5);
- VBox vbRight = new VBox(5);
- HBox hbLeft = new HBox(5);
- HBox hbRight = new HBox(5);
- hbCenter.setAlignment(Pos.CENTER);
- hbLeft.setAlignment(Pos.CENTER);
- hbRight.setAlignment(Pos.CENTER);
- vbLeft.setPrefWidth(200);
- vbRight.setPrefWidth(200);
- vbLeft.setAlignment(Pos.CENTER);
- vbRight.setAlignment(Pos.CENTER);
- //elements
- Button bLeft = new Button("Randomize");
- Button bCLeft = new Button("Clear");
- Button bRight = new Button("Randomize");
- Button bCRight = new Button("Clear");
- taLeft.setWrapText(true);
- taRight.setWrapText(true);
- bLeft.setOnAction(e-> taLeft.setText(getRandom()));
- bRight.setOnAction(e-> taRight.setText(getRandom()));
- bCLeft.setOnAction( e-> taLeft.setText(""));
- bCRight.setOnAction( e-> taRight.setText(""));
- hbLeft.getChildren().addAll(bLeft, bCLeft);
- hbRight.getChildren().addAll(bRight,bCRight);
- vbLeft.getChildren().addAll(taLeft, hbLeft);
- vbRight.getChildren().addAll(taRight, hbRight );
- hbCenter.getChildren().addAll(vbLeft, vbRight);
- return hbCenter;
- }
- private Node bottom(){
- //structure
- VBox vbGo = new VBox();
- HBox hbCompare = new HBox();
- HBox hbStatus = new HBox(5);
- hbCompare.setAlignment(Pos.CENTER);
- hbStatus.setAlignment(Pos.CENTER);
- hbStatus.setPrefHeight(50);
- //elements
- Button bCompare = new Button("Compare");
- Text tStatus = new Text("Status:");
- bCompare.setPrefWidth(75);
- tfStatus.setPrefColumnCount(25);
- tfStatus.setEditable(false);
- EventHandler<ActionEvent> Compare = e ->{
- String sLeft = taLeft.getText();
- String sRight = taRight.getText();
- if(sLeft.matches("(\\s*\\d+\\s*)+") && sRight.matches("(\\s*\\d+\\s*)+")){
- String[] saLeft = sLeft.split("(\\s+)");
- String[] saRight = sRight.split("(\\s+)");
- if(isEquivalent(saLeft, saRight)){
- tfStatus.setText("Equivalent Arrays");
- }
- else{
- tfStatus.setText("Not Eqivalent Arrays");
- }
- }
- else{
- tfStatus.setText("Error with inputs");
- }
- };
- bCompare.setOnAction(Compare);
- hbCompare.getChildren().add(bCompare);
- hbStatus.getChildren().addAll(tStatus, tfStatus);
- vbGo.getChildren().addAll(hbCompare, hbStatus);
- return vbGo;
- }
- private boolean isEquivalent(String[] saA, String[] saB ){
- if(saA.length != saB.length || saA.length == 0 || saB.length == 0)
- return false;
- int[] iaA = new int[saA.length];
- int[] iaB = new int[saB.length];
- for(int i =0; i < saA.length; i++){
- if(!(saA[i].isEmpty()))
- iaA[i] = Integer.parseInt(saA[i]);
- }
- for(int i = 0; i< saB.length; i++){
- if(!(saB[i].isEmpty()))
- iaB[i] = Integer.parseInt(saB[i]);
- }
- Arrays.sort(iaA);
- Arrays.sort(iaB);
- for(int i = 0; i < saA.length;i++){
- if(iaA[i] != iaB[i])
- return false;
- }
- return true;
- }
- private String getRandom(){
- String sRandom = "";
- for(int i = 0; i < (int)(Math.random()* 50 + 1); i++){
- sRandom += ((int)(Math.random()* 100 + 1) + " ");
- }
- return sRandom;
- }
- }
Add Comment
Please, Sign In to add comment