Question

1) Write a public static method named printArray, that takes two arguments. The first argument is...

1) Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second argument is a String. The method should print out a list of the values in the array, each separated by the value of the second argument.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9}; printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9

printArray(myArray, " - ") will print out 1 - 22 - 333 - 400 - 5005 - 9
2) Write a public static method named getFirst, that takes an Array of int as an argument and returns the value of

the first element of the array.
For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9}; getFirst(myArray) will return 1

3) Write a public static method named getLast, that takes an Array of int as an argument and returns the value of the last element of the array.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9}; getLast(myArray) will return 9

4) Write a public static method named getAllButFirst, that takes an Array of int as an argument and creates and returns a new array with all of the values in the argument array except the first value.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9};
getAllButFirst(myArray) will return an Array of int with these values {22, 333, 400, 5005, 9}

5) Write a public static method named getIndexOfMin, that takes an Array of int as an argument and returns the index of the least value in the array.

For example, given the following Array declaration and instantiation:

int[] myArray = {333, 22, 1, 400, 5005, 9}; getIndexOfMin(myArray) will return 2

6) Write a public static method named getIndexOfMax, that takes an Array of int as an argument and returns the index of the largest value in the array.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9};

getIndexOfMax(myArray) will return 4

7) Write a public static method named swapByIndex, that takes three arguments. The first argument is an Array of int, and the second and third arguments are int indexes. This method will swap the values at the two given index arguments in the array, and return a reference to the array.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9};
swapByIndex(myArray, 1, 4) will return the Array with these values {1, 5005, 333, 400, 22, 9}

8) Write a public static method named removeAtIndex, that takes two arguments. The first argument is an Array of int, and the second argument is an int index. This method create and return a new array with all of the values in the argument array except the value at the argument index.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 5005, 9};
removeAtIndex(myArray, 3) will return an Array with these values {1, 22, 333, 5005, 9}

9) Write a public static method named insertAtIndex, that takes three arguments. The first argument is an Array of int, the second argument is an int index, and the third argument is an int value. This method create and return a new array with all of the values in the argument array and including the third argument value inserted at the index specified by the second argument value.

For example, given the following Array declaration and instantiation:

int[] myArray = {1, 22, 333, 400, 9};
insertAtIndex(myArray, 2, 777) will return an Array with these values {1, 22, 777, 333, 400, 9}

10) Write a public static method named isSorted, that takes an Array of int as an argument. This method should return the boolean value true if all the element values in the array are in ascending order; otherwise the method should return the boolean value false.

For example, given the following Array declaration and instantiation:

int[] myArray = {22, 5005, 400, 333, 1, 9}; isSorted(myArray) will return false

Method Template

Here is a template that you may use or refer to when defining your methods. All of your ten methods will follow this template; you must provide the components designated by the angle brackets (< >)

public static <returnType> <methodName>(<parameters>) { <methodBody>

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I've also added a main method to demonstrate all the methods.

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

# # Please consider providing a thumbs up to this question if it helps you. by doing that, you will help other students who are facing a similar issue.

//--------------OUTPUT--------------------------

//--------------------------------------------------

import java.util.Arrays;

public class Main{

    public static void main(String[] args){

        

        int[] array={43,42,23,42,4,56,36,7,8,676,54};

        System.out.println(Arrays.toString(array));

        printArray(array,"*");

        System.out.println(""+getFirst(array));

        System.out.println(""+getLast(array));

        System.out.println(Arrays.toString(getAllButFirst(array)));

        System.out.println(""+getIndexOfMin(array));

        System.out.println(""+getIndexOfMax(array));

        System.out.println(Arrays.toString(swapByIndex(array, 1,2)));

        System.out.println(Arrays.toString(removeAtIndex(array,3)));

        System.out.println(Arrays.toString(insertAtIndex(array,3,65)));


    }

    //---1----

    public static void printArray(int[] arr, String sep){

        for(int i=0; i<arr.length-1;i++){

            System.out.print(" "+arr[i]+" "+sep);

        }

        System.out.println(" "+arr[arr.length-1]);

    }

    //---2----

    public static int getFirst(int[] arr){

        return arr[0];

    }

    //---3----

    public static int getLast(int[] arr){

        return arr[arr.length-1];

    }

    //---4-----

    public static int[] getAllButFirst(int[] arr){

        int[] anotherArray=new int[arr.length-1];

        for(int i=1; i<arr.length;i++){

            anotherArray[i-1]=arr[i];

        }

        return anotherArray;

    }

    //---5------

    public static int getIndexOfMin(int[] arr){

        int index=0;

        int min=arr[0];

        for(int i=1; i<arr.length;i++){

            if(min>arr[i]){

                min=arr[i];

                index=i;

            }

        }

        return index;

    }

    //---6------

    public static int getIndexOfMax(int[] arr){

        int index=0;

        int max=arr[0];

        for(int i=1; i<arr.length;i++){

            if(max<arr[i]){

                max=arr[i];

                index=i;

            }

        }

        return index;

    }

    //---7------

    public static int[] swapByIndex(int[] arr, int a , int b){

        int temp=arr[a];

        arr[a]=arr[b];

        arr[b]=temp;

        return arr;

    }

    //---8------

    public static int[] removeAtIndex(int[] arr, int index){

        int[] anotherArray = new int[arr.length - 1];

for (int i = 0, k = 0; i < arr.length; i++) {

if (i == index) {

continue;

}

anotherArray[k++] = arr[i];

}

// return the resultant array

return anotherArray;

    }

    //---9------

    public static int[] insertAtIndex(int[] arr, int index, int element){

        int[] anotherArray = new int[arr.length + 1];

        

for (int i = 0, k=0;k < arr.length+1; k++) {

if (k == index) {

anotherArray[k]=element;

                continue;

}

anotherArray[k] = arr[i++];

}

// return the resultant array

return anotherArray;

    }

    //---10------

    public static boolean isSorted(int[] arr){

        for(int i=0; i<arr.length-1;i++){

            if (arr[i+1]<arr[i]){

                return false;

            }

        }

        return true;

    }

}

Add a comment
Know the answer?
Add Answer to:
1) Write a public static method named printArray, that takes two arguments. The first argument is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not...

    Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course. Assignment Objectives After completing this assignment the student should be able to:  Declare and instantiate arrays  Access array elements by index  Use loops and decisions to manipulate arrays and array elements  Write methods that manipulate arrays  Write methods that take array arguments  Write methods...

  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • Write a static method named sumOf that takes an array of int values as an argument...

    Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Write a static method named middleValue that takes three int arguments, and returns an int. When...

    Write a static method named middleValue that takes three int arguments, and returns an int. When given three different argument values, the method should return the value that is numerically between the other two values. When given three values where two or more argument values are the same, then the function should return that value. Examples: middleValue(1, 2, 3) will return 2 middleValue(5, 2, 7) will return 5 middleValue(8, 4, 6) will return 6 middleValue(1, 2, 1) will return 1...

  • JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in...

    JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT