Question: QUE1. Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. Also provide a test
QUE1. Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. Also provide a test class that invokes each of the array methods. public class ArrayMethods { private int [] values; public ArrayMethods(int [] initValues) {values = initValues; } public void swapFirstAndLast() { } public void shiftRight() { } . } Swap the first and last elements in the array Shift all elements to the right by one and move the last element into the first position. For example, 1, 4, 9, 16, 25 would be transferred into 25, 1, 4, 6, 16 Replace all even numbers with 0. Remove the middle element if the array length is odd, or the middle two elements if the length is even Move all even elements to the front, otherwise preserving the order of the elements Return the second largest element in the array Return true if the array is currently sorted in ascending order Return true if the array contains two adjacent duplicate elements Return true if the array contains duplicate elements (which need not be adjacent)
QUE2. Consider the following class: public class Sequence { private int [] values; public Sequence(int size) { values = new int[size]; } public void set(int i, int n) { values [i] = n; } public int get(int i) { return values [i]; } public int size() { return values.length; } } Add a method: public boolean equals(Sequence other) That checks whether two sequences have the same values in the same order
QUE3. The game of Nim. This is a well-known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most have of the marbles. The the other player takes a turn. The player who takes the last marble loses. Write a java application in which the computer plays against a human opponent. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide whether the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid mode, the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart mode, the computer takes off enough marbles to make the size of the pile a power of two minus 1 - that is, 3, 7, 15, 31, or 63. That is always a
legal move, except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move. You will note that the computer cannot be beaten in smart mode when it has the first move, unless the pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can win against the computer.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
