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

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

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

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) {...

    Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target) { targetLocation = mid; found = true; } else if (numbers[mid] < target) { first = mid + 1; } else { // numbers[mid] > target last = mid - 1;...

  • Consider the following methods’ headers: public static int one(int a, char b, double c, String d)...

    Consider the following methods’ headers: public static int one(int a, char b, double c, String d) public static double two(double x, double y) public static char three(int r, int s, char t, double u) Answer the following questions: Q1) What is the signature of method one? Answer: Q2) What is the return type of method two? Answer: Q3) What the formal parameters of method three? Answer: Q4) How many actual parameters are needed to call the method three? Answer: Q5)...

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