Java program :
MyArrays.java
public class MyArrays
{
public static int linearSearch(int arr[],int key, int start, int end) // linear search with start and end positions of array
{
for(int i=start; i<end; i++)
{
if(arr[i] == key) // if element found then return its index
{
return i;
}
}
return -1; // if element not found return -1
}
public static int linearSearch(int arr[],int key) // linear search of all elements in array
{
for(int i=0; i<arr.length; i++)
{
if(arr[i] == key)
{
return i;
}
}
return -1;
}
public static boolean equals(int arr1[], int arr2[]) // checks if two arrays are equal or not
{
int n1 = arr1.length;
int n2 = arr2.length;
if(arr1==null && arr2==null) // check if both arrays are null
{
return true;
}
else if((arr1!=null && arr2==null) || (arr1==null && arr2!=null) || n1!=n2) // check if one of the array is null
{
return false;
}
else
{
for(int i=0; i<n1; i++)
{
if(arr1[i] != arr2[i])
{
return false;
}
}
return true;
}
}
public static void fill(int arr[], int val, int start, int end) // fills the array with the given value from start to end
{
for(int i=start; i<end; i++)
{
arr[i] = val;
}
}
public static void fill(int arr[], int val) // fills the whole array with the given value
{
for(int i=0; i<arr.length; i++)
{
arr[i] = val;
}
}
public static int[] copyOf(int arr[], int start, int end) // creates a copy of sub-array and returns it
{
int[] array = new int[end-start];
int k=0;
for(int i=start; i<end; i++)
{
array[k]=arr[i];
k++;
}
return array;
}
public static int[] copyOf(int arr[]) // creates a copy of entire array and returns it
{
int[] array = new int[arr.length];
for(int i=0; i<arr.length; i++)
{
array[i]=arr[i];
}
return array;
}
public static void sort(int arr[],int start, int end) // sorts a part of array fom start...end
{
int min, temp;
int i=start;
while(i != end-1)
{
min = arr[i];
for(int j=i; j<end; j++)
{
if(arr[j] < min)
{
min = arr[j];
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i++;
}
}
public static void sort(int arr[]) // sorts the entire array in ascending order
{
int n = arr.length;
int min, temp;
int i=0;
while(i != n-1)
{
min = arr[i];
for(int j=i; j<n; j++)
{
if(arr[j] < min)
{
min = arr[j];
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i++;
}
}
public static String toString(int arr[]) // converts a array to a string
{
String str = "[";
for(int i =0 ;i<arr.length;i++)
{
if(i!=arr.length-1)
{
str = str + Integer.toString(arr[i]) + ", ";
}
else
{
str = str + Integer.toString(arr[i]) + "]";
}
}
return str;
}
}
Screenshot :
in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can...
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...
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...
HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...
Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...
in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...
write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...
I currently have this but it doesn't work for the three item arrays public class Quicksort extends Partitioner { public static void quicksort(int[] values) { if (values == null || values.length < 2) { return; } quicksort(values, 0, values.length - 1); } private static void quicksort(int[] values, int start, int end) { System.out.println(values); System.out.println(start); System.out.println(end); if (values == null || Math.abs(start - end) == 1) { return; } if (end > start) { int pivotValueIndex = partition(values, start, end); quicksort(values,...
JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...
Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course. Assignment Objectives After completing this assignment the student should be able to: Declare and instantiate arrays Access array elements by index Use loops and decisions to manipulate arrays and array elements Write methods that manipulate arrays Write methods that take array arguments Write methods...