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...
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 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 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...
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
Write code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT