Please help in Java: Write a program that will remove a random integer (between
ID: 3773842 • Letter: P
Question
Please help in Java:
Write a program that will remove a random integer (between 0-6) from an array containing random integers (between 0-6). This program must use four methods to accomplish this:
a. Create a method to build the array of random integers between 0-6. The array must be size 10.
b. Create a method to generate the random integer between 0-6.
c. Create a method called remove(int v, int[] in) that will return a new array of the integers in the given array but with the value v removed. For example, if v is randomly generated as 3 and in contains 0,1,3,4,6,5,3,1,3,6 the method will return an array containing 0,1,4,6,5,1,6.
d. Create a method to print the original array, the number that is to be removed, and the new array.
i.e. “The original array contained: 0,1,3,4,6,5,3,1,3,6.”
“The number requested to be removed is 3.”
“The new array contains: 0,1,4,6,5,1,6.”
*Keep in mind that because we are dealing with random numbers, it is possible that the random number you generate to be removed from the array, will not exist in the array. In this case, your output should look like:
i.e. “The original array contained: 0,1,3,4,6,5,3,1,3,6.”
“The number requested to be removed is 2.”
“The new array contains: 0,1,3,4,6,5,3,1,3,6.”
Explanation / Answer
import java.util.Random;
public class RemoveRandom {
public static void main(String[] args) {
int v = 3;
int[] in = getRandomArray();
System.out.println("The original array contained: ");
printArray(in);
System.out.println("The number requested to be removed is " + v);
System.out.println("The new array contains: ");
remove(v, in);
}
/**
* method to generate randome array
*
* @return
*/
public static int[] remove(int v, int[] in) {
int flag = 1, loc = 0;
int n = in.length;
for (int i = 0; i < n; i++) {
if (in[i] == v) {
flag = 1;
loc = i;
break;
} else {
flag = 0;
}
}
if (flag == 1) {
for (int i = loc + 1; i < n; i++) {
in[i - 1] = in[i];
}
System.out.print("After Deleting:");
for (int i = 0; i < n - 2; i++) {
System.out.print(in[i] + ",");
}
System.out.print(in[n - 2]);
} else {
System.out.println("Element not found");
}
return in;
}
/**
* method to generate random array
*
* @return
*/
public static void printArray(int[] in) {
for (int i = 0; i < in.length; i++) {
System.out.print(in[i] + ", ");
}
System.out.println();
}
/**
* method to generate random array
*
* @return
*/
public static int[] getRandomArray() {
int[] randArr = new int[6];
for (int i = 0; i < randArr.length; i++) {
randArr[i] = getRandomNum(0, 6);
}
return randArr;
}
/**
* method to generate the random number
*
* @param min
* @param max
* @return
*/
public static int getRandomNum(int min, int max) {
Random random = new Random();
return random.nextInt(max - min + 1) + min;
}
}
OUTPUT:
The original array contained:
2, 2, 1, 4, 0, 3,
The number requested to be removed is 3
The new array contains:
After Deleting:2,2,1,4,0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.