CabDude
CabDude

Reputation: 132

Check If a number is contained in an interval without using "if" or loops

So, I have a basic question. I could solve this easily but I am dumbfounded at the moment because my teacher would like us to solve this not using any if statements or loops. So the interval is not an array. A basic example is [1,6] or (1,6) or a mixed of both open and closed. So, 5 would be in the interval. I need a contains(double number) method that checks if the number is inside. How can you possibly do this without an if or loop? Am I stupid? Is there some magic method that I have not stumbled upon that does this?

My approach would be something along the lines of

public double contains(number)
{
    if (number >= leftEndPoint && number <= rightEndPoint) //lets assume interval is "closed" heh. on both ends
    return true;

    return false;

}

But...We can't use an if statement or loop.

Upvotes: 4

Views: 1521

Answers (5)

Andreas
Andreas

Reputation: 5103

Here is my solution. Pretty verbose, but should be maintainable.

public class Interval {
  final int bottom;
  final int top;
  final IntervalEvaluationStrategy strategy;

  public Interval(final int bottom, final int top, final IntervalEvaluationStrategy strategy) {
    this.bottom = bottom;
    this.top = top;
    this.strategy = strategy;
  }

  public boolean check(int value) {
    return strategy.evaluate(value, bottom, top);
  }

  public enum IntervalEvaluationStrategy {
    OPEN {
      @Override
      boolean evaluate(int value, int bottom, int top) {
        return CLOSED.evaluate(value, bottom, top)
            || value == bottom
            || value == top;
      }
    },
    CLOSED {
      @Override
      boolean evaluate(int value, int bottom, int top) {
        return value > bottom
            && value < top;
      }
    };

    abstract boolean evaluate(int value, int bottom, int top);
  }

  public static void main(String[] args) {
    assert checkInterval(5, 5, 10, IntervalEvaluationStrategy.OPEN);
  }

  //helper method with example usage
  public static boolean checkInterval(final int value, final int bottom, final int top, IntervalEvaluationStrategy strategy) {
    final Interval interval = new Interval(bottom, top, strategy);
    return interval.check(value);
  }
}

Upvotes: 0

Boney Jacob
Boney Jacob

Reputation: 19

Conditional operator can also be used. For example: return (number >= leftEndPoint && number <= rightEndPoint)?true:false;

Upvotes: -2

Niklas S.
Niklas S.

Reputation: 1054

You want to return a boolean value of true or false. Every conditional expression returns such a value, which you can return in your function without having if-statements:

public boolean contains(double number) {
    return number >= startNum && number <= endNum;
}

That works perfectly fine if you - of course - define the startNum and endNum somewhere.

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83537

Note that the expression in the condition of an if statement evaluates to either true or false. This means you can return the value directly rather than having an explicit if. In general,

if (x)
    return true;
else
    return false;

can be replaced by

return x;

Upvotes: 5

djechlin
djechlin

Reputation: 60788

if(x)
    return true;

never makes sense. At least, it never makes more sense than if (true) /* something */; or if (isOn == true) /**/.

Please write:

return number >= leftEndPoint && number <= rightEndPoint;

and thank you.

Upvotes: 1

Related Questions