Question

In: Computer Science

Complete the following method to return the median of three integers. Remember, you need to use...

Complete the following method to return the median of three integers. Remember, you need to use the names of the parameters as given in the method header. This is a return method, so you need to include return statement(s) NOT System.out.println.

JAVA PROGRAM

Exampe:

Method call Value returned
medof 5
medof 5
medof -2
medof 0

Method header:

public static int medof(int n1,int n2, int n3)

Solutions

Expert Solution

code in java

(code to copy)

public class Main {
   public static int medof(int n1, int n2, int n3) {
      // check if n2 is the median
      if ((n1 < n2 && n2 < n3) || (n3 < n2 && n2 < n1))
         return n2;

      // Checking for n1
      else if ((n2 < n1 && n1 < n3) || (n3 < n1 && n1 < n2))
         return n1;

      else
         return n3;
   }

   public static void main(String[] args) {
      // test the medof function
      System.out.println("Method call          Value returned");
      System.out.println("medof (20, 1, 5)       " + medof(20, 1, 5));
      System.out.println("medof (5, 3, 1)        " + medof(5, 3, 1));
      System.out.println("medof (-10, -2, 4)     " + medof(-10, -2, 4));
      System.out.println("medof (-1, 4, 0)       " + medof(-1, 4, 0));
   }
}

code screenshot

Sample output screenshot


Related Solutions

Use data in BUSI1013 Credit Card Balance.xlsx to complete the following. You will need to use...
Use data in BUSI1013 Credit Card Balance.xlsx to complete the following. You will need to use a statistical package such as StatTools or the Regression program within Excel’s Data Analysis Add-in to generate the estimated regression equation and the ANOVA etc. (12 points) What is the estimated regression equation using Account Balance as the dependent variable, and Income, Years of Education, as well as Size of Household as the independent variable? a.Comment on the goodness of fit of the model...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type)...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color...
Intermediate Accounting Aging schedule There are three reports that you need to complete. The second and...
Intermediate Accounting Aging schedule There are three reports that you need to complete. The second and third reports are given in sheets 2 and 3. Execl Project There are three reports that you need to complete. The second and third reports are given in sheets 2 and 3. Your name: Today's Date: 11/1/2017 (1) Prepare an aging schedule: Use the =IF() function to identify past due invoices and # of days past due. Use the =IF(AND()) function for the aging...
Using the bisection method:     Make a program to use this method using the following three...
Using the bisection method:     Make a program to use this method using the following three functions and use it to find the root of this function f (x) = x * x * x-8. a) A function so that the user between xlower and xupper that meets the value of the function has a different sign and if he does not ask for new values. b) A function to find the root and call it bisection and perform a...
Download the following zip file Bag and complete the program. You will need to complete files...
Download the following zip file Bag and complete the program. You will need to complete files MyBag and . MyBag uses the java API class ArrayList as the underline data structure. Using the class variables given, complete the methods of the MyBag class. In your BagHand class you should set appropriate default values for select class variables in the constructor in the add method, use the MyBag object to add a PlayingCard type to the bag and find and set...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
In the following problems , give a complete hypothesis test for each problem. Use the method...
In the following problems , give a complete hypothesis test for each problem. Use the method described. Make sure you include the original claim in symbols, The null and alternative hypothesis, the significance level, the actual formula and calculation of the test statistic, give the p-value and a  drawing of critical region ,the decision concerning the null hypothesis and a conclusion stated in non technical terms In a recent year, of the 109,857 arrests for Federal Offenses, 31968 were for drug...
Use the method of undetermined coefficients to find the complete solutions of the following differential equations....
Use the method of undetermined coefficients to find the complete solutions of the following differential equations. d2y/dx2 − 3 dy/dx + 2y = 2x2 + ex + 2xex + 4e3x .
Java programming. *******I Need complete the following requirements in this project: 1/ Remove the applyRandomBonus method...
Java programming. *******I Need complete the following requirements in this project: 1/ Remove the applyRandomBonus method from the test class 2/ Create a File, Printwriter for an output file yourlastnameErrorLog.txt 3/ Set your maximum array size for accounts to 10 4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when reading data from the file: a. Skip any lines that cause an exception b. Write information about the exception to the log file, yourlastnameError.txt c. Include exception type and line number in exception...
Please use Isoquant-isocost graph to explain. Thank You! remember: isoquant curve would not change, just need...
Please use Isoquant-isocost graph to explain. Thank You! remember: isoquant curve would not change, just need to generate Q1 level of electricity. here are asking about the changes in production cost •Suppose a power plant can use a mixture of coal and renewable resources to generate Q1 level of electricity. And the price of coal is Pc and price of renewable resources is Pr. Notice that the use of coal or renewable resources will subject to law of diminishing marginal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT