Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help adding the above methods to this code: public class IntArrayAlgorithms { public static void main(String[] args) { int[] arr = {1, 2,

image text in transcribedI need help adding the above methods to this code:

public class IntArrayAlgorithms {

public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7}; System.out.print("The result of incrementing all elements in array " + printArray(arr) + " is "); incrementAll(arr); System.out.println(printArray(arr) + ".");

int[] arr1 = {10, 35, 81, 15, 26, 35, 42}; System.out.println("The index of the value 35 in array " + printArray(arr1) + " is " + indexOf(arr1, 35) + ".");

int[] arr2 = {5, 10, 17, 26, 14, 10, 42}; System.out.print("The result of replacing all 10's with 20's in array " + printArray(arr2) + " is "); replaceAll(arr2, 10, 20); System.out.println(printArray(arr2) + "."); int[] arr3 = {1, 2, 3, 4, 5, 6, 7}; System.out.print("The result of reversing the elements in array " + printArray(arr3) + " is "); reverse(arr3); System.out.println(printArray(arr3) + ".");

System.out.print("Arrays " + printArray(arr2) + " and " + printArray(arr3) + " are "); System.out.println( (equals(arr2, arr3)) ? "equal." : "not equal."); }

public static void incrementAll(int[] array) { for (int i = 0; i

public static String printArray(int[] data) { String out = "{"; for(int i = 0; i

public static int indexOf(int [] array, int value) { for (int i = 0; i

public static void replaceAll(int[] array, int val1, int val2) { for (int i = 0; i

public static void swap(int [] array, int idx1, int idx2) { int temp = array[idx1]; array[idx1] = array[idx2]; array[idx2] = temp; } public static void reverse(int [] array) { for (int i = 0; i

public static boolean equals(int [] array1, int [] array2) { if (array1.length != array2.length) { return false; } for (int i = 0; i

}

o lastIndexOf: 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 not found. For example, in the array [74, 85, 102, 99, 101, 85, 56), the last index of the value 85 is 5. o range: returns the range of values in an array of integers. The range is defined as 1 more than the difference between the maximum and minimum values in the array. For example, if an array called list contains the values [36, 12, 25, 19, 46, 31, 22), the call of range(list) should return 35 (46-12 + 1). You may assume that the array has at least one element o countinRange: accepts an array of integers, a minimum value, and a maximum value as parameters and returns the count of how many elements from the array fall between the minimum and maximum (inclusive). For example, in the array [14, 1, 22, 17, 36, 7,-43, 5), for minimum value 4 and maximum value 17, there are four elements whose values fall between 4 and 17. Your main method should create arrays to pass into the methods you write and print any results with context. Given the above examples, the expected output from your program would be: o lastIndexOf() will return an int which you should print: The last index of the value 85 in 74, 85, 102, 99, 101, 85, 56} is 5. o range() will return an int which you should print: The range of {36, 12, 25, 19, 46, 31, 22} is 35. o count_in_range) will return an int which you should print: In the array {14, 1, 22, 17, 36, 7, -43, 5}, there are 4 elements whose values fall between 4 and 17. o Use the given printArray method to print array contents: Here is an of how to create an array and then call a method and print the result: int[] arr = {74, 85, 102, 99, 101, 85, 56}; System.out.println("The last index of the value 85 in + printArray(arr) + is " + lastIndexOf(arr, 85) + ".")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions