In Java write four methods that generate array outputs or accept arrays as inputs:
1. The method generate() will produce integer arrays of a random size between 10 and 20 members.
2. The method print() will print out the members of an integer array input.
3. The method insert() will accept two arrays as inputs. It will produce a new array long enough to contain both arrays, and both input arrays should be inserted into the new array in the following manner: select a random member of the first array, and insert every member of the second array in before the selected member and the remainder of the first array. Thus, output should look like this (with smaller arrays):
Input 1:{1,2,3,4,5,6} Input 2:{10,11,12,13}
4. The method shorten() should accept an input array and an input integer. It should check if the input integer actually refers to an index within the range of the input array's length. If not, then return the original array. Otherwise, return a new array having length that is one member less than the input. The output array in that case should have the member referenced by the input index removed. E.g:
Input 1: {1,2,3,4,5,6,7} Input 2: 25
Output: {1,2,3,4,5,6,7}
Input 1: {1,2,3,4,5,6,7} Input 2: 5
Output: {1,2,3,4,5,7}
Write a main method that asks the user if they want to run insert or shorten. In each case, use generate() to produce random array inputs for each one, and generate your own random integer inputs. Then run the method associated with the user input.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Random;
import java.util.Scanner;

public class HandlingArrays {
       //Random object will be used for generation of all random values
       private Random rand;
       //Constructor will only instantiate Random object
       public HandlingArrays() {
                rand = new Random();

       //Method which will generate array of 10 - 20 integers
       public int[] generate() {
                //maximum value for array values, can be changed
                int maxValue = 100;
                //size will be random number 0-10
                int size = rand.nextInt(11);
                //add offset so that size of the array is in the range 10-20
                size += 10;
                //now size is known, create the array
                int array[] = new int[size];
                //and set all values of the array to random numbers 0-99
                for (int i = 0; i < size; i++) {
                        array[i] = rand.nextInt(maxValue);
                //return created array
                return array;

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats