Question

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

  1. Write a class called RandomIntegerArrayCreator that:
  1. upon its object instantiation:
    1. 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},
    2. 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,
  2. has two accessor methods:
    1. public int getArraySize() that will return the array size,
    2. public int[] getArray() that will return the reference to the random integer array that was generated.
  1. Write a class called CommonElements with a single method main that will:
  1. Create and obtain two integer arrays (arrayA and arrayB) using RandomIntegerArrayCreator type objects and its methods,
  2. find the number of common elements between arrayA and arrayB (say: if integer 2 appears in arrayA once and twice in arrayB, that counts as ONE common element between the two),
  3. display the result using the format shown below (see Sample Output box).
  4. Constraints / notes:

  5. All array elements are integers from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and can appear multiple times in each array,
  6. Arrays A and B do NOT have to be of the same size,
  7. Arrays A and B CAN be empty (no elements),
  8. Arrays A and B will NOT be sorted.
  9. Given these two sample arrays (orientation changed for readability):

    Array A

    Array B

    2

    3

    1

    0

    1

    5

    4

    3

    4

    0

    5

    2

    1

    2

    2

    5

    Your program output should look like this:

    Sample output

    Array A: 2     3    1    0    1    5

    Array B: 4     3    4    0    5    2    1    2    2    5

    Element:       # in A:        # in B:

    0                    1                    1

    1                    2                    1

    2                    1                    3

    3                    1                    1

    5                    1                    2

    Number of common elements in A and B: 5

    If there are no common elements (or one or two arrays are empty), just display:

    Number of common elements in A and B: 0

    Test your class with different arrays. Your solution does not need to be efficient, just effective.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
class RandomIntegerArrayCreator{
int[] arr;
RandomIntegerArrayCreator(){
Random rand = new Random();

// It will generate a number between 0-15
int size = rand.nextInt(16);
arr = new int[size];
for(int i=0;i<size;i++){

// It will generate a number between 0-10
arr[i] = rand.nextInt(11);
}
}
public int getArraySize(){
return this.arr.length;
}
public int[] getArray(){
return this.arr;
}
public static void main(String[] args) {
RandomIntegerArrayCreator r = new RandomIntegerArrayCreator();
System.out.println("Size = "+r.getArraySize());
int[] arr = r.getArray();
System.out.print("Generated array is ");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}

Add a comment
Know the answer?
Add Answer to:
Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize...
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
  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

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

    The method generate() will produce integer arrays of a random size between 10 and 20 members. The method print() will print out the members of an integer array input. 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...

  • eciare a class to represent sample data set of 150 elements called DataType. DataType has two member vari member functions and include the functions to generate a sample set of random numbers be...

    eciare a class to represent sample data set of 150 elements called DataType. DataType has two member vari member functions and include the functions to generate a sample set of random numbers between 81 and 255, maximum value, and the range ables- an integer array and its size. Declare the basic calculate the mean, median, the minimum value, the eciare a class to represent sample data set of 150 elements called DataType. DataType has two member vari member functions and...

  • Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an arra...

    C++ please Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an array of integers. The class contains two data members: array (of type int"), which represents the array of integers, and size (of type int) that represents the size of the array. The class SpecialArray also overloads the following operators: operator: Compares two arrays. An array "A" is smallerhan an array "B" if the size of array "A" is smaller than the size of "B", or if...

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • This question has been answered before, but no codes given as the answer were right. please...

    This question has been answered before, but no codes given as the answer were right. please help. In the mathematical theory of sets, a set is defined as a collection of distinct items of the same type. In some programming languages, sets are built-in data types; unfortunately, this is not the case in C++. However, we can simulate a set using a one-dimensional array. Some operations can be performed on sets. We will consider three(3) of them: union, intersection and...

  • Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding...

    Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding array elements, that array1 [0] + array2[0], etc. Save the sum into a third array called sumArray[]. Display the sum array. Submit your and the input and output of the complete execution.

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

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