-3

I need some assistance with Java. How to write a boolean compare method to return true if int a = int b else false. Would it be something like this:

public int checkIfEqual(int a, int b) {
  if (int a = int b) {
    return 1;
  } else {
    return -1;
  } 
}
mateo
  • 15
  • 1
  • 9
  • If a == b, you should return 0. There are three value in a Java `compare`: -1, 0, +1. – markspace May 20 '18 at 03:16
  • A boolean compare method that return true if the values are the same, and false otherwise? What you're describing is the `equals()` method. For *primitives*, it is done using the `==` equality operator: `public static boolean equals(int a, int b) { return a == b; }` – Andreas May 20 '18 at 03:41
  • Please see: [How do I avoid misusing tags?](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags) – EJoshuaS - Stand with Ukraine May 20 '18 at 04:59
  • 1
    Unfortunately your question boils down to "somebody please please help me with this". But we do not regard such requests as *questions* in the scope of this site. Please read [this](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) carefully to understand why that is. Then consider to either delete this question and putting up a new, more precise question within the scope of this community. Alternatively, you could [edit], rework and improve this question. Thanks! – GhostCat May 20 '18 at 05:31
  • If you want to **compare** to int values, then put `a == b` or `a > b` right there in your code. Otherwise think up a more telling method name than "compare". Like areBothNumbersEqual() or something like that. – GhostCat May 20 '18 at 05:33
  • I modified the question to be more specific. I apologize for the broadness of the question. Please let me know if my edits are sufficient. – mateo May 20 '18 at 15:57

3 Answers3

4

Keep it simple:

public boolean compare(int a, int b) {
    return a == b;
}

I notice you were trying to return an int: Java ain't C, so some of the C idioms like 0 is false, non-zero is true don't hold in Java.

Also, such a method is worse than useless, because:

  • if (a == b) is shorter to write than if (compare(a, b))
  • if (a == b) is clearer than if (compare(a, b))
  • compare() is practically a reserved word, because it's the (only) method of the Comparator interface, a very commonly used class
  • compare is a poor name, both for the previous reason, but also because equals is what it's really doing and is the java convention
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

As @Bohemian says, if you just want to test if two int values are equal:

public boolean equal(int a, int b) {
    return a == b;
}

Don't call this method compare, because the name compare has strong connotations of a testing an ordering ... in Java. Whether it should be equal or equals is debatable.

(And @Bohemian's point about the above method being useless are apropos.)


This is what a proper compare method that implements int compare(int, int) should look like1.

public int compare(int a, int b) {
   if (a == b) {
      return 0;
   } else if (a < b) {
      return -1;
   } else {
      return 1;
   } 
}

This is compatible with the semantics of Comparator::compare(T, T).


I also am not sure how to add the following constraints as well:

0 ≤ a, b ≤ 10000000

Add if statements like this:

 if (a < 0 || b > 10000000) {
     throw SomeException("some message");
 }

where SomeException is the exception you are suppose to throw to indicate a constraint error.


Also, do I need to create a checker class that implements the comparator interface as well?

There is no specific reason that a checker class should implement Comparator. Only do it if your checker needs to be used as a comparator.


Would this also work the same for strings with the following constraints:

1 ≤ |a|, |b| ≤ 2000

This approach would work for constraining the lengths of the string.


1 - It is quicker to show you the code than to explain what was wrong with your version. But compare them ... and you should be able to work that out.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • To compare strings, would I be able to use the following: `public boolean compare(String s1, String s20 { return s1==s2); }` – mateo May 20 '18 at 04:22
  • @mateo - That is an incorrect implementation. The correct implementation is `public boolean compare(String s1, String s2) { return s1.equals(s2); }`. Or to just use `String::equals(Object)`. Read this: https://stackoverflow.com/questions/513832 – Stephen C May 20 '18 at 04:31
-1

I need some assistance with Java and how to write a boolean compare method to return true if int a = int b and false otherwise

a == b

a.Equals(b) (Integer class)

b.Equals(a) (Integer class)

a-b == 0

b-a == 0

I think is enough for equality check.

Now for the others checks... add if's with && (a>x && a<y && b>x && b<y)

Edit: not use int as boolean value. This is not C (where 0 is false and !=0 is true), bool is a bool, int is a int.

DrkDeveloper
  • 939
  • 7
  • 17