Question

A method called linearSearch(), which takes as parameters an array of int followed by three values...

A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in that range; otherwise, the method returns -1. For example, suppose the following method call is executed:
int[] myArray = {4, 5, 6, 5, 7, 1, 5, 9};
int position = MyArrays.linearSearch(myArray, 5, 2, 7);
After the execution of the above method call, the value stored in variable position will be 3, as the first occurrence of 5 in myArray between positions 2 (inclusive) and 7 (exclusive) is at position 3.
The values stored in the array can appear in any order. You MAY assume that the parameter values satisfy all of the following preconditions:
–   The value of the starting position is greater than or equal to 0, and less than the size of the array.
–   The value of the end position is greater than or equal to 0, and less than or equal to the size of the array.
–   The value of the start position is less than or equal to the value of the end position.
–   The array parameter is not a null reference.
In other words, your method does not have to handle cases where one or more of the above conditions are not satisfied.
A method called linearSearch(), which takes as parameters an array of int followed by a single value of type int, and returns a value of type int. The single int parameter represents a key; if this key occurs anywhere in the array, the method returns the position of the first occurrence of the key in the array; otherwise, the method returns -1. For example, suppose the following method call is executed:
int[] myArray = {4, 5, 6, 5, 7, 1, 5, 9};
int position = MyArrays.linearSearch(myArray, 5);
After the execution of the above method call, the value stored in variable position will be 1, as the first occurrence of 5 in the array myArray is at position 1.
The values stored in the array can appear in any order. You MAY assume that the array parameter is not a null reference; in other words, your method does not have to handle cases where the above condition is not satisfied.
A method called equals(), which takes as parameters two arrays of int and returns a value of type boolean. This boolean value is true if the two parameter arrays are equal, false otherwise. Two arrays are equal if and only if they contain the same elements in the same order; that is, in order for two arrays to be equal, they must contain the same number of elements, and for every valid position i in these arrays, the value stored at position i in the first array must be equal to the value stored at position i in the second array. Note that if both arrays are null references, they are also considered equal.
For example, suppose the following method call is executed:
int[] left = {1, 3, 3};
int[] right = {1, 3, 3};
boolean same = MyArrays.equals(left, right);

After the execution of the above method call, the value stored in variable same will be true, as arrays left and right contain the same values in the same order. Your method MUST correctly handle the case where one of the two parameter arrays is a null reference, but not the other.
A method called fill(), which takes as parameters an array of int followed by three values of type int, and returns nothing. The first parameter represents an arbitrary value, the second int parameter represents a starting position, and the third int parameter represents an end position. The method then assigns a value equal to the first int parameter to each element of the array whose position is between the starting position (inclusive) and the end position (exclusive).
For example, suppose the following method call is executed:
int[] myArray = {2, 3, 5, 7, 11, 13, 17, 19};
MyArrays.fill(myArray, 0, 2, 6);
After the execution of the above method call, the contents of the array whose address is stored in variable myArray will be 2, 3, 0, 0, 0, 0, 17, 19 , as every element whose position is between 2 (inclusive) and 6 (exclusive) is replaced with a 0.
Note that if the starting position is equal to the end position, then none of the values stored in the array are changed.
You MAY assume that the parameter values satisfy all of the following preconditions:
–   The value of the starting position is greater than or equal to 0, and less than the size of the array.
–   The value of the end position is greater than or equal to 0, and less than or equal to the size of the array.
–   The value of the start position is less than or equal to the value of the end position.
–   The array parameter is not a null reference.
In other words, your method does not have to handle cases where one or more of the above conditions are not satisfied.
A method called fill(), which takes as parameters an array of int followed by a single value of type int, and returns nothing. The method then assigns a value equal to the first int parameter to each element in the array.
For example, suppose the following method call is executed:
int[] myArray = {2, 3, 5, 7, 11, 13, 17, 19};
MyArrays.fill(myArray, 0);
After the execution of the above method call, the contents of the array whose address is stored in variable myArray will be 0, 0, 0, 0, 0, 0, 0, 0 , as every element in the array is replaced
with a 0.
You MAY assume that the array parameter is not a null reference; in other words, your method does not have to handle cases where the above condition is not satisfied.
A method called copyOf(), which takes as parameters an array of int followed by two values of type int, and returns an array of int. The first int parameter represents a starting position, and the third int parameter represents an end position. This method creates a new array, and copies all elements of the parameter array whose positions are between the starting index (inclusive) and the end index (exclusive) so that the order in which the values appear in the new array is the same as the order in which they appear in the parameter array.
For example, suppose the following method call is executed:

int[] myArray = {2, 3, 5, 7, 11, 13, 17, 19};
int[] copy = MyArrays.copyOf(myArray, 2, 6);
After the execution of the above method call, the contents of the array whose address is stored in variable copy will be 5, 7, 11, 13 , as every element in myArray whose position is between 2 (inclusive) and 6 (exclusive) is copied to the new array.
If the starting position is equal to the end position, then none of the values stored in the parameter array are copied. The size of the array returned by this method is exactly equal to the number of elements copied from the parameter array.
You MAY assume that the parameter values satisfy all of the following preconditions:
–   The value of the starting position is greater than or equal to 0, and less than the size of the array.
–   The value of the end position is greater than or equal to 0, and less than or equal to the size of the array.
–   The value of the start position is less than or equal to the value of the end position.
–   The array parameter is not a null reference.
In other words, your method does not have to handle cases where one or more of the above conditions are not satisfied.
A method called copyOf(), which takes as its only parameter an array of int, and returns an array of int. This method creates a new array whose length is the same as the parameter array, and copies all elements of the parameter array into the new array, so that the order in which the values appear in the new array is the same as the order in which they appear in the parameter array.
For example, suppose the following method call is executed:
int[] myArray = {2, 3, 5, 7, 11, 13, 17, 19};
int[] copy = MyArrays.copyOf(myArray);
After the execution of the above method call, the contents of the array whose address is stored in variable copy will be 2, 3, 5, 7, 11, 13, 15, 19 , as every element of the original array is copied to the new array.
Subsequent changes to the elements stored in the array returned by this method MUST NOT affect the parameter array; conversely, subsequent changes to the elements stored in the parameter array MUST NOT affect the array returned by this method.
You MAY assume that the array parameter is not a null reference; in other words, your method does not have to handle cases where the above condition is not satisfied.
A method called sort(), which takes as parameters an array of int followed by two values of type int, and returns nothing. The first int parameter represents a starting position, and the third int parameter represents an end position. The method modifies the array so that the values occurring between the start position (inclusive) and the end position (exclusive) appear in increasing order.
There are multiple ways to sort the elements of an array. However, for the purposes of this assign- ment, you MUST implement the following simple sorting algorithm:
–   Set i to be the starting position
–   Find the minimum element occurring at a position between i (inclusive) and the end position (exclusive), and swap this element with the one at position i.
–   If i is equal to the end position minus 1, then stop; otherwise, increment i and go back to the previous step.
For example, suppose the following method call is executed:

int[] myArray = {6, 2, 5, 7, 1, 8, 4, 3};
MyArrays.sort(myArray, 2, 6);
After the execution of the above method call, the contents of the array whose address is stored in variable myArray will be 6, 2, 1, 5, 7, 8, 4, 3 , as every element in myArray whose position is between 2 (inclusive) and 6 (exclusive) is sorted in increasing order.
You MAY assume that the parameter values satisfy all of the following preconditions:
–   The value of the starting position is greater than or equal to 0, and less than the size of the array.
–   The value of the end position is greater than or equal to 0, and less than or equal to the size of the array.
–   The value of the start position is less than or equal to the value of the end position.
–   The array parameter is not a null reference.
In other words, your method does not have to handle cases where one or more of the above conditions are not satisfied.
A method called sort(), which takes as its only parameter an array of int, and returns nothing. The method modifies the array so that all the values it contains appear in increasing order. This method MUST implement the same sorting algorithm as the one implemented by the other sort() method you wrote.
For example, suppose the following method call is executed:
int[] myArray = {6, 2, 5, 7, 1, 8, 4, 3};
MyArrays.sort(myArray);
After the execution of the above method call, the contents of the array whose address is stored in variable myArray will be 1, 2, 3, 4, 5, 6, 7, 8 , as every element in myArray is sorted in increasing order.
You MAY assume that the array parameter is not a null reference; in other words, your method does not have to handle cases where the above condition is not satisfied.
A method called toString(), which takes as its only parameter an array of int, and returns a single String. This method produces a textual representation of the contents of the parameter array. This textual representation consists of the concatenation of the following elements:
–   The String "["
–   The text representation of each element in the parameter array; each pair of adjacent elements is separated by the String ", "
–   The String "]"
In the textual representation of the parameter array, the array elements occur in the same order as in the parameter array.
For example, suppose the following method call is executed:
int[] myArray = {6, 2, 5, 7, 1, 8, 4, 3};
String text = MyArrays.toString(myArray);
After the execution of the above method call, the String stored in in variable text will be "[6, 2, 5, 7, 1, 8, 4, 3]".
If the array parameter is a null reference, your method MUST return the String "null".
A test program is provided so that you can verify that your methods work properly. This test program consists of a single class called MyArraysTest, and is saved in a file called MyArraysTest.java.

Save your class in a file called MyArrays.java, and submit this file to myCourses. You MUST NOT
submit the MyArraysTest.java file.

What To Submit

MyArrays.java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have implemented the following static methods for the "MyArrays" class::

1> public static int linearSearch(array, key, start, end) :- This method returns position of key element, if it is found within the start and end index of array. Otherwise reurns -1.

2> public static int linearSearch(array, key) :- This method returns position of key element, if it is found in the array. Otherwise reurns -1.

3> public static boolean equals(array, array1) :- This method returns true if both the array elements are equal. Otherwise returns false.

4> public static void fill(array, fillElement, start, end) :- This method fill the element within the start and end index of array.

5> public static void fill(array, fillElement) :- This method fill the element in all the positiones of the array.

6> public static int[] copyOf(array, start, end) :- This method returns an new array which contains the elements of array which is passed to the method and copied elements are from start to end index of passed array.

7> public static int[] copyOf(array) :- This method returns an new array which contains all the elements of array which is passed to the method.

8> public static void sort(array, start, end) :- This method sort the elements of the array which is passed to the method and elements will be sorted from start to end index of the passed array.

9> public static void sort(array) :- This method sort the all elements of the array which is passed to the method.

10> public static String toString(array) :- This method returns a string which contains the elemets of the passed array.

MyArrays.java File:-


public class MyArrays {
    
    /**
     * This method returns position of the element given by start and end index from the array
     * @param myArray
     * @param key
     * @param start
     * @param end
     * @return position of the key element
     */
    public static int linearSearch(int[] myArray, int key, int start, int end) {
        
        // stor position of key element
        int position = -1;
        
        for(int i = start; i<end; i++){
            
            if(myArray[i] == key){
                // store position of key element
                position = i;
            }
        }
        
        return position;
    }

    
    /**
     * This method returns the position of key element from whole array
     * @param myArray
     * @param key
     * @return position of key element 
     */
    public static int linearSearch(int[] myArray, int key) {
        
        // stor position of key element
        int position = -1;
        
        for(int i = 0; i<myArray.length; i++){
            
            if(myArray[i] == key){
                // store position of key element
                position = i;
            }
        }
        
        return position;
    }
    
    
    /**
     * This method returns true if both the array are equal otherwise false.
     * @param myArray
     * @param myArray1
     * @return boolean value 
     */
    public static boolean equals(int[] myArray, int[] myArray1) {
        
        // if both array are null return true
        if(myArray == null && myArray1 == null)
            return true;
        
        // if anyone array is null return false
        if(myArray == null || myArray1 == null)
            return false;
        
        // now if length of both arrays are not equal the returns false
        if(myArray.length != myArray1.length)
            return false;
        
        // now compare each element of aaray1 with another array
        for(int i=0; i<myArray.length; i++){
            
            // if array elements are not equal return false
            if(myArray[i] != myArray1[i])
                return false;
        }
        
        return true;
        
    }

    
    /**
     * This method fill the key element of with the given indexes(start and end) element into the array
     * @param myArray
     * @param key
     * @param start
     * @param end 
     */
    public static void fill(int[] myArray, int key, int start, int end) {
        
        for(int i=start; i<end; i++){
            // replace each element of array with key element
            myArray[i] = key;
        }
       
    }

