Problem 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true.

Respuesta :

Answer:

Here is the JAVA program:

class Main  {  //class name

public static boolean compare (String[] array1, String[] array2) {  //method that accepts two String arrays as parameters and returns a boolean

 if (array1 == array2)  //if array1 and array2 are equal

  return true;   //returns true

 if (array1 == null || array2 == null)  //if array1 or array 2 is null

  return false;   //returns false

 int n = array1.length;  //computes the length of array1 and stores it in n variable

if (n != array2.length)  //if length of array1 is not equal to that of array2

  return false;   //returns false

 for (int i = 0; i < n; i++) {  //iterates through the arrays

  if (!array1[i].equals(array2[i]))  //if i-th index of array1 is not equal to i-th index of array2

   return false;    }   //returns false

 return true;  }  //returns 2 otherwise

public static void main(String[] args)  {  //start of main method

String[] array1 = { "Hello", "World" };  //declares and initializes array1

String[] array2 = { "Hello", "World"};   //declares and initializes array1

System.out.println(compare(array1, array2));   }  } //calls compare method to check if both string type arrays are equal

Explanation:

The method compare that accepts two String arrays as parameters and returns a boolean true if both arrays contain the same values otherwise returns false.

if (array1 == array2) This checks if both arrays are equal then they contain same values and method returns true.

if (array1 == null || array2 == null)  This is used to check if any of the array is null and if any of the two arrays is null then this means they do not contain same values so method returns false

if (n != array2.length)   This condition checks if the length of both arrays is same.

Next the for loop for (int i = 0; i < n; i++) executes which has an if statement if (!array1[i].equals(array2[i])) that uses equals() method to compare two strings, and returns false if this if condition evaluates to true, otherwise false.

The screenshot of the program along with its output is attached.

Ver imagen mahamnasir

In this exercise we have to use the knowledge of computational language in JAVA to describe the code, like this:

We can find the code in the attached image.

What is string in programming?

Strings are strings of characters that store textual data and, therefore, can store information for the most diverse purposes. The content of a string can represent a fact in itself, or a piece of information.

The code can be written more simply as:

class Main

public static boolean compare (String[] array1, String[] array2) {  

if (array1 == array2)  

 return true;  

if (array1 == null || array2 == null)

 return false;  

int n = array1.length;  

if (n != array2.length)  

 return false;  

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

 if (!array1[i].equals(array2[i]))  

  return false;    }  

return true;  }  

public static void main(String[] args)  {

   String[] array1 = { "Hello", "World" };

String[] array2 = { "Hello", "World"};  

System.out.println(compare(array1, array2));   }  }

See more about JAVA at brainly.com/question/6490447

Ver imagen lhmarianateixeira