Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Write Hund's rule?

Answered: 1 week ago