    /**
     * This method fill key element with all the elements of array
     * @param myArray
     * @param key 
     */
    public static void fill(int[] myArray, int key) {
        
        for(int i=0; i<myArray.length; i++){
            // fill key element
            myArray[i] = key;
        }
    }

    
    /**
     *  This method return copy array from the given array with start and end index elements
     * @param myArray
     * @param start
     * @param end
     * @return 
     */
    public static int[] copyOf(int[] myArray, int start, int end) {
        
        // create copy array
        int copyArray[] = new int[Math.abs(start - end)];
        
        for(int i=start,j=0; i<end; i++,j++){
            // store elements into the copy array
            copyArray[j] = myArray[i];
        }
        
        return copyArray;
    }

    
    /**
     * This method returns a copy array from the given array
     * @param myArray
     * @return copied array of passed array
     */
    public static int[] copyOf(int[] myArray) {
        
        // create copy array
        int copyArray[] = new int[myArray.length];
        
        for(int i=0,j=0; i<myArray.length; i++,j++){
            // store elements into the copy array
            copyArray[j] = myArray[i];
        }
        
        return copyArray;
    }

    /**
     * This method sort the array from start to end index of the array
     * @param myArray
     * @param start
     * @param end 
     */
    public static void sort(int[] myArray, int start, int end) {
        
        // sort the elements using selection sort
        for(int i=start; i<end; i++){
            for(int j=(i+1); j<end; j++){
                
                if(myArray[i] > myArray[j]){
                    
                    // swap the elements
                    int temp = myArray[i];
                    myArray[i] = myArray[j];
                    myArray[j] = temp;
                }
            }
        }
    }
    
    /**
     *  This method sort the all elements of array
     * @param myArray 
     */
    public static void sort(int[] myArray) {
        
        // sort the array using selection sort
        for(int i=0; i<myArray.length; i++){
            for(int j=(i+1); j<myArray.length; j++){
                
                if(myArray[i] > myArray[j]){
                    
                    // swap the elements
                    int temp = myArray[i];
                    myArray[i] = myArray[j];
                    myArray[j] = temp;
                }
            }
        }
    }

    
    /**
     * This method return all the elements in a String format
     * @param myArray
     * @return String contains the all elements of array
     */
    public static String toString(int[] myArray){
        
        // if the array passed as null then returns null string
        if(myArray == null)
            return "null";
        
        String array = "[";
        
        for(int i=0; i<myArray.length; i++){
            
            // dont print , for last element
            if(i == myArray.length-1)
                array += String.valueOf(myArray[i]);
            else
                array += String.valueOf(myArray[i])+", ";
        }
        
        array += "]";
        
        return array;
    }

}

Here, I have attached a java program which demonstrate the MyArrays.java file.

Program:-


public class MyArrays {
    
    /**
     * This method returns position of the element given by start and end index from the array
     * @param myArray
     * @param key
     * @param start
     * @param end
     * @return position of the key element
     */
    public static int linearSearch(int[] myArray, int key, int start, int end) {
        
        // stor position of key element
        int position = -1;
        
        for(int i = start; i<end; i++){
            
            if(myArray[i] == key){
                // store position of key element
                position = i;
            }
        }
        
        return position;
    }

    
    /**
     * This method returns the position of key element from whole array
     * @param myArray
     * @param key
     * @return position of key element 
     */
    public static int linearSearch(int[] myArray, int key) {
        
        // stor position of key element
        int position = -1;
        
        for(int i = 0; i<myArray.length; i++){
            
            if(myArray[i] == key){
                // store position of key element
                position = i;
            }
        }
        
        return position;
    }
    
    
    /**
     * This method returns true if both the array are equal otherwise false.
     * @param myArray
     * @param myArray1
     * @return boolean value 
     */
    public static boolean equals(int[] myArray, int[] myArray1) {
        
        // if both array are null return true
        if(myArray == null && myArray1 == null)
            return true;
        
        // if anyone array is null return false
        if(myArray == null || myArray1 == null)
            return false;
        
        // now if length of both arrays are not equal the returns false
        if(myArray.length != myArray1.length)
            return false;
        
        // now compare each element of aaray1 with another array
        for(int i=0; i<myArray.length; i++){
            
            // if array elements are not equal return false
            if(myArray[i] != myArray1[i])
                return false;
        }
        
        return true;
        
    }

    
    /**
     * This method fill the key element of with the given indexes(start and end) element into the array
     * @param myArray
     * @param key
     * @param start
     * @param end 
     */
    public static void fill(int[] myArray, int key, int start, int end) {
        
        for(int i=start; i<end; i++){
            // replace each element of array with key element
            myArray[i] = key;
        }
       
    }

    /**
     * This method fill key element with all the elements of array
     * @param myArray
     * @param key 
     */
    public static void fill(int[] myArray, int key) {
        
        for(int i=0; i<myArray.length; i++){
            // fill key element
            myArray[i] = key;
        }
    }

    
    /**
     *  This method return copy array from the given array with start and end index elements
     * @param myArray
     * @param start
     * @param end
     * @return 
     */
    public static int[] copyOf(int[] myArray, int start, int end) {
        
        // create copy array
        int copyArray[] = new int[Math.abs(start - end)];
        
        for(int i=start,j=0; i<end; i++,j++){
            // store elements into the copy array
            copyArray[j] = myArray[i];
        }
        
        return copyArray;
    }

    
    /**
     * This method returns a copy array from the given array
     * @param myArray
     * @return copied array of passed array
     */
    public static int[] copyOf(int[] myArray) {
        
        // create copy array
        int copyArray[] = new int[myArray.length];
        
        for(int i=0,j=0; i<myArray.length; i++,j++){
            // store elements into the copy array
            copyArray[j] = myArray[i];
        }
        
        return copyArray;
    }

    /**
     * This method sort the array from start to end index of the array
     * @param myArray
     * @param start
     * @param end 
     */
    public static void sort(int[] myArray, int start, int end) {
        
        // sort all the elements using selection sort
        for(int i=start; i<end; i++){
            for(int j=(i+1); j<end; j++){
                
                if(myArray[i] > myArray[j]){
                    
                    // swap the elements
                    int temp = myArray[i];
                    myArray[i] = myArray[j];
                    myArray[j] = temp;
                }
            }
        }
    }
    
    /**
     *  This method sort the all elements of array
     * @param myArray 
     */
    public static void sort(int[] myArray) {
        
        // sort all the elements using selection sort
        for(int i=0; i<myArray.length; i++){
            for(int j=(i+1); j<myArray.length; j++){
                
                if(myArray[i] > myArray[j]){
                    
                    // swap the elements
                    int temp = myArray[i];
                    myArray[i] = myArray[j];
                    myArray[j] = temp;
                }
            }
        }
    }

    
    /**
     * This method return all the elements in a String format
     * @param myArray
     * @return String contains the all elements of array
     */
    public static String toString(int[] myArray){
        
        // if the array passed as null then returns null string
        if(myArray == null)
            return "null";
        
        String array = "[";
        
        for(int i=0; i<myArray.length; i++){
            
            // dont print , for last element
            if(i == myArray.length-1)
                array += String.valueOf(myArray[i]);
            else
                array += String.valueOf(myArray[i])+", ";
        }
        
        array += "]";
        
        return array;
    }
    
    
    /**
     * The main() method tests all the methods of the MyArrays.java class
     * @param args 
     */
        
    public static void main(String[] args) {
        int myArray[]= {10, 2, 1, 8, 3, 0};
        int myArray1[]= {10, 2, 1, 7, 3, 1};
        
        // calling linearSearch(array, key, start, end) method
        System.out.println("Array : "+MyArrays.toString(myArray));
        int position = MyArrays.linearSearch(myArray, 3, 0, 6);
        System.out.println("Result of MyArrays.linearSearch(myArray, 3, 0, 6) is "+position);
        
        System.out.println();
        
        // calling linearSearch(array, key) method
        System.out.println("Array : "+MyArrays.toString(myArray));
        int position1 = MyArrays.linearSearch(myArray, 3);
        System.out.println("Result of MyArrays.linearSearch(myArray, 3) is "+position);
        
        System.out.println("\n");
        
        // calling equals(myArray, myArray1) method
        System.out.println("Array1 : "+MyArrays.toString(myArray));
        System.out.println("Array2 : "+MyArrays.toString(myArray1));
        boolean same = MyArrays.equals(myArray, myArray1);
        System.out.println("Result of MyArrays.equals(myArray, myArray1) is "+same);
        
        System.out.println("\n");
        
        // calling fill(array, fillElement, start, end) method
        System.out.println("Array : "+MyArrays.toString(myArray));
        MyArrays.fill(myArray, 0, 2, 6);
        System.out.println("Resultant Array after calling MyArrays.fill(myArray, 0, 2, 6) : "+MyArrays.toString(myArray));
        
        System.out.println();
        
        // calling fill(array, fillElement) method
        System.out.println("Array : "+MyArrays.toString(myArray));
        MyArrays.fill(myArray, 0);
        System.out.println("Resultant Array after calling MyArrays.fill(myArray, 0) : "+MyArrays.toString(myArray));
        
        
        System.out.println("\n");
        
        // calling copyOf(array, start, end) method
        System.out.println("Array : "+MyArrays.toString(myArray));
        int copyArray[] = MyArrays.copyOf(myArray, 2, 6);
        System.out.println("Copied array after calling MyArrays.copyOf(myArray, 2, 6) : "+MyArrays.toString(copyArray));
        
        System.out.println();
        
        // calling copyOf(array) method
        System.out.println("Array : "+MyArrays.toString(myArray));
        int copyArray1[] = MyArrays.copyOf(myArray);
        System.out.println("Copied array after calling MyArrays.copyOf(myArray) : "+MyArrays.toString(copyArray1));
        
        
        System.out.println("\n");
        
        // calling sort(array, start, end) method
        System.out.println("Array : "+MyArrays.toString(myArray1));
        MyArrays.sort(myArray1, 2, 6);
        System.out.println("After calling MyArrays.sort(myArray1, 2, 6) : "+MyArrays.toString(myArray1));
        
        System.out.println();
        
        // calling copyOf(array) method
        System.out.println("Array : "+MyArrays.toString(myArray1));
        MyArrays.sort(myArray1);
        System.out.println("After calling MyArrays.sort(myArray1) : "+MyArrays.toString(myArray1));
        
        System.out.println("\n");
        
        // call the toString() method by passing null
        System.out.println("myArray is "+MyArrays.toString(null));
    }

}

Here, in main() method we call the toString() method by passing nnull and it will return "null" string.

Output:-

run: Array : [10, 2, 1, 8, 3, 0] Result of MyArrays.linearSearch (myArray, 3, 0, 6) is 4 Array : [10, 2, 1, 8, 3, 0] Result o

According to the output, we can see that when we pass "null" in toString() method then method returns "null" string.

So I have include all the test cases for all the methods of MyArrays.java class.

I hope you will understand the above program.

Do you feel needful and useful then please upvote me.

Thank you.

Add a comment
Know the answer?
Add Answer to:
A method called linearSearch(), which takes as parameters an array of int followed by three values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can...

    in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can verily your subscription Sign In ASSIGNMENT Unit 9 Methods, Arrays, and the Java Standard Class Library Reimplementing the Arrays class (60 points) The Arrays class, which is also part of the Java standard class library, provides various class methods that perform common tasks on arrays, such as searching and sorting. Write a public class called MyArrays, which provides some of the functionality which is...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • In java, write method, shuffle, which accepts an array of int, creates a copy of the...

    In java, write method, shuffle, which accepts an array of int, creates a copy of the formal parameter, shuffles the copy, then returns the copied, shuffled array, leaving the formal parameter unchanged. Shuffling is a process of swapping each element of array with another element randomly chosen from the array. For testing the shuffle method have main call shuffle, repeatedly having it shuffle its previously returned array until the returned (shuffled) array is identical (equal) to the original array that...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT