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

Please help in Java: Write a program that will remove a random integer (between

ID: 3774725 • 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 HelloWorld{

//random number generator program   
public static int random_generator(){
Random r = new Random();
int Low = 0;
int High = 7;
int generated_int = r.nextInt(High-Low) + Low;
return generated_int;
}
//build array function
public static int[] buildArray(){
int[] arr=new int[10];
for(int i=0;i<10;i++)
arr[i]=random_generator();
return arr;
}

//remove function
public static int[] remove(int v, int[] in){   
int count=0;
for(int i=0;i<in.length;i++){
if(v==in[i])
count++;
}
int[] new_arr=new int[in.length - count];
int i=0,j=0;
while(i<in.length && j< new_arr.length)
{if(v==in[i])
i++;
else{
new_arr[j++]=in[i];
i++;
}
}

return new_arr;
}
public static void main(String []args){

int v=random_generator(); //v generating randomly
  
System.out.print("The original array contained:");
int[] in=new int[10];
in=buildArray();//calling build array
for(int i=0;i<10;i++)
System.out.print(in[i]+",");
System.out.println();
System.out.println("The number requested to be removed is "+v);
System.out.print("The new array contains:");

  
int[] new_array=remove(v,in); //calling remove

for(int i=0;i<new_array.length;i++)
System.out.print(new_array[i]+",");
  
}
}

/********OUTPUT*****
The original array contained:5,3,0,0,2,0,6,2,4,3,   
The number requested to be removed is 2   
The new array contains:5,3,0,0,0,6,4,3,
********OUTPUT*****/
//note:in case of any doubt please do ask the question,thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote