Question

In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be
countMultiples The static countMultiples method will accept three parameters: an integer array, an integer representing the n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.Scanner;

public class ArrayFun {

   public static void main(String[] args) {
       //create an object to Scanner class to read input from the keyboard
       Scanner input = new Scanner(System.in);
       //create an array that can hold 100 values
       int[] arr = new int[100];
       //fill the array using fillArray method and get the number of values in the array
       int actualSize = fillArray(arr, input);
       //print the array
       printArray(arr, actualSize);
       //print the product of array elements using computeProduct method
       System.out.println("The product of array elements: " + computProduct(arr, actualSize));
       //ask the user for and integer for factor
       System.out.print("Enter an integer for factor: ");
       int factor = input.nextInt();
       //get the number of multiples of factor
       System.out.println("The number of multiples of " + factor + ": " + countMultiples(arr, actualSize, factor));
       input.close();
   }

   //method that fills the array
   public static int fillArray(int[] arr, Scanner input)
   {
       boolean repeat = true;
       int value, size = 0;
       //run a loop to until -999 is entered
       while(repeat)
       {
           //prompt and read a number
           System.out.print("Enter a value into the array (-999 to stop entering): ");
           value = input.nextInt();
           //if -999 is entered set repeat to false to terminate the loop
           if(value == -999)
               repeat = false;
           //else store the value entered in the array and increment the size
           else
           {
               arr[size] = value;
               size++;
           }
       }
       //return the number of values in the array
       return size;
   }

   //method that computes the product of elements in the array
   public static int computProduct(int[] arr, int size)
   {
       int product = 1;
       //run a loop to iterate through the array
       for(int i = 0; i < size; i++)
       {
           //multiple product by arr[i];
           product = product * arr[i];
       }
       return product; //return the product of the elements in the array
   }

   //method that counts the number of multiples of factor given
   public static int countMultiples(int[] arr, int size, int factor)
   {
       int count = 0;
       //run a loop to iterate through the array
       for(int i = 0; i < size; i++)
       {
           //check if the array element is a multiple of factor
           if(arr[i] % factor == 0)
               count++;
       }
       return count;
   }

   //method that prints the array
   public static void printArray(int[] arr, int size)
   {
       System.out.println("The values in the array are: ");
       //run a loop to iterate through the array
       for(int i = 0; i < size; i++)
       {
           //print the array values one per line
           System.out.println(arr[i]);
       }
   }
}

Output:

Console 3 <terminated> ArrayFun [Java Application] C\Program Files\Java\jre1.8.0 151\bin\javaw.exe (Nov 7, 2019, 8:57:05 AM)

Program Screenshot:

ArrayFun.java 1 import java.util.Scanner; 3 public class ArrayFun public static void main(String [] args) //create an objectArrayFun.java 23 //method that fills the array 24 public static int fillArray (int [] arr, Scanner input) 25e 26 boolean repe61 //method that counts the number of multiples of factor given public static int countMultiples (int [] arr, int size, int f

Add a comment
Know the answer?
Add Answer to:
In this lab, you will create one class named ArrayFun.java which will contain a main method...
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
  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • c# Write compile and test a program named IntegerStatistics. The programs Main() method will: 1. Declare...

    c# Write compile and test a program named IntegerStatistics. The programs Main() method will: 1. Declare an array of 10 integers. 2. Call a method to interactively fill the array with any number of values (up to 10) until a sentinel value is entered. [If an entry is not an integer, continue prompting the user until an integer is entered.] When fewer than 10 integers are placed into the array, your statistics will be off unless you resize the array...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main()...

    Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main() method. Within the same file, create another class named Employee, and do not declare it public. Create a field for each of the following pieces of information: employee name, employee number, hourly pay rate, and overtime rate. Create accessor and mutator methods for each field. The mutator method for the hourly pay rate should require the value to be greater than zero. If it...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Create a class named Program10. Your program should have the following 4 methods (other helper methods...

    Create a class named Program10. Your program should have the following 4 methods (other helper methods are allowed, but these four methods must be implemented as specified). You need to define a global scanner object and only use it inside the main method and the getValues method, since other methods do not get any values from the input. getValues: This method does not have any input parameter and returns an array of integers. This method prompts the user with a...

  • The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc...

    The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc is greater or equal to 3. • If it is not, then print the following message: Incorrect number of arguments - please call with assignment min max where assignment is the name of your executable and min/max define the range of numbers for your array. • If argc is ≥ 3 convert the values for min and max into integers and store them in...

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