Question: JAVA exercise: Using the code below, implement the method: public static int[] repeat(int[] array) that returns an array containing the original array elements repeated 3

JAVA exercise:

Using the code below, implement the method: public static int[] repeat(int[] array) that returns an array containing the original array elements repeated 3 times. For example, if you call it with [1, 4, 9], then the method should return a new array containing [1, 4, 9, 1, 4, 9, 1, 4, 9].

Hints: 1. Create a new array of size determined by array.length 2. Use a for loop(s) to copy values from one array to the other in the required order.

Code:

import java.util.Scanner; import java.util.Arrays; public class ArrayExercises { public static void main(String[] args) { final int SIZE = 5; int[] array = new int[SIZE]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < array.length; i++) { System.out.print("Please enter whole number " + (i + 1) + ": "); int input = scanner.nextInt(); // get input from the user array[i] = input; // store the value in the array } printArray("Input array:", array); // call method sum and print out the result int sum = sum(array); System.out.println("The sum of elements is " + sum); // call method repeat and print out the result int[] trebled = repeat(array); // INSERT THE CODE FOR PRINTING HERE } public static void printArray(String msg, int[] array) { System.out.println(msg + " " + Arrays.toString(array)); } public static int sum(int[] array) { int s = 0; for (int i = 0; i < array.length; i++) s += array[i]; return s; } public static int[] repeat(int[] array) { // INSERT YOU CODE HERE } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!