Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Random; ? public class ArrayNum1 { static Random r = new Random

ID: 3575748 • Letter: I

Question

import java.util.Random;

?

public class ArrayNum1 {

static Random r = new Random(10);

static int [] array = new int [20];

public static void main(String[] args) {

// creates an array of 20 elements

//assigns random numbers to the 20 elements from 1-20

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

array[i] = r.nextInt(20)+1;

}

//prints the array

printArray();

//calls duplicate_method

duplicate_method();

System.out.println(" No DUPS");

printArray();

}

public static void duplicate_method( ) {

int number;

int count = 0;

boolean flag = true;

while(flag) {

flag = false;

// remove duplicates

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

for (int j = i+1; j < array.length; j++) {

// loops through each number in the array to see if there are duplicates

if (array[i] == array[j]){

//if there are any duplicates, the duplicate's value will be changed to 0

array[j] = 0;

flag = true;

}

}

}

System.out.println(" Dups removed:");printArray();

// replace duplicates with zero

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

if (array[i]==0) { array[i] = r.nextInt(20)+1; }

}

System.out.println(" Repopulated:");printArray();

}

}

public static void printArray() {

for (int i = 0; i < array.length -1; i++) {

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

}

}

}

Program ArrayNum1 What code changes are needed to store 20 unique numbers from to 50?

Explanation / Answer

package snippet;

import java.util.Random;

public class ArrayNum1 {
static Random r = new Random(10);
static int [] array = new int [20];
public static void main(String[] args) {
// creates an array of 20 elements

//assigns random numbers to the 50 elements from 1-50
for (int i = 0; i< array.length; i++) {
array[i] = r.nextInt(50)+1;
}
//prints the array
printArray();
//calls duplicate_method
duplicate_method();
System.out.println(" No DUPS");
printArray();
}

public static void duplicate_method( ) {
int number;
int count = 0;
boolean flag = true;
while(flag) {
flag = false;
// remove duplicates
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
// loops through each number in the array to see if there are duplicates
if (array[i] == array[j]){
//if there are any duplicates, the duplicate's value will be changed to 0
array[j] = 0;
flag = true;
}
}
}
System.out.println(" Dups removed:");printArray();
// replace duplicates with zero
for (int i = 0; i< array.length; i++) {
if (array[i]==0) { array[i] = r.nextInt(20)+1; }
}
System.out.println(" Repopulated:");printArray();
}
}
public static void printArray() {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
====================================================================================

Output:

14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 31 37 4 24 39
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 0 37 4 0 0
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 14 37 4 10 16
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 0 37 4 10 16
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 9 37 4 10 16
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 0 37 4 10 16
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 16
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 0
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 10
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 0
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 15
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 0
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 11
Dups removed:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 11
Repopulated:
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 11
No DUPS
14 31 44 41 47 7 48 39 32 15 24 50 42 9 46 16 37 4 10 11

=================================================================

I have changed highlight part of code in you code.

array[i] = r.nextInt(20)+1; changed to array[i] = r.nextInt(50)+1;

=============================================

public static void printArray() {
for (int i = 0; i < array.length-1; i++) {
System.out.print(array[i] + " ");
}

changed to

public static void printArray() {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}