Part A: Create a file called Lab8.java. In this file, write the following method
ID: 675415 • Letter: P
Question
Part A: Create a file called Lab8.java.
In this file, write the following methods.
1. Write a method that prints the elements of an integer array, all on the same line. Separate the elements by at least one space. Here is the method header:
public static void printIntArray( int[ ] array)
2. Write a method that generates a random number between min and max (inclusive) for each element in the array.
public static void fillArray(int[] array, int min, int max)
3. Write a method that takes an integer array and an integer value and searches (using the linear search method) the array for the value. If the value is found in the array, the method should return true. If the value is not found, the method should return false.
public static boolean linearSearch(int [] array, int value)
4. Write a method that takes an integer array and an integer value and searches the array for the value. If the value is found in the array, the method should return the first position of the value in the array. If the value is not found, the method should return -1.
public static int linearPositionSearch(int [] array, int value)
5. Write a method that takes an integer array and sorts the array using the bubble sort algorithm.
public static void bubbleSort(int [] array)
6. Write a method that takes an integer array and sorts the array using the insertion sort algorithm.
public static void insertionSort(int [] array)
7. Write a method that takes a sorted integer array and an integer value and searches the array for the value, using the binary search method. If the value is found in the array, the method should return true. If the value is not found, the method should return false.
public static boolean binarySearch(int [] array, int value)
8. (Overloaded method) Write another method that prints the elements of an integer array. This version has two parameters. The parameter count determines the number of elements that are printed on each line. For example, if count = 5, the method should print 5 elements per line.
public static void printIntArray(int[] array, int count)
I know what I posted was a lot, but I have no idea what to do, and I am clueless to where I need to start with this at all. So any help and any instruction on what I need to do, would be extermley appreciated, because like I said I am lost, and nothing about this makes sense to me, so I am hoping someone can help show and explain to me what is happening and how to actually do arrays. Thank you in advance for any help.
Explanation / Answer
1.
Compose a Java project named Test8.java that contains a principle system. Inside the principle technique, compose proclamations that will call the strategies from Lab8.java. You ought to take a stab at calling every technique a few times to ensure the system works for all potential outcomes.
Here’s what I mention to put in Test8.java:
1. Create an integer array named array1 with 25 elements.
2. Fill the array with random numbers in the range from 1 to 100 inclusive. Hint: use a for loop and Math.random( ).
3. Call printIntArray to print array1.
4. Call the second version of printIntArray to print array1, but this time with only 5 numbers per line.
5. Create an integer array named array2 with 50 elements.
6. Fill the array with random numbers in the range from 1 to 1000 inclusive.
7. Call the second version of printIntArray to print array2 with 10 numbers per line.
8. Call evenElements with array1.
9. Call evenElements with array2.
10. Create an integer array named array3 with 25 elements.
11. Copy the elements from array1 into array3.
12. Call compareArrays with array1 and array3.
13. Change element [24] of array3 to a different number. Hint: just add 1 to it.
14. Call compareArrays again with array1 and array3.
15. Call compareArrays with array2 and array3.
16. Call findMax with array1.
2.
public static int getRandom(int min, int max)
{
return ((int) (Math.random() * (max - min + 1))) + min;
}
public static void fillArray(int[] toFill)
{
boolean[] beenUsed = new boolean[46];
for (int i = 0; i < 45; i++)
{
beenUsed[i] = false;
}
for (int j = 0; j < toFill.length; j++)
{
int ran;
do {
ran = getRandom(1, 45);
} while (beenUsed[ran]);
toFill[j] = ran;
beenUsed[ran] = true;
}
}
3.
public int linearSearch(int[ ] array, int value)
{
for(int i = 0; i < array.length; i++)
if(array[i] = = value) return i;
return -1;
}
4.
public static int linearPositionSearch(int [ ] array, int value)
{
for (int index = 0; index <array.length; index++)
{
if ( array[index] = = value )
return index;
}
return -1;
}
5.
public static void bubblesort(int[] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
6.
public static void insertionSort(int[] array)
{
for (int i = 1; i < array.length; i++)
{
int j = i;
while (j > 0 && array[j] < array[j - 1])
{
int temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp; j--;
}
}
}
}