Question

Write a method MaxMagnitude with two integer input parameters that returns the largest magnitude value.


6.12 LAB: Max magnitude


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 -8 -2, the method returns:

-8


Note: The method does not just return the largest value, which for -8-2 would be -2. Though not necessary, you may use the absolute-value built-in math method.


Your program must define and call a method:

public static int MaxMagnitude(int userVal1, int userVal2)


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1


import java.util.Scanner;

public class MaxMagnitude {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int n1=sc.nextInt();
       int n2=sc.nextInt();
       System.out.println(maxMagnitude(n1,n2));
   }

   private static int maxMagnitude(int n1, int n2) {
       //finding absolute values for 2 input params
       // so if any negative number comes it will make as positive
       n1=Math.abs(n1);
       n2=Math.abs(n2);
       return (n1>n2)?n1:n2;
   }
}

3 import java.util.Scanner; public class MaxMagnitude { public static void main(String[] args) { Scanner sc = new Scanner(Sys

Add a comment
Know the answer?
Add Answer to:
Write a method MaxMagnitude with two integer input parameters that returns the largest magnitude value.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • *Using Coral Language* Write a function MaxMagnitude with two integer parameters that returns the largest magnitude...

    *Using Coral Language* Write a function MaxMagnitude with two integer parameters that returns the largest magnitude value. Ex: If the inputs are 5 7, the function returns 7. Ex: If the inputs are -8 -2, the function returns -8. Note: The function does not just return the largest value, which for -8 -2 would be -2. Though not necessary, you may use the absolute value built-in math function: AbsoluteValue Use the function in a program that gets two integer inputs,...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallo

    6.26 LAB: Driving cost - methodsWrite a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975.Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method three times.Output...

  • EX 1) Determine the outcome of the following code fragment without writing and running the program....

    EX 1) Determine the outcome of the following code fragment without writing and running the program. int num = 0, max = 20; while (num <= max) {     System.out.println(num);     num += 4; } EX 2) Write a code fragment that reads and prints integer values entered by user until a particular sentinel value (stored in constant SENTINEL) is entered. Do not print the sentinel value.                 Ex 3)  Write a method called averageLargestTwo  that accepts three int parameters  and returns the average...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Please help me with this question Write a recursive method that returns the largest integer in...

    Please help me with this question Write a recursive method that returns the largest integer in an array. Write a test program that prompts the user to enter a list of eight integers and displays the largest largest element. Thank you! and can you give details so I know what is going on?

  • LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent...

    LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent (2 pts). For example, integerPower(3, 4) returns 81. Assume thatexponent is a positive nonzero integer, and base is an integer. Method integer should use for or while loop to control the calculation. Do not use any Math library methods. Incorporate this method into a program class and invoke this method with different combinations of input values at least 4 times. Please use printf() method...

  • Step 2: Create the method specified below. static int GetValidWidth(string prompt) Input parameters prompt: message for...

    Step 2: Create the method specified below. static int GetValidWidth(string prompt) Input parameters prompt: message for user describing the input required Returns A non-zero width entered by the user Step 3: Create the method specified below. static int GetValidHeight(string prompt) Input parameters prompt: message for user describing the input required Returns A non-zero positive height entered by the user Step 4: Create the method specified below. static void DisplayBox(int width, int height, char fillChar) Input parameters width: width of each...

  • Given an integer x which is assumed to be in the list a, write a method...

    Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of x in a. Positions are counted as 0,1,2,.... If x does not appear in the list, you should throw an IllegalArgumentException. static int find(int x, List<Integer> a) Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of u in a....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT