Question

The method generate() will produce integer arrays of a random size between 10 and 20 members. ...

  1. The method generate() will produce integer arrays of a random size between 10 and 20 members.

  1. The method print() will print out the members of an integer array input.

  1. 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 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}

Output: {1,2,3,10,11,12,13,4,5,6}

  1. 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:

Input1: {1,2,3,4,5,6,7} Input 2: 25

Output: {1,2,3,4,5,6,7}

Input1: {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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Explanation::
  • Code in JAVA is given below
  • Please comments for better understanding of the code
  • OUTPUT is given at the end of the code

CODE in JAVA::

import java.util.Random;

import java.util.Scanner;

public class RandomArray {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("!WELCOME TO RANDOM ARRAY!");

int a[],b[],c[];

while(true) {

System.out.println("Select any one option");

System.out.println("1. Insert");

System.out.println("2. Shorten");

System.out.println("3. EXIT");

System.out.println("Your Choice : ");

int choice=sc.nextInt();

if(choice==3) {

break;

}else if(choice==1) {

System.out.println("Array A:");

a=generate();

print(a);

System.out.println("\nArray B:");

b=generate();

print(b);

System.out.println("\nAfter insert Array C:");

c=insert(a,b);

print(c);

}else {

a=generate();

System.out.println("Array A:");

print(a);

System.out.println("\nEnter the index :");

int index=sc.nextInt();

a=shorten(a,index);

System.out.println("\nArray A after shorten:");

print(a);

}

System.out.println("\n");

}

}

public static int[] generate() {

/**

* We generate an integer array of random size between 10 - 20 members

* */

Random ran = new Random();

/**

* An integer named size is declared and it stores the random number

* generated

* */

int size=ran.nextInt(11)+10;

/**

* We create an integer array named a of length size

* */

int a[]=new int[size];

for(int i=0;i<size;i++) {

/**

* We generate random numbers in array a[] between 1 and 100

* */

a[i]=ran.nextInt(100)+1;

}

return a;

}

public static void print(int a[]) {

/**

* We just print the array a

* */

for(int i=0;i<a.length;i++) {

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

}

System.out.println();

}

public static int[] insert(int a[],int b[]) {

Random ran = new Random();

int c[]=new int[a.length+b.length];

/*

* An integer named memberIndex is declared and initialized as random number

* between 0 and a.length-1

* */

int memberIndex=ran.nextInt(a.length);

int index=0;

for(int i=0;i<memberIndex;i++) {

c[index]=a[i];

index++;

}

for(int j=0;j<b.length;j++) {

c[index]=b[j];

index++;

}

for(int i=memberIndex;i<a.length;i++) {

c[index]=a[i];

index++;

}

return c;

}

public static int[] shorten(int a[],int index) {

int b[]=new int[a.length-1];

if(index>=0 && index<a.length) {

int j=0;

for(int i=0;i<a.length;i++) {

if(i!=index) {

b[j]=a[i];

j++;

}

}

return b;

}

return a;

}

}

OUTPUT:

!WELCOME TO RANDOM ARRAY!

Select any one option

1. Insert

2. Shorten

3. EXIT

Your Choice :

1

Array A:

53 52 36 77 70 13 12 54 66 16 52 64 40 65 56 98 34 15

Array B:

22 96 72 64 32 87 87 44 46 24 76

After insert Array C:

53 52 36 77 70 13 12 22 96 72 64 32 87 87 44 46 24 76 54 66 16 52 64 40 65 56 98 34 15

Select any one option

1. Insert

2. Shorten

3. EXIT

Your Choice :

2

Array A:

13 11 75 15 75 48 22 92 60 60 3 66 21 6

Enter the index :

0

Array A after shorten:

11 75 15 75 48 22 92 60 60 3 66 21 6

Select any one option

1. Insert

2. Shorten

3. EXIT

Your Choice :

3

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
The method generate() will produce integer arrays of a random size between 10 and 20 members. ...
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
  • Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.r...

    Please write a JAVA program according to following requirement. Thanks Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.random () generates a random integer greater than or equal to 0 and less than 1. The expression Math. random) 6generates random numbers between 0 and 5, simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair; that is, whether each possible...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

  • Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize...

    Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, create a random integer array of size arraySize (15 OR LESS) with elements from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} (integers can appear multiple times in this array, has two accessor methods: public int getArraySize() that will...

  • c++ help Write a program that: Creates two finite arrays of size 10 These arrays will...

    c++ help Write a program that: Creates two finite arrays of size 10 These arrays will serve as a storing mechanism for student grades in Classes A and B Uses a loop to populate the arrays with randomly generated numbers from 0 to 100.These numbers will represent student grades. Compute average grade of each class. Finally, output the two arrays and the message with two averages and best class. ("Class A average is: 65.43; class B average is: 68.90. Class...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm...

    Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm Objective This assignment will consist of writing a program that involves practice with arrays, random number generation, and Strings. You may use c-strings or string objects here, however, string objects is recommended. Exercise Filename: sentences.cpp Write a program that uses random-number generation to create sentences. Create four arrays of strings (string objects highly suggested over c-strings) called article, noun, verb, and preposition. The arrays...

  • using c++ Problem 1 In this problem, we want to generate N random values of type...

    using c++ Problem 1 In this problem, we want to generate N random values of type integer between 7 and 70 inclusive, and then find their average. Print the average and a sentence "the average is low" if the average is less than 52, "the average is high" if it is more than 52, and "the average is in the middle" if it is 52. Asks how many values to generate (i.e.: N). Generates N values between 7 and 70...

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
ADVERTISEMENT