Ask Question
18 December, 09:24

java Write a method maxMagnitude () with two integer input parameters that returns the largest magnitude value. Use the method in a program that takes two integer inputs, and outputs the largest magnitude value. Ex: If the inputs are: 5 7 the method returns: 7 Ex: If the inputs are:

+3
Answers (1)
  1. 18 December, 10:31
    0
    import java. util. Scanner;

    public class Main

    {

    public static void main (String[] args) {

    Scanner input = new Scanner (System. in);

    System. out. print ("Enter two numbers: ");

    int number1 = input. nextInt ();

    int number2 = input. nextInt ();

    System. out. println ("Max magnitude is: " + maxMagnitude (number1, number2));

    }

    public static int maxMagnitude (int number1, int number2) {

    number1 = Math. abs (number1);

    number2 = Math. abs (number2);

    if (number1 > = number2)

    return number1;

    else

    return number2;

    }

    }

    Explanation:

    In the function:

    - Get the absolute value of the numbers to compare their magnitude

    - Check which one is greater

    In the main:

    - Ask the user for the inputs

    - Call the function, and print the result
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “java Write a method maxMagnitude () with two integer input parameters that returns the largest magnitude value. Use the method in a program ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers