203k views
3 votes
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:

User Jay Lu
by
4.9k points

2 Answers

5 votes

Final answer:

To find the largest magnitude value of two integers, compare their absolute values using the maxMagnitude() method.

Step-by-step explanation:

To find the largest magnitude value of two integers, you can compare the absolute values of the numbers. A positive number has the same magnitude as its absolute value, while a negative number has a magnitude equal to the absolute value with the negative sign removed. In the maxMagnitude() method, you can use Math.abs() to get the absolute value of each input integer and then compare them using the Math.max() method to get the largest magnitude value.

Here's an example implementation of the method:

public static int maxMagnitude(int num1, int num2) {
int mag1 = Math.abs(num1);
int mag2 = Math.abs(num2);

return Math.max(mag1, mag2);
}

In your program, you can call the maxMagnitude() method with two integer inputs and output the result:

int input1 = 5;
int input2 = 7;
int largestMagnitude = maxMagnitude(input1, input2);
System.out.println(largestMagnitude);

User Fallen
by
5.5k points
7 votes

Answer:

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;

}

}

Step-by-step 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

User Astrieanna
by
5.1k points