Question

In Java, Assume we have these two methods. public static boolean allAreEqual(int n1, int n2, int...

In Java, Assume we have these two methods. public static boolean allAreEqual(int n1, int n2, int n3) { return n1 == n2 && n2 == n3; } public static boolean mystery(int n1, int n2, int n3) { return !allAreEqual(n1, n2, n3) && (n1 == n2 || n1 == n3 || n2 == n3); } Part 1: What value will be returned by the following method calls? mystery(5, 5, 5) mystery(5, 5, 1) Part 2: What does the mystery method do in general? What would be a good name for this method?

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

Part 1: method call mystery(5, 5, 5)

Answer :false

Explanation :

  • method allAreEqual(5,5,5) will return true because 5==5 && 5==5 will return true && true which returns true
  • (n1 == n2 || n1 == n3 || n2 == n3) will return true || true is true ==> true || true ==> will be true
  • hence return !allAreEqual(n1, n2, n3) && (n1 == n2 || n1 == n3 || n2 == n3);
  • !true && true ==> false && true ==> false

Below screen shows the output

л во 6 7 Main.java 1 //Main() method 2 public class Main 3 - 4. public static void main(String[] args) { System.out.println(m

***********************************

Part 1: method call mystery(5, 5, 1)

Answer :false

Explanation :

  • method allAreEqual(5,5,1) will return false because 5==5 && 5==1 will return true && false which returns false
  • (n1 == n2 || n1 == n3 || n2 == n3) will return true || false is true ==> true || false ==> will be true
  • hence return !allAreEqual(n1, n2, n3) && (n1 == n2 || n1 == n3 || n2 == n3); will be
  • !false && true ==> true && true ==> true

Below screen shows the output

000 Main.java 1 //Main() method 2 public class Main 3- { 4 public static void main(String[] args) { 5 System.out.println(myst

***********************************

mystery method do in general :

  • This method will all the method allAreEqual() and will compre the each parameter value and will return the result.

*******************************

good name for this method :

  • These methods compare parameters hence will be called as compareParameters()
Add a comment
Know the answer?
Add Answer to:
In Java, Assume we have these two methods. public static boolean allAreEqual(int n1, int n2, int...
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
  • What is the missing keyword in the program below? public class Batman { public static int...

    What is the missing keyword in the program below? public class Batman { public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; //This is the value that will be returned from the minFunction method. //The value after the keyword return must match the return type from the method header. Ours is OK because they are both int. } // end the public method named minFunction }...

  • Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int...

    Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int col) This method does the following: compare the first character from word to the character in puzzle[row][col] if they match subtract one from row (to check the previous character in the same column) and continue comparing characters, by advancing to the next character in word else if puzzle[row][col] is NOT part of puzzle array or the character in the cell does not match the...

  • Java code about writing two methods: public static String randomStringone(int length) public static String randomStringtwo(int length)...

    Java code about writing two methods: public static String randomStringone(int length) public static String randomStringtwo(int length) This method should return a String of random lowercase letters with the given length by using for loops. To generate a random lowercase letter, use a local Random variable and the method nextInt() to generate a number between 97 and 122, then cast the result to a char. The method nextInt() can be found here: https://www.geeksforgeeks.org/java-util-random-nextint-java/    In randomStringone(), you should use String concatenation...

  • 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...

  • Consider the following method: public static int mystery (int x, double y, char ch) {              ...

    Consider the following method: public static int mystery (int x, double y, char ch) {               int u; if ('A' <= ch && ch <= 'R')                   return (2 * x + (int)(y)); else return((int)(2 * y) - x); } What is the output of the following Java statements? a. System.out.println (mystery(5, 4.3, 'B')); b. System.out.println (mystery(4, 9.7, 'v')); c. System.out.println (2 * mystery(6, 3.9, 'D'));

  • 24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) (...

    24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) ( int result 0: if (a <b) ( else if (a b) else ( return result: result mystery2 (a) mystery2 (a)i result - mystery2 (b) result-ab; public static int mystery2 (int x) f int countx for (int i 0; іск; i++) count +1: return counti What are the values stored in the variable result after the following method calls? a) int result mysteryl(4,1): b) int...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

  • 3. Consider the mystery method given. public static int mystery ( int n) [ if (n...

    3. Consider the mystery method given. public static int mystery ( int n) [ if (n == 0 ) { return 1; How do we get the values for recurse? else if (n%2 == 0 ) { int recurse = mystery ( n - 1); int result = recurse + n; return result; since n =5, we go to the else statement and do int recurse = mystery(5-1) which equals 4? why is 3 written? else { int recurse =...

  • Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[]...

    Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[] two) Given two arrays, it returns a new array with all values that are in both arrays. The returned array has a size fitting its elements exactly and without duplicates. The order of values does not matter. For example, given {1,5,5,1} and {5,3}, the method returns {5}.

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