Exercise 5.16 JHTP: Write a method isMultiple that determines, for a pair of int
ID: 3752345 • Letter: E
Question
Exercise 5.16 JHTP: Write a method isMultiple that determines, for a pair of integers, whether the second integer is a multiple of the first. The method will take 2 integer arguments and return true if the second is a multiple of the first and false otherwise. [Hint: Use the remainder operator]. Incorporate this method into an application that inputs a series of pairs of integers (1 pair at a time) and determines whether the second value in each pair is a multiple of the first
With explanations for all lines of code please.
Explanation / Answer
Belos is the solution:
CheckMultiple.java
import java.util.Scanner;
public class CheckMultiple {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // scanner object
System.out.println("Enter two Integers with spaces to check if they are multiples : "); // ask the user to enter
// two number
int number1 = in.nextInt(); // input first number
int number2 = in.nextInt(); // input seecond number
if (ismultiple(number1, number2) == true) // create a method isMultiple and pass into that method two number
// whether they are multiple or not
System.out.println(number1 + " is a multiple of " + number2);
else
System.out.println(number1 + " is not multiple of " + number2);
}
public static boolean ismultiple(int x, int y) { // method definition to check if both entered number is multiple or
// not
if (x % y == 0 || x % y == 1) // chcek using mod if
return true;
else
return false;
}
}
output:
Enter two Integers with spaces to check if they are multiples :
7 42
7 is not multiple of 42
Enter two Integers with spaces to check if they are multiples :
42 7
42 is a multiple of 7