Write a method that takes two integer parameters Return the

Write a method that takes two integer parameters. Return the integer that is closer to 50. (Hint: Use Math.abs. If it\'s a tie, return the larger integer. Make sure your method has a contract. Test your method with a small application

Solution

CloseCheck.java


public class CloseCheck {

  
   public static void main(String[] args) {
       System.out.println(check(51,50));
       System.out.println(check(51,52));

   }
   public static int check(int a, int b)
{
long A = Math.abs(10 - (long)a);
long B = Math.abs(10 - (long)b);

if (A == B)
{
return a > b ? a : b;
}
else{
  
   return A > B ? b : a;
}
   }
}

Output:

50
51

Write a method that takes two integer parameters. Return the integer that is closer to 50. (Hint: Use Math.abs. If it\'s a tie, return the larger integer. Make

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site