1 limport java util.Random 4 public class ArrayNum1 static Random r new Random (
ID: 3575733 • Letter: 1
Question
1 limport java util.Random 4 public class ArrayNum1 static Random r new Random (10) static int new int 20 array 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; ik array.length; itt) 11 array[i] r next Int(20)+1; 12 13 prints the array 14 printArray(); 15 //calls duplicate method 16 duplicate method(); 17 system out.println("MnNo DUPS"); 18 printArray(); 19 20 21 22e public static void duplicate method( f int number 23 int count 0; 25 boolean flag true while(flag) 26 flag false 27 remove duplicates 28 for (int i- 0; i k array length i++) 29 for (int j i+1 j array length; j++) 30 loops through each number in the array to see if there are duplicates 31 if (array[i] array[j]){ 32 //if there are any duplicates the duplicate's value will be changed to 33 array [j] 0; 34 flag true 35 36 37 38 system out.println("VnDups removed:"); printArray(); replace duplicates with zero for (int i 0; i array. length; i++) 41 if (array[i 0) array[i] next Int (20)+1; 42 43 system out.println("VnRepopulated: "),printArray0, 45 46 47e public static void printArray() for (int i 0; i array. length -1; it+) 48 System. out.print (arrayli) 50 51Explanation / Answer
Here in the given code, the boolean variable called flag doen't have any impact on any of the control flow in the program, because the flag is only being setting, it is not used by any of the control flow statements or any. Thus if you change the flag=true on line 27, it doesn't impact the control flow of the program.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.