Attached are insturctions and given templete More Arrays Create a class called L
ID: 3774417 • Letter: A
Question
Attached are insturctions and given templete
More Arrays Create a class called Lab8 and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don't forget to turn in your file to Canvas before the end of the lab today. 1. Write a function called median, that takes as parameter a full, sorted array of doubles and returns the median of the list. For a sorted list of odd length, the median is the middle value. For a sorted list length, the median is the average of the two middle values. Make an example function call in your main 2. Write a function called is sorted that takes an array of doubles as a parameter and returns true if the list is in sorted (non-decreasing) order and returns false otherwise. Make an example function call in your main 3. Write a function called findcommon that takes three arrays of positive integers as parameters. The first two array parameters are filled with ints. Fill the third array parameter with all the values that are uniquely in common from the first two arrays and the rest of the array with zeros. For example (a) al contains: 3 8 5 6 5 8 9 2 (b) a20 contains: 5 15 4 673 9 119 3 12 13 14 9 5 3 13 (c) common should contain: 3 56 90 000 4. Write a function called rotateRight that takes an array of integers as an argument and rotates values in the array one to the right (i.e., one forward in position), shifting the value at the end of the array to the front. For example, if the array called list stores [3, 8, 19, 7] before the function is called, it should store C7, 3, 8, 19] after the function is called. Another call on rotateRight would leave the list as [19, 7, 3, 8]. Another call would leave the list as [8, 19, 7, 31 5. Write a function count that takes an array of integers and a target value as parameters and returns the number of occurrences of the target value in the array. For example, if an array called list stores the se- quence of values [3, 5, 2, 1, 92 38, 3, 14, 5, 73] then the following call int n count (list, 3); would return 2 because there are 2 occurrences of the value 3 in the list 6. Write a function called stretch that takes an array of integers as an argument, and returns twice as large as the original that replaces' every integer from the original list with a pair of integers each half the original and then returns it. If a number in the original list is odd, then the first number in the new pair should be one higher than the second so that the sum equals the original number. Fo example, suppose a variable called list stores the sequence of values [18, 7, 4, 24, 11]. The numbe 18 is stretched into the pair (9, 9), the number 7 is stretched into (4, 3), the number 4 is stretched into (2, 2), the number 24 is stretched into (12, 12) and the number 11 is stretched into (6, 5). Thus,the call stretch list) should replace list with the following list which is twice the length of the original: 19, 9, 4, 3, 2, 2, 12, 12, 6, 5]
Explanation / Answer
Hi, I have implemented all methods.
Please let me know in case of any issue.
public class Lab8 {
//findCommon can use this as a helper methods
public static boolean contains(int[] arr, int target) {
for(int i = 0; i < arr.length; i++) {
if (arr[i] == target) return true; //"Im outta here!" condition
}
return false;
}
// 1
public static double median(double[] arr) {
//for odd length array it is the middle value
//for even length arrays it is the average of the 2 middle values
if (!isSorted(arr))
return -1;
int len = arr.length;
if(len % 2 == 0){
return ((arr[len/2]+arr[len/2+1])/2);
}else{
return arr[len/2];
}
}
//2
public static boolean isSorted(double[] arr) {
for(int i = 0; i < arr.length-1; i++) {
if (arr[i] > arr[i+1])
return false;
}
return true;
}
public static void findCommon(int[] arr1, int[] arr2, int[] common) {
int k= 0;
for(int i=0; i<arr1.length; i++){
if(contains(arr2, arr1[i])){
if(!contains(common, arr1[i])){
common[k++] = arr1[i];
}
}
}
while( k < common.length)
common[k++] = 0;
}
public static void rotateRight(int[] arr) {
int len = arr.length;
int last = arr[len-1];
for(int i = len-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = last;
}
public static int count(int[] arr, int target) {
return 1;
}
public static int[] stretch(int[] arr) {
int len = arr.length;
int streched[] = new int[len*2];
int k= 0;
for(int i=0; i<len; i++){
if(arr[i]%2 == 1){
streched[k++] = arr[i]/2+1;
streched[k++] = arr[i]/2;
}
else{
streched[k++] = arr[i]/2;
streched[k++] = arr[i]/2;
}
}
return streched;
}
//all your methods are here
public static void main(String[] args) {
//HOW to create some test cases!
double[] arr1 = { 1, 2.3, 4, 5,6.7, 45.6};
double[] arr2 = {1, 2.3, 4, 6.7, 5, 45.6};
System.out.println(median(arr1));
double[] arr3 = new double[100];
double[] arr4 = new double[100];
for(int i=0; i<arr3.length; i++)
arr3[i] = i;
for(int i=0; i<arr4.length; i++)
arr4[i] = (int)(100*Math.random());
for(double x : arr3)
System.out.print(x+", ");
System.out.println();
for(double x : arr4)
System.out.print(x + ", ");
System.out.println();
System.out.println(isSorted(arr3));
System.out.println(isSorted(arr4));
}
}
/*
Sample run:
5.85
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,
97.0, 28.0, 14.0, 43.0, 55.0, 96.0, 85.0, 92.0, 75.0, 39.0, 14.0, 30.0, 55.0, 7.0, 77.0, 47.0, 42.0, 43.0, 63.0, 73.0, 31.0, 91.0, 13.0, 68.0, 74.0, 65.0, 70.0, 94.0, 84.0, 42.0, 33.0, 7.0, 32.0, 55.0, 46.0, 77.0, 97.0, 65.0, 83.0, 49.0, 67.0, 53.0, 92.0, 31.0, 81.0, 9.0, 87.0, 67.0, 2.0, 46.0, 71.0, 21.0, 66.0, 0.0, 52.0, 38.0, 71.0, 99.0, 7.0, 24.0, 78.0, 84.0, 23.0, 53.0, 3.0, 17.0, 83.0, 42.0, 71.0, 10.0, 42.0, 3.0, 92.0, 46.0, 70.0, 79.0, 90.0, 32.0, 67.0, 26.0, 84.0, 47.0, 89.0, 71.0, 79.0, 13.0, 42.0, 51.0, 66.0, 83.0, 74.0, 22.0, 77.0, 41.0, 51.0, 63.0, 57.0, 99.0, 16.0, 76.0,
true
false
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.