Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class
ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . }
Too many question try submitting in different question
package mis;
public class ArrayMethods {
private int[] values;
public ArrayMethods(int[] initialValues){
values=new
int[initialValues.length]; //set size to given array
for(int
i=0;i<values.length;i++){
values[i]=initialValues[i]; //copy one by one each element to value
array
}
}
public void swapFirstAndLast() {
int val=values[values.length-1];
//swap first and last element
values[values.length-1]=values[0];
values[0]=val;
}
public void shiftRight(){
int last=values[values.length-1];
//keep last element
for(int
i=values.length-1;i>0;i--){
values[i]=values[i-1]; //copy prev element to cur element
}
values[0]=last; //set last to
first
}
public void replaceEvenWithZero(){
for(int
i=0;i<values.length;i++){
if(values[i]%2==0) //replace all even to zero
values[i]=0;
}
}
public void replaceLargetsOfTwoNeb(){
for(int
i=1;i<values.length-1;i++){
values[i]=Integer.max(values[i-1], values[i+1]);
//set cur value to max of left and right value
}
}
public void removeMiddle(){
int l=values.length;
int temp[];
int mid=-1;
int mid1=-1;
if(l%2==0){ //if length is
even
temp=new
int[l-2]; //temp value will be 2 less
}
else{
temp=new
int[l-1]; //else 1
}
if(l%2==1){
mid=l/2; //mid
element
}
else{
mid=l/2; //tow
mid element
mid1=mid-1;
}
int k=0;
for(int
i=0;i<values.length;i++){
if(mid==i ||
mid1==i){ //if mid element do not copy to tem array
continue;
}
else{
temp[k++]=values[i]; //else copy
}
}
values=temp; //make temp array to
value
}
//to print
public void print(){
for(int
i=0;i<values.length;i++){
System.out.print(values[i]+" ");
}
System.out.println();
}
//driver program to test
public static void main(String[] args) {
int ar[]={1,2,3,4,5,0};
ArrayMethods a=new
ArrayMethods(ar);
a.print();
a.swapFirstAndLast();
a.print();
a.shiftRight();
a.print();
a.replaceEvenWithZero();
a.print();
a.replaceLargetsOfTwoNeb();
a.print();
a.removeMiddle();
a.print();
}
}
output
1 2 3 4 5 0
0 2 3 4 5 1
1 0 2 3 4 5
1 0 0 3 0 5
1 1 3 3 5 5
1 1 5 5
Write array methods that carry out the following tasks for an array of integers by completing...
Exercise 2: Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called 'Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[] initialValues) {values = initialValues;} //constructor public void shiftRight() { } public Boolean adjacentDuplicate()...
Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...
Create an array of size 10 Assign values to the array Print the array Swap the first and last elements in the array Print the array Shift all elements by one to the right Print the array Replace even elements with 0 Replace each element except the first and last by the larger of its two neighbors Print the array
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
Please Help me! Recursive approximate median Consider an array of integers. You wish to find an approximate median. You have an idea: split the array (or a range of the array) into three pieces, find the approximate median of each piece, and then return the actual median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find...
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...
Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...
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)...
A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no duplicate values. All values in the first row of the square appear in each row of the square. All values in the first row of the square appear in each column of the square. Examples of Latin Squares 10 30 200 0 20 30 10 30 0 10 20 20 10 0 30 Examples that are NOT Latin Squares...
In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element); /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...