Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that does the following: In your main function create an int arr

ID: 3533028 • Letter: W

Question

Write a program that does the following:
  1. In your main function create an int array of 5 elements, e.g.: { 3, 4, 10, 8, 21 }
  2. Create a function that can accept the array as parameter. This function reverses the given array elements. After function finishes execution, using above example array, elements should look like { 21, 8, 10, 4, 3 }. Call this function from main,
  3. Create another function that also accepts the array as its parameter. This functions prints all the elements in the array. Call this function from the main after the previous function call.
Note: Arrays are always passed by reference, as such both first and the second function doesn't need to return anything (i.e. it should be of void type). Write a program that does the following: Note: Arrays are always passed by reference, as such both first and the second function doesn't need to return anything (i.e. it should be of void type).

Explanation / Answer

Here you go :


// Demonstrate the Stack class.

import java.util.*;

public class StackDemo {

public static void reverse(Object[] arr)

{

List e=Arrays.asList(arr);

Collections.reverse(e);

arr=e.toArray();

}

public static void showArr(Object[] a)

{

for(Object e:a)

System.out.println(e);

}

public static void main(String args[]) {

Object[] arr=new Object[]{3,4,10,8,21};

reverse(arr);

showArr(arr);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote