Ask Question
21 December, 18:19

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:

+4
Answers (1)
  1. 21 December, 18:26
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers