Question

In: Computer Science

Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...

Details:

Create a class called CompareArrays that determines if two specified integer arrays are equal.

The class should have one static methods:

  • public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false.

NOTE: You are not allowed to use the Arrays.equals method for this programming assignment.

Create a second class called CompareArraysTest that contains the main method, and thoroughly tests the ComparyArrays.compare method. This test class does not need to ask users for input. Just create the needed arrays to ensure that you test the ComparyArrays.compare method well. The thoroughness of your testing in will impact your grade.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

import java.util.*;

/*class definition*/

class CompareArrays

{

    /*static method to compare two arrays*/

    public static boolean compare(int[] arrayOne, int[] arrayTwo)

    {

        boolean check=true;

        /*if lengths are not equal then return false*/

        if(arrayOne.length!=arrayTwo.length)

            return false;

        else

        {

            /*check for same size and same order*/

            for(int i=0;i<arrayOne.length;i++)

            {

                /*if order is missed then return false and break*/

                if(arrayOne[i]!=arrayTwo[i])

                {

                    check=false;

                    break;

                }

            }

            /*return check*/

            return check;

        }

    }

}

/*class to test above method**/

public class CompareArraysTest

{

    /*main method*/

    public static void main(String[] args)

    {

        /*two arrays initialization*/

        int[] arrayOne={12,3,3,4,5};

        int[] arrayTwo={12,3,3,4,5};

        /*method call*/

        boolean check=CompareArrays.compare(arrayOne,arrayTwo);

        /*print equal or not equal*/

        if(check)

            System.out.print("Equal");

        else

            System.out.print("NotEqual");

    }

}




Note:

The above code doesn't read input from the user as mentioned in the question


Related Solutions

in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer...
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list. [B] Edit your class...
Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These...
Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These should be declared as public and you should not use automatic properties to declare them. Your class should have a constructor that takes one integer argument. In the constructor you will code if statements to set one of the three class variables (indicators) to 1 if the number sent to the constructor is either equal to zero, negative, or positive. In the class provide...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for a dynamic array with enough room for both integer arrays, and then copying the elements of both arrays to the new array as follows:             first, copy the elements of the first array up to a given position,             then, copy all the elements of the second array,             then, copy the remaining elements in the first array. The function should have parameters for...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These should be declared as public and you should not use automatic properties to declare them. Your class should have a constructor that takes one integer argument. In the constructor you will code if statements to set one of the three class variables (indicators) to 1 if the number sent to the constructor is either equal to zero, negative, or positive. In the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT