nate23nate23

hw2

Sep 23rd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. /*
  2.  * Name: Nate Wheeler
  3.  * Date: sept 23, 2016
  4.  * Course Number: csc220
  5.  * Course Name: data structures
  6.  * Problem Number: hw2
  7.  * Email: nate23nate23@gmail.com
  8.  * Short Description of the Problem:
  9.  *  Write a method, isEquivalent, that checks
  10.  *  whether two int arrays are equivalent. Two
  11.  *  arrays are equivalent if they have the same
  12.  *  length and contain the same values in any order.
  13.  */
  14.  
  15. import java.util.Arrays;
  16. import java.util.Scanner;
  17.  
  18. public class EquivalentArrays {
  19.     static int[] x;
  20.     static int[] y;
  21.  
  22.     private static void process(Scanner sc, String args[]) {
  23.         // make an array
  24.  
  25.         System.out.print("Enter array size: ");
  26.         x = new int[sc.nextInt()];
  27.         System.out.println("Processing " + Arrays.toString(x) + " ...");
  28.         System.out.println("Please insert numbers for the array:");
  29.         int c = 0;
  30.         while (c < x.length) {
  31.             x[c] = sc.nextInt();
  32.             c++;
  33.         }
  34.         System.out.println("New array parameters: " + Arrays.toString(x));
  35.         System.out.println("Please Enter Size of Next Array: ");
  36.         y = new int[sc.nextInt()];
  37.         System.out.println("Processing " + Arrays.toString(y) + " ...");
  38.         int c1 = 0;
  39.         while (c1 < y.length) {
  40.             y[c1] = sc.nextInt();
  41.             c1++;
  42.         }
  43.         System.out.println("New array parameters: " + Arrays.toString(y));
  44.         // sort(x,y);
  45.         System.out.println(Arrays.toString(x) + "\n" + Arrays.toString(y));
  46.         // if(isEquivalent(x,y)==true)
  47.         // System.out.println("The 2 Arrays are equivalent.");
  48.         if (isEquivalent(x, y) != true)
  49.             System.out.println("The 2 Arrays are inequivalent.");
  50.         else
  51.             System.out.println("The 2 Arrays are equivalent.");
  52.         sc.nextLine();
  53.     }
  54.  
  55.     public static void sort(int[] x1, int[] y1) {
  56.         Arrays.sort(x1);
  57.         Arrays.sort(y1);
  58.         for (int i = 0; i <= x.length; i++) {
  59.             x[i] = x1[i];
  60.             y[i] = y1[i];
  61.         }
  62.     }
  63.  
  64.     public static boolean isEquivalent(int[] x1, int[] y1) {
  65.         if (x1.length != y1.length)
  66.             return false;
  67.         Arrays.sort(x1);
  68.         Arrays.sort(y1);
  69.         System.out.println("sorted arrays: \n " + Arrays.toString(x1)
  70.                 + Arrays.toString(y1));
  71.         for (int i = 0; i <= x1.length; i++) {
  72.             if (x1[i] != y1[i])
  73.                 return false;
  74.             i++;
  75.         }
  76.         return true;
  77.     }
  78.  
  79.     // create a method for array input
  80.     // create sorting method
  81.     // create isEquivalent method
  82.     private static boolean doThisAgain(Scanner sc, String prompt) {
  83.         System.out.print(prompt);
  84.         String doOver = sc.nextLine();
  85.         return doOver.equalsIgnoreCase("Y");
  86.     }
  87.  
  88.     public static void main(String args[]) {
  89.         final String TITLE = "CSC220 Homework 2: Equivalent Arrays";
  90.         final String CONTINUE_PROMPT = "Do this again? [Y/N] ";
  91.  
  92.         System.out.println("Welcome to " + TITLE);
  93.         Scanner sc = new Scanner(System.in);
  94.         do {
  95.             process(sc, args);
  96.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  97.         sc.close();
  98.         System.out.println("Thank you for using " + TITLE);
  99.     }
  100.  
  101. }
Add Comment
Please, Sign In to add comment