Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Nate Wheeler
- * Date: sept 23, 2016
- * Course Number: csc220
- * Course Name: data structures
- * Problem Number: hw2
- * Email: nate23nate23@gmail.com
- * Short Description of the Problem:
- * Write a method, isEquivalent, that checks
- * whether two int arrays are equivalent. Two
- * arrays are equivalent if they have the same
- * length and contain the same values in any order.
- */
- import java.util.Arrays;
- import java.util.Scanner;
- public class EquivalentArrays {
- static int[] x;
- static int[] y;
- private static void process(Scanner sc, String args[]) {
- // make an array
- System.out.print("Enter array size: ");
- x = new int[sc.nextInt()];
- System.out.println("Processing " + Arrays.toString(x) + " ...");
- System.out.println("Please insert numbers for the array:");
- int c = 0;
- while (c < x.length) {
- x[c] = sc.nextInt();
- c++;
- }
- System.out.println("New array parameters: " + Arrays.toString(x));
- System.out.println("Please Enter Size of Next Array: ");
- y = new int[sc.nextInt()];
- System.out.println("Processing " + Arrays.toString(y) + " ...");
- int c1 = 0;
- while (c1 < y.length) {
- y[c1] = sc.nextInt();
- c1++;
- }
- System.out.println("New array parameters: " + Arrays.toString(y));
- // sort(x,y);
- System.out.println(Arrays.toString(x) + "\n" + Arrays.toString(y));
- // if(isEquivalent(x,y)==true)
- // System.out.println("The 2 Arrays are equivalent.");
- if (isEquivalent(x, y) != true)
- System.out.println("The 2 Arrays are inequivalent.");
- else
- System.out.println("The 2 Arrays are equivalent.");
- sc.nextLine();
- }
- public static void sort(int[] x1, int[] y1) {
- Arrays.sort(x1);
- Arrays.sort(y1);
- for (int i = 0; i <= x.length; i++) {
- x[i] = x1[i];
- y[i] = y1[i];
- }
- }
- public static boolean isEquivalent(int[] x1, int[] y1) {
- if (x1.length != y1.length)
- return false;
- Arrays.sort(x1);
- Arrays.sort(y1);
- System.out.println("sorted arrays: \n " + Arrays.toString(x1)
- + Arrays.toString(y1));
- for (int i = 0; i <= x1.length; i++) {
- if (x1[i] != y1[i])
- return false;
- i++;
- }
- return true;
- }
- // create a method for array input
- // create sorting method
- // create isEquivalent method
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- public static void main(String args[]) {
- final String TITLE = "CSC220 Homework 2: Equivalent Arrays";
- final String CONTINUE_PROMPT = "Do this again? [Y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- process(sc, args);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Add Comment
Please, Sign In to add comment