In java jGrasp, please follow the guidelines exactly /* In this exercise, we wil
ID: 3786609 • Letter: I
Question
In java jGrasp, please follow the guidelines exactly /* In this exercise, we will explore some more common array algorithms. Complete the methods below. You will need to write your own code to test the methods. */ public class ArrayTraversalAlgorithms { /* Replace all the occurrences of a value with some new value. You cannot use a for each loop because you are changing the contents of the array. Remember arrays are pass by reference so you do not need to return the array in order for the changes to be seen by the caller. In the method below target represents the old value and the variable replacement represents the new value. */ public static void replaceAll(int[] list, int target, int replacement) { } /* Overload the replaceAll method to accept an array of String instead of ints. Replace all occurrences of a string value with some new string value. In the method below target represents the old value and the variable replacement represents the new value. */ public static void replaceAll(String[] list, String target, String replacement) { } /* Two arrays are equivalent in value if they have the same length and store the same sequence of values. This method will take 2 arrays as parameters and will return a boolean indicating whether or not the two arrays are equal. */ public static boolean equals(int[] list1, int[] list2) { } /* Consider the task of reversing an array. One approach might be to create a new array and to the store the values from the first array into the second array in reverse order. Although this would work it might not always be feasible. This approach could use up a lot of memory if the array was very large. Another approach is to do a series of swaps. Write a method using a series of swaps to reverse the elements in an integer array. */ public static void reverse(int[] list) { } }
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class ArrayTraversalAlgorithms
{
/* Replace all the occurrences of a value with some new value.
You cannot use a for each loop because you are changing the
contents of the array. Remember arrays are pass by reference
so you do not need to return the array in order for the changes
to be seen by the caller.
In the method below target represents the old value and the variable
replacement represents the new value.
*/
public static void replaceAll(int[] list, int target, int replacement)
{
for(int i=0; i<list.length; i++){
if(list[i] == target)
list[i] = replacement;
}
}
/* Overload the replaceAll method to accept an array of String instead of
ints. Replace all occurrences of a string value with some new string
value.
In the method below target represents the old value and the variable
replacement represents the new value.
*/
public static void replaceAll(String[] list, String target, String replacement)
{
for(int i=0; i<list.length; i++){
if(list[i].equals(target))
list[i] = replacement;
}
}
/* Two arrays are equivalent in value if they have the same length and store the
same sequence of values.
This method will take 2 arrays as parameters and will return a boolean
indicating whether or not the two arrays are equal.
*/
public static boolean equals(int[] list1, int[] list2)
{
if(list1.length != list2.length)
return false;
for(int i=0; i<list1.length; i++){
if(list1[i] != list2[i])
return false;
}
return true;
}
/* Consider the task of reversing an array. One approach might be to create
a new array and to the store the values from the first array into the
second array in reverse order. Although this would work it might not
always be feasible. This approach could use up a lot of memory if the array
was very large. Another approach is to do a series of swaps.
Write a method using a series of swaps to reverse the elements in an integer
array.
*/
public static void reverse(int[] list)
{
int i=0;
int j = list.length;
while(i < j){
int temp = list[i];
list[i] = list[j];
list[j] = temp;
i++;
j--;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.