jeudi 27 août 2020

How can I simplify/more readable this by using calls to InOrder (code below)

How can I simplify/more readable this by using calls to InOrder (code below). The aim of the code is to check if a Rectangle contains a Point. Below is the code for firstly the Rectangle class, and further below is the InOrder class. I am having trouble finding a way to make the code more readable and I want to simplify it the best way possible.

// Construct a rectangle from two arbitrary points
public class Rectangle {
private int x1;
private int y1;
private int x2;
private int y2;

public Rectangle(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

public boolean contains(int x, int y) {
if(x1 <= x2) {
    if(x1 <= x && x <= x2) {
    if(y1 <= y2) {
        return y1 <= y && y <= y2;
    } else {
        return y2 <= y && y <= y1;
    }
    }
} else {
    if(x2 <= x && x <= x1) {
    if(y1 <= y2) {
        return y1 <= y && y <= y2;
    } else {
        return y2 <= y && y <= y1;
    }       
    }       
}

return false;
}
}



public class InOrder {
//Don't change this
public boolean OutOfOrder(int n1, int n2, int n3) {
return (n1 > n2) || (n2 > n3);
}

//The original and messy InOrder, leave this as an example of what not to do
public boolean inOrder(int n1, int n2, int n3) {
if (n2 > n1) {
  if (n3 > n2) {
    return true;
  } else {
    return false;
  }
} else if (n2 == n1) {
  if (n3 == n1) {
    return true;
  } else {
    return false;
  }
} else {
  return false;
}
}
}

Aucun commentaire:

Enregistrer un commentaire