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

I\'m in intro to Java can you write this program in printf (Identical arrays) Th

ID: 3702116 • Letter: I

Question

I'm in intro to Java can you write this program in printf

(Identical arrays) The arrays myArray1 and myArray2 are identical if they
have the same contents. Write a method that returns true if myArray1 and
myArray2 are identical, using the following header:

public static boolean isIdentical(int[] myArray1, int[] myArray2)

Note that "identical" means that the same numbers are in each array but not
necessarily in the same order. Hint: Sort the arrays before comparing their elements.

Write a test program that prompts the user to enter two arrays of integers
and displays whether the two are identical. The first number in the input
indicates the number of the elements in the list. This number is not part of
the list. Here are two sample runs.

Enter myArray1: 5 2 5 6 6 1
Enter myArray2: 5 5 2 6 1 6
Two arrays are identical.

Enter myArray1: 5 5 5 6 6 1
Enter myArray2: 5 5 2 6 1 6
Two arrays are not identical.

Use only printf for all printings, thanks in advance

Explanation / Answer


package identicalarrays;

import java.util.Scanner;


public class Identicalarrays
{
static int n=1000;
static int original_size_of_array1=0;
static int original_size_of_array2=0;
static int array1[]=new int[n];
static int array2[]=new int[n];
  
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.printf(" Enter MyArray1: ");
//Entering array1 values;
for(int i=0;i<n;i++){
if(i==0)
{
array1[i]=s.nextInt();
original_size_of_array1=array1[i];
n=original_size_of_array1+1;
}
else
{
array1[i]= s.nextInt();
}
}
n=1000;
System.out.printf(" Enter MyArray2: ");
//Entering array2 values;
for(int j=0;j<n;j++){
if(j==0)
{
array2[j]=s.nextInt();
original_size_of_array2=array2[j];
n=original_size_of_array2+1;
}
else
{
array2[j]= s.nextInt();
}
}   
//sorting array1
  
for (int i = 1; i <= original_size_of_array1; i++) {
for (int j = i + 1; j <= original_size_of_array1; j++) {
int tmp = 0;
if (array1[i] > array1[j]) {
tmp = array1[i];
array1[i] = array1[j];
array1[j] = tmp;
}
}
}

//sorting array2
  
for (int i = 1; i <=original_size_of_array2; i++) {
for (int j = i + 1; j <= original_size_of_array2; j++) {
int tmp = 0;
if (array2[i] > array2[j]) {
tmp = array2[i];
array2[i] = array2[j];
array2[j] = tmp;
}
}
}
  
boolean identical= isIdentical(array1,array2);
if(identical==true)
{
System.out.printf(" Two Arrays are Identical. ");  
}
else
{
System.out.printf(" Two Arrays are not Identical. ");   
}
}//End of Main Method
  
public static boolean isIdentical(int[] myArray1, int[] myArray2)
{
//comparing the two arrays values
int count=0;
if(original_size_of_array1!=original_size_of_array2)
{
return false;
}
else
{
for(int i=1;i<=original_size_of_array1;i++)
{
if(array1[i]==array2[i])
{
count+=1;
}
else
{
break;
}
}
if(count!=original_size_of_array1)
{
return false;
}
else
{
return true;
}
}
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote