1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
makkiz [27]
4 years ago
13

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)
Computers and Technology
1 answer:
Ira Lisetskai [31]4 years ago
8 0

Answer:

The java program is as follows.

import java.lang.*;

public class Numbers

{

   //variables to hold two numbers

   static int num1=-8, num2=-2;

   //method to return integer with higher magnitude

   public static int maxMagnitude(int userVal1, int userVal2)

   {

       if(Math.abs(userVal1) > Math.abs(userVal2))

           return userVal1;

       else    

           return userVal2;

   }

public static void main(String[] args) {

    int max = maxMagnitude(num1, num2);

 System.out.println("The number with higher magnitude among "+num1+ " and "+num2+ " is "+max);

}

}

OUTPUT

The number with higher magnitude among -8 and -2 is -8

Explanation:

1. Two integer variables are declared to hold the two numbers. The variables are declared at class level (outside main()) and declared static.

2. The variables are initialized with any random value.

3. The method, maxMagnitude(), takes the two integer parameters and returns the number with higher magnitude. Hence, return type of the method is int.

public static int maxMagnitude(int userVal1, int userVal2)

4. This method finds the number with higher magnitude using the Math.abs() method inside if-else statement.

               if(Math.abs(userVal1) > Math.abs(userVal2))

                   return userVal1;

        else    

                   return userVal2;

5. Inside main(), the method, maxMagnitude(), is called. This method takes the two variables as input parameters.

6. The integer returned by the method, maxMagnitude() is assigned to a local integer variable, max, as shown.

      int max = maxMagnitude(num1, num2);

7. The result is displayed to the user as shown.

System.out.println("The number with higher magnitude among "+num1+ " and "+num2+ " is "+max);

8. The class variables and the method, maxMagnitude(), are declared static so that they can be used and called inside main().

9. The variable, max, is called local variable since it can be used only within the main() where it is declared. It is not declared static.

10. The class is declared public since it has the main() method and execution begins with main().

11. The program is saved with the same name as the name of the public class having main(). In this case, the program is saved as Numbers.java.

12. No user input is taken for the two input parameter values. The static variables can be assigned any integer value and the program can be tested accordingly.

You might be interested in
Why are user manuals important? Select all that apply.
Oksi-84 [34.3K]

Answer:

I. User manuals contain text and images to describe how to use the program features.

II. User manuals explain the commands and syntax of programming languages.

III. User manuals often include chapter on problems and their resolutions.

Explanation:

A user manual can be defined as a clear, detailed and an informative guide drafted by the manufacturer (producer) of a product or service, which is to be given to a customer (end user) so as to help them know and understand how to use the product or service properly.

User manuals are important because they contain the following necessary informations;

I. User manuals contain text and images to describe how to use the program features.

II. User manuals explain the commands and syntax of programming languages.

III. User manuals often include chapter on problems and their resolutions.

Additionally, a user manual contains information on the safety precautions that are to be taken by the end user while using a product or service.

4 0
3 years ago
who likes tom holland as spiderman and his web shooter tippets when he swings off buildings in new york city midtown, Forest Hil
S_A_V [24]

Answer:

YESSSS!!!!

Explanation:

5 0
3 years ago
Read 2 more answers
In a function name, what should you use between words instead of whitespaces?
Tom [10]
2. Identify the function of computer hardware components 
3 0
4 years ago
Some commands listed in a menu cannot be selected.<br><br><br> True or False
bogdanovich [222]
True! Have a nice week
4 0
3 years ago
Read 2 more answers
Write a program that prints all the numbers from 0 to 6 except 3 and 6. Expected output: 0 1 2 4 5
stellarik [79]
<h2>Answer:</h2><h2></h2>

for x in range(7):

   if (x == 3 or x==6):

       continue

   print(x, end=' ')

print("\n")

<h2>Output:</h2>

>> 0 1 2 4 5

<h2>Explanation:</h2><h2></h2>

The code above has been written in Python. The following explains each line of the code.

<em>Line 1:</em> for x in range(7):

The built-in function <em>range(7)</em>  generates integers between 0 and 7. 0 is included but not 7. i.e 0 - 6.

The for loop then iterates over the sequence of number being generated by the range() function. At each iteration, the value of x equals the number at that iteration. i.e

For the first iteration, x = 0

For the second iteration, x = 1

For the third iteration, x = 2 and so on up to x = 6 (since the last number, 7, is not included).

<em>Line 2:</em> if (x == 3 or x == 6):

This line checks for the value of x at each iteration. if the value of x is 3 or 6, then the next line, line 3 is executed.

<em>Line 3:</em> continue

The <em>continue</em> keyword is used to skip an iteration in a loop. In other words, when the continue statement is encountered in a loop, the loop skips to the next iteration without executing expressions that follow the <em>continue</em> statement in that iteration. In this case, the <em>print(x, end=' ')  </em>in line 4 will not be executed when x is 3 or 6. That means 3 and 6 will not be printed.

<em>Line 4:</em> print(x, end=' ')

This line prints the value of x at each iteration(loop) and then followed by a single space. i.e 0 1 2 ... will be printed. Bear in mind that 3 and 6 will not be printed though.

<em>Line 5:</em> print("\n")

This line will be printed after the loop has finished execution. This line prints a new line character.

8 0
4 years ago
Other questions:
  • There are two cons to an OLTP database. Which of the following are a solution of these cons?​
    9·1 answer
  • Write the java statement to creat an object mp4 class digital​
    8·1 answer
  • When two or more links need to pass traffic as if they were one physical link, which of the following would be used to satisfy t
    12·1 answer
  • Josh wants to convey his best wishes to johnathin for a meeting schedualed later diring the day. Which business documented would
    7·1 answer
  • 1. The precious metals needed to make computer chips, graphic cards, and transistors are found in only a small population of cou
    8·1 answer
  • A student builds a model of an ATP molecule out of some scraps she finds at home. She uses a block of wood for the bulk of the m
    5·1 answer
  • Where Go to set up margins on a letter
    15·1 answer
  • Question 1
    10·1 answer
  • Identify and state the value of hardware components​
    12·1 answer
  • What type of redundant storage configuration is most common for hosting the operating system and applications on a server
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!