184,271 views
30 votes
30 votes
LAB: Count multiples (EO)

Complete a program that creates an object of the Count class, takes three integers as input: low, high, and x, and then calls the countMultiples() method. The countMultiples() method then returns the number of multiples of x between low and high inclusively.
Ex: If the input is:
1 10 2
countMutiples() returns and the program output is:
5
Hint: Use the % operator to determine if a number is a multiple of x. Use a for loop to test each number between low and high.
Note: Your program must define the method:
public int countMultiples(int low, int high, int x)
Count.java
1 import java.util.Scanner;
2
3 public class Count {
4
5 public int countMultiples(int low, int high, int x) {
6 /* Type your code here. */
7
8 }
9
10 public static void main(String[] args) {
11 Scanner scnr = new Scanner(System.in);
12 /* Type your code here. */
13 }
14)
15

User RashadRivera
by
2.8k points

2 Answers

24 votes
24 votes

Final answer:

The student must implement the countMultiples method in Java, which counts the number of multiples of x between a given range low and high.

Step-by-step explanation:

The question is asking to complete a program in Java which can count the number of multiples of a given integer x within the range of two other integers, low and high. To accomplish this, one must write the logic for the countMultiples method that iterates through the range and uses the modulus operator to check for multiples of x.

The completed countMultiples function would look like this:

public int countMultiples(int low, int high, int x) {
int count = 0;
for (int i = low; i <= high; i++) {
if (i % x == 0) { // Check if i is a multiple of x
count++;
}
}
return count;
}

User Andrew Stein
by
3.1k points
17 votes
17 votes

Final answer:

To solve the student's question, implement the countMultiples method using a for loop to count the multiples of x between low and high, and then call this method from the main function after getting user input.

Step-by-step explanation:

To complete the program, you must fill in the countMultiples method within the Count class. This method will calculate the number of multiples of x between two integers, low and high. The method will use a for loop to iterate through the numbers, checking each one to see if it's a multiple of x using the modulo operator (%).

Here's how you can define the countMultiples method:

public int countMultiples(int low, int high, int x) {
int count = 0;
for (int i = low; i <= high; i++) {
if (i % x == 0) {
count++;
}
}
return count;
}

In the main method, you'll need to read three integers from the user input and invoke the countMultiples method, finally printing out the result:

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int low = scnr.nextInt();
int high = scnr.nextInt();
int x = scnr.nextInt();
Count myCount = new Count();
int result = myCount.countMultiples(low, high, x);
System.out.println(result);
}

User Lvd
by
3.1k points