Question

Create a java class that user put two inputs and first input generate n length array...

Create a java class that user put two inputs

and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example,

if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0

here is the main method that I'm going to use

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
       Random ran = new Random();
       int fiv = sc.nextInt();
       int n = sc.nextInt();
       ran.setSeed(n);
      
       int[] arr = new int[n];
       for (int i = 0; i < n; i++)
           arr[i] = ran.nextInt(100);
      
       for (int i = 0; i < n; i++)
       System.out.print(arr[i] + " ");
       System.out.println();
      
       Eliminator.eliminate(arr, fiv);
       for (int i = 0; i < n; i++)
           System.out.print(arr[i] + " ");
      
sc.close();
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Eliminator.java
public class Eliminator {
  public static void eliminate(int[] arr, int fiv) {
    for(int i = 0;i<arr.length;i++){
      if(arr[i]%fiv == 0){
        arr[i] = 0;
      }
    }
  }
}

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

//Main.java
import java.util.Random;
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Random ran = new Random();
    int n = sc.nextInt();
    int fiv = sc.nextInt();
    ran.setSeed(n);

    int[] arr = new int[n];
    for (int i = 0; i < n; i++)
      arr[i] = ran.nextInt(100);

    for (int i = 0; i < n; i++)
      System.out.print(arr[i] + " ");
    System.out.println();

    Eliminator.eliminate(arr, fiv);
    for (int i = 0; i < n; i++)
      System.out.print(arr[i] + " ");

    sc.close();
  }
}

Add a comment
Know the answer?
Add Answer to:
Create a java class that user put two inputs and first input generate n length array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • design two java classes The example of output would be this: user puts the amount of...

    design two java classes The example of output would be this: user puts the amount of money: (for example 9000) I'm going to get three things 1. Cellphone cost: 3000 2. Laptop cost: 6000 3. Macbook cost: 4000 From now on, my balance would be (+balance) and I bought cellphone, laptop, (since there is not enough amount of money, I can only buy cellphone and laptop. if there is enough money then it can buy all things) so I have...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • The code snippet below is an example of pass by reference. We also provide a sample...

    The code snippet below is an example of pass by reference. We also provide a sample method setZeroAt, which sets 0 at a position i in the array, to illustrate pass by reference. public class Sample { public static void setZeroAt(int [] arr, int i){ arr[i] = 0; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int sample[] = {1, 2, 3}; setZeroAt(sample, 1); //Now sample looks like {1, 0, 3} } } Write a Java...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT