subject

Must be in java and have testers b) Shift all elements by one to the right and move the last element into the first position. For example,
1 4 9 16 25 would become 25 1 4 9 16.
c) Replace all even elements with 0.
d) Replace each element except the first and last by the larger of its two neighbors. For the example in
part (b), your output would be 1 9 16 25 25.
e) Remove the middle element if the array length is odd, or the middle two elements if the length is
even. (You will need to test two different arrays here to make sure your method works on both even
and odd length arrays).
f) Move all even elements to the front, otherwise preserving the order of the elements. For the example
in part (b) your output should be 4 16 1 9 25
g) Return the second-largest element in the array. (Your method should return this value, and your
main method should print the value out).
h) Return true if the array is currently sorted in increasing order. (You can use Arrays. sort() to sort an
array before you call your method for testing purposes. Make sure to test both a sorted array and an
unsorted array as input). Your main method should print whether the array was in order or not.
i) Return true if the array contains two adjacent duplicate elements.
j) Return true if the array contains duplicate elements (which need not be adjacent).
starting code:
import java. util. Arrays;
import java. util. Random;
public class ArrayMethods {
//***NOTE that these methods will change the array itself
//part a, fill in this method
public static void swapFirstAndLast(int[] values) {
// save the first element to a temp var
int temp = values[0];
//move the last element to the first position
values[0] = values[values. length-1];
// now put the saved first element into the last position
values[values. length-1] = temp;
}
//part b, fill in this method
public static void shiftRight(int[] values) {
}
//part c, set all even elements to 0.
public static void setEvensToZero(int[] values) {
}
//part d, replace each element except the first and last by larger of two
//around it
public static void largerOfAdjacents(int[] values) {
}
//part e, remove middle el if odd length, else remove middle two els.
public static int[] removeMiddle(int[] values) {
//replace the following line with your answer
//this line needed to compile
return null;
}
//part f - move all evens to front
public static void moveEvensToFront(int[] values) {
}
//part g - return second largest element in array
public static int ret2ndLargest(int[] values) {
// replace this line with your correct return value
return 0;
}
//part H - returns true if array is sorted in increasing order
public static boolean isSorted(int[] values) {
// replace this line with your correct return value
return false;
}
//PART I - return true if array contains 2 adjacent duplicate values
public static boolean hasAdjDuplicates(int[] values) {
// replace this line with your correct return value
return false; //dummy return value
}
//PART J - return true if array contains 2 duplicate values
//duplicates need not be adjacent to return true
public static boolean hasDuplicates(int[] values) {
// replace this line with your correct return value
return false;
}
}

tester code:
import java. util. Arrays;
import java. util. Random;
public class ArrayMethodsTester {
//helper method to print an array
public static void printArray(int[] values) {
System. out. println(Arrays. toString(values));
}
public static void main(String[] args) {
//In your main method you should test your array methods
//Create an array of size 10
// HERE
int[] a = new int[1]; //array of size 1
// Fill the array with random values (use a loop, and a
//Random object)
//Now print the array to show initial values
System. out. println("Initial Array:");
//note the usage of the "toString()" method here to print the array
System. out. println(Arrays. toString(a));
//Could replace the previous line with this:
//printArray(testValues);
//blank line
System. out. println();
//Test methods below this line.
//Test of swapFirstAndLast()
System. out. println("Before call to swapFirstAndLast():");
printArray(a);
//swap first and last element
//this method modifies the array referenced by "testValues"
ArrayMethods. swapFirstAndLast(a);
System. out. println("After call to swapFirstAndLast()");
printArray(a); //printing the same array but it has changed
System. out. println();
//continue with tests as you complete methods ... }
}

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 14:00, michelle7511
Which database model is best used for data warehouse and data mining
Answers: 3
image
Computers and Technology, 23.06.2019 04:31, mjweed3381
Cloud computing service providers manage different computing resources based on the services they offer. which resources do iaas and paas providers not manage? iaas providers do not manage the for the client, whereas paas providers usually do not manage the for their clients. iaas- storage server operating system network paas- applications interafce storage vertualiation
Answers: 2
image
Computers and Technology, 23.06.2019 07:00, lin550
Why were most movies from the late 1890s until the early 1930s only filmed in black and white? there were only a few people who could afford the technology to produce color motion pictures back then. audiences did not want color motion pictures until later. the film used to make color motion pictures often overheated, which was a safety hazard, so it was generally not allowed. color films had to be hand-colored, frame by frame.
Answers: 3
image
Computers and Technology, 23.06.2019 18:30, bsept1018
Report all segments of identity by descent longer than 20 polymorphisms between pairs of individuals in the following cohort of 15 individuals across 49 polymorphisms: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 numeric input 2 points possible (graded) you have 2 attempts to complete the assignment below. for example if the sequence is "bcd", which occurs in "abcdef" , the starting point would be 2 (b), and the finishing point would be 4(d). individuals 7,10 between positions
Answers: 1
You know the right answer?
Must be in java and have testers b) Shift all elements by one to the right and move the last eleme...

Questions in other subjects:

Konu
English, 11.04.2021 01:20
Konu
Mathematics, 11.04.2021 01:20
Konu
Physics, 11.04.2021 01:20