Question

In: Computer Science

Write array methods that carry out the following tasks for an array of integers by creating...

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( ) {…} }

Method shiftRight: that shifts all elements by one to the right and move the last element into the first position. For example, 1 4 9 16 25 would be transformed into 25 1 4 19 16.

Method adjacentDuplicate: returns true if the array has two adjacent duplicate elements.

Solutions

Expert Solution

Program Code Screenshot :

Sample Output

Program Code to Copy

import java.util.Arrays;

class ArrayMethods {

    private int[] values; //declare instant variables

    public ArrayMethods(int[] initialValues) {
        values = initialValues;
    }

    public void shiftRight() {
        int t = values[values.length-1];
        //Shift each element to right
        for(int i=values.length-1;i>=1;i--){
            values[i] = values[i-1];
        }
        //Shift last to first
        values[0] = t;
    }

    public Boolean adjacentDuplicate() {
        for(int i=1;i<values.length;i++){
            //Check adjacent elements
            if(values[i]==values[i-1]){
                return true;
            }
        }
        return false;
    }

    public String toString(){
        return Arrays.toString(values);
    }
}

class Lab5_yourID {
    public static void main(String[] args) {
        ArrayMethods am = new ArrayMethods(new int[]{1,4,9,16,25});
        System.out.println("Array is "+am);
        System.out.println("After shifting right");
        am.shiftRight();
        System.out.println(am);
        System.out.println("Adjacent duplicaets : "+am.adjacentDuplicate());
    }
}

Related Solutions

Write an array method to carry out each of the following tasks for an array of...
Write an array method to carry out each of the following tasks for an array of integers. Note: you can think of each as a separate problem independent of the others. a) Swap the first and last elements in the array. b) Shi< all elements by one to the right and move the last element into the first posi>on. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16. c) Replace all even elements...
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of...
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of size three as the parameter, that sorts the values of the array into ascending order. Ex: If the array contains [5, 2, 7], after the call Ascend3(int[] vals), the array will now hold [2, 5, 7]. Hints: Return type should be void. One approach puts the three values into an array, then sorts the array. We won't be describing that approach here. Instead, we'll...
Write program to store ten integers in an array and the program will do the following...
Write program to store ten integers in an array and the program will do the following Find the average of even marks Find how many prime integers inserted Note: please solve it by c , not c++
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
write a program which will prompt an array of 12 integers then print the array as...
write a program which will prompt an array of 12 integers then print the array as an array of 4 columns on 3 rows
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
Write out the null and alternative hypotheses for the following hypothetical proposal. Carry out a one-sample...
Write out the null and alternative hypotheses for the following hypothetical proposal. Carry out a one-sample Z test to determine significance at the α=0.05 level. PROPOSAL In the field of cancer epidemiology, many researchers are interested in developing risk measurement assays that are intended to influence the screening process for prevention of late stage and metastatic cancers. Parity, or the number of children that a woman has over her lifetime, is associated with the overall risk of breast cancer in...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT