Open Java IDE, open the project. The project contains two classes:
ArraysLab and ArraysDriverLab - solution of Lab10.
- Creating an array with unique elements in ascending order
- In the ArraysLab class implement a method to create an array in ascending order
public static void createArrayAscending(int[] array, int size, int upper, int increment)
The “upper” parameter is used to create the first element in the array.
Each next element in the array is made equal to the previous element plus a random number between 1 and
“increment”.
- To test this method, In the ArraysDriverLab class add an option “test” to do the following:
- Ask the user to enter the size, the upper limit and the increment and read the entered values
- Instantiate an array with the given size
- Call the createArrayAscending method to create the array
- Print the created array.
- Creating an array with unique elements in random order
The method createArrayAscending will create an array with unique elements, however ordered.
To make the elements in random order, we need to shuffle them.
- In the ArraysLab class implement a method to shuffle the elements in an array.
public static void shuffle(int[]array)
To shuffle the elements, we choose randomly two elements (by choosing randomly two indexes)
and swap them. This is repeated in a loop that runs a sufficient number of times, e.g. close to 1000.
Hint: when randomly generating indexes, remember that the scope is from 0 to the size of the array.
- In the test option add a call to the shuffle method and print the elements of the shuffled array.
- Union of two arrays with unique elements
The union arrayC of two arrays arrayA and arrayB contains all elements of arrayA
and arrayB without repetition.
- In the ArraysLab class implement a method that given two arrays, constructs
a third array that contains the union of the two arrays
public static int findUnion(int[] arrayA, int[] arrayB, int[] arrayC)
First, we copy all elements of arrayA into arrayC.
Then we compare each element of arrayB with each element of arrayA, and if it is not there,
we copy it into arrayC.
The method will return the number of elements copied into arrayC.
Algorithm in pseudocode (begin – end is used to indicate loop body):
1. Initialize k to 0
2. For all elements arrayA[i] in arrayA do
begin
arrayC[k] ← arrayA[i]
k ← k+1
end
3. For all elements arrayB[j] in arrayB do
begin
Initialize a boolean variable found to false
For all elements arrayA[i] , while found is false do
begin
if arrayB[j] is equal to arrayA[i]
found ← true
end
if found is false
arrayC[k] ← arrayB[j]
k ← k+1
end
4. return k
Hint: The header of a loop that says:
For all elements arrayA[i] , while found is false do
would be:
for(i = 0; i < sizeA && !found; i++)
where sizeA is the size of arrayA
- In the driver class, rename the option “test” to “union”
Modify the case “union” to create the two arrays A and B with unique elements in random order,
and then call the method findUnion to construct arrayC.
Print how many elements have been copied into arrayC, and print the arrayC.
Hint: Do not forget that the method findUnion returns a value that needs to be stored somewhere
and then used when printing arrayC.
- Intersection of two arrays with unique elements.
The intersection arrayC of two arrays arrayA and arrayB with unique elements contains
only the elements that appear in both arrays.
- In the ArraysLab class implement a method that given two arrays constructs
a third array that contains the intersection of the two arrays.
public static int common(int[] arrayA, int[] arrayB, int[] arrayC)
The algorithm roughly is the following:
We compare each element of arrayA with the elements in arrayB. If we find it,
we copy it into arrayC.
Algorithm in pseudocode (begin – end is used to indicate loop body):
1. Initialize k to 0
2. For all elements arrayA[i] in arrayA do
begin
Initialize a boolean variable found to false
For all elements arrayB[j], while found is false do
begin
if arrayA[i] is equal to arrayB[j]
arrayC[k] ← arrayB[j]
k ← k+1
found ← true
end
end
3. return k
- In the driver class add a new option “common elements” and a new case to handle this task.
The necessary actions are similar to the “findUnion” case.
At the end of the lab zip the program folder and sent it to me even
if the program is not completed. Do not forget to write your name
in the title comment. Follow the rules for submitting assignments.
Your grade will be reduced by 10% for each violation.
If the program is not completed, you have to complete it and
send it by Thursday 03/30, 5 pm.