Question

Write a static method named middleValue that takes three int arguments, and returns an int. When...

Write a static method named middleValue that takes three int arguments, and returns an int. When given three different argument values, the method should return the value that is numerically between the other two values. When given three values where two or more argument values are the same, then the function should return that value.

Examples:
middleValue(1, 2, 3) will return 2
middleValue(5, 2, 7) will return 5
middleValue(8, 4, 6) will return 6
middleValue(1, 2, 1) will return 1
middleValue(1, 2, 2) will return 2
middleValue(5, 5, 5) will return 5

*code must define a class called “Utility”

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

import static java.lang.Math.*;

public class Utility {

   public static int middleValue(int var1, int var2, int var3){
      
       return max(min(var1,var2), min(max(var1,var2),var3));
  
   }
  
   public static void main(String[] args) {
       int a = 1;
       int b = 1;
       int c = 5;
      
       System.out.println("Values: " +a+ ", "+b+ ", "+c);
      
       System.out.println("Middle value: " + middleValue(a, b, c));
   }
  
  
}

alues: 8, 1, 5 Middle value: 5

alues: 1, 1, 5 Middle value: 1

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
PLEASE PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Write a static method named middleValue that takes three int arguments, and returns an int. When...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a static method named sumOf that takes an array of int values as an argument...

    Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not...

    Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course. Assignment Objectives After completing this assignment the student should be able to:  Declare and instantiate arrays  Access array elements by index  Use loops and decisions to manipulate arrays and array elements  Write methods that manipulate arrays  Write methods that take array arguments  Write methods...

  • In Python beginner code: Write a function named even_div, that takes two arguments (you can safely...

    In Python beginner code: Write a function named even_div, that takes two arguments (you can safely assume that both arguments will always be non-zero integers). When this function is called, it should return how many times the first argument can be divided by the second argument, if it is evenly divisible (no remainder). If it is not evenly divisible, the function should return 0. Examples: even_div(5, 2) will return 0 (5 divided by 2 is 2.5, not evenly divisible) even_div(10,...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • Create a public static method named valueG that takes an ArrayList of type Double and returns...

    Create a public static method named valueG that takes an ArrayList of type Double and returns a double. This method returns the maximum result ( do not return the original value) of taking the sine of each value from the input

  • Please write the following program in Java: Write the code for  invoking a static method named sendTwo,...

    Please write the following program in Java: Write the code for  invoking a static method named sendTwo, provided by the DataTransmitter class. There are two arguments for this method: a double and an int. Invoke the method with the double value of 15.955 and the int value of 133.

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

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