Skip to Content
Java Coding Problems
book

Java Coding Problems

by Anghel Leonard
September 2019
Intermediate to advanced
816 pages
18h 47m
English
Packt Publishing
Content preview from Java Coding Problems

44. Checking the index in the range from 0 to length

To begin with, let's have a simple scenario to highlight this problem. This scenario may materialize in the following simple class:

public class Function {  private final int x;  public Function(int x) {    this.x = x;  }  public int xMinusY(int y) {    return x - y;  }  public static int oneMinusY(int y) {    return 1 - y;  }}

Notice that the preceding snippet of code doesn't assume any range restrictions over x and y. Now, let's impose the following ranges (this is very common with mathematical functions):

  • x must be between 0 (inclusive) and 11 (exclusive), so x belongs to [0, 11].
  • In the xMinusY() method, y must be between 0 (inclusive) and x (exclusive), so y belongs to [0, x].
  • In the oneMinusY() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Coding Problems - Second Edition

Java Coding Problems - Second Edition

Anghel Leonard
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9781789801415Supplemental Content