Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / questions and answers / i\'m in

ID: 3688046 • Letter: H

Question

home / study / engineering / computer science / questions and answers / i'm in java one and need help with this code. please ...

Question

I'm in java one and need help with this code. please follow the given directions and test your program to make sure it works as it is required

Programming Concepts

1. Searching and sorting arrays

Assignment Description

Write a program that will have the user enter in values for two arrays. Then sort them, and check to see if they're the same.

Assignment Suggestions

For this assignment only, you may use the Arrays library to sort and print the arrays. To use the library, do:

import java.util.Arrays;at the top of your program.

To sort an array, you can do the following:

int[] x = { 9, 8, 7, 6 }; Arrays.sort(x);

To print an array with the Arrays library, you can do the following:

int[] x = { 9, 8, 7, 6 };

System.out.println( Arrays.toString(x) );

which outputs: [9, 8, 7, 6]

Assignment Requirements

Do NOT use Arrays.equals() to test to see if the two arrays are equal. You will not be given credit if you do this.

>>>>Instead, use a loop and a flag variable to determine if they are equal.

Output Example

user@loki:~$ java SameArray

Please enter in 5 integers for array #1: 1 2 3 4 5   Please enter in 5 integers for array #2: 5 4 3 2 1

Array 1: [1, 2, 3, 4, 5] Array 2: [1, 2, 3, 4, 5]

The arrays are the same.

user@loki:~java SameArray
Please enter in 5 integers for array #1: 1 2 3 4 5

Please enter in 5 integers for array #2: 9 8 7 6 5

Array 1: [1, 2, 3, 4, 5]

Array 2: [5, 6, 7, 8, 9]

The arrays are not the same.

Explanation / Answer

import java.util.Arrays;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class ArraysSortSearch {

   /**
   * @param args
   */
   public static void main(String[] args) {
       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           int arr1[] = new int[5];
           int arr2[] = new int[5];

           System.out.print("Please enter in 5 integers for array #1:");
           for (int i = 0; i < 5; i++) {

               arr1[i] = scanner.nextInt();
           }

           System.out.print("Please enter in 5 integers for array #2:");
           for (int i = 0; i < 5; i++) {

               arr2[i] = scanner.nextInt();
           }

           Arrays.sort(arr1);
           Arrays.sort(arr2);
           System.out.println("Array 1:" + Arrays.toString(arr1));
           System.out.println("Array 2:" + Arrays.toString(arr2));

           boolean flag = false;

           for (int i = 0; i < 5; i++) {

               if (arr1[i] != arr2[i]) {
                   flag = true;
                   break;
               }
           }

           if (flag)
               System.out.println("The arrays are not the same.");
           else
               System.out.println("The arrays are the same.");

       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

OUTPUT:

Please enter in 5 integers for array #1:1 2 3 4 5
Please enter in 5 integers for array #2:5 4 3 2 1
Array 1:[1, 2, 3, 4, 5]
Array 2:[1, 2, 3, 4, 5]
The arrays are the same.


Please enter in 5 integers for array #1:1 2 3 4 5
Please enter in 5 integers for array #2:9 8 7 6 5
Array 1:[1, 2, 3, 4, 5]
Array 2:[5, 6, 7, 8, 9]
The arrays are not the same.