I have defined the Coordinates class before as it takes two integers, now i want to use it somewhere in my other class like
public boolean isLocationValid( Coordinates location ){
...and here I want to define that location should be in the boundaries (for example 0<x<10 and 0<y<10)
}
but I don't know and the location object also do not accept x and y!! what can i do?
here you find the Code for Coordinates:
import java.util.IdentityHashMap;
public class Coordinates {
private int x ;
private int y ;
public Coordinates(){ //default constructor
this.x = 0;
this.y = 0;
}
public Coordinates(int x, int y){ //parameterized constructor
this.x = x;
this.y = y;
}
//getter and setter
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//the outcome of the rows should be in Letters, so I defined the Hashmap for the alphabet
static IdentityHashMap<Integer, String> Row = new IdentityHashMap<Integer, String>();
/*Adding elements to HashMap*/
static {
Row.put(0, "A");
Row.put(1, "B");
Row.put(2, "C");
Row.put(3, "D");
Row.put(4,"E");
Row.put(5, "F");
Row.put(6, "G");
Row.put(7, "H");
Row.put(8, "I");
Row.put(9, "J");
Row.put(10, "K");
Row.put(11, "L");
Row.put(12, "M");
Row.put(13, "N");
Row.put(14, "O");
Row.put(15, "P");
Row.put(16, "Q");
Row.put(17, "R");
Row.put(18, "S");
Row.put(19, "T");
Row.put(20, "U");
Row.put(21, "V");
Row.put(22, "W");
Row.put(23, "X");
Row.put(24, "Y");
Row.put(25, "Z");
}
public String toString() {
return Row.get(getX())+getY();
}
}
how is it possible to define the location variable out of Coordinates and define it in the boundary?
There are getters on the Coordinates
class, so within the isLocationValid()
method, call location.getX()
and location.getY()
. To expand your example:
public boolean isLocationValid( Coordinates location ){
// ...and here I want to define that location should be in the boundaries (for example 0<x<10 and 0<y<10)
return isBetweenMinAndMax(location.getX() && isBetweenMinAndMax(location.getY()))
}
private final int MIN_EXCLUSIVE = 0;
private final int MAX_EXCLUSIVE = 10;
private isBetweenMinAndMax(int value) {
return value > MIN_EXCLUSIVE && value < MAX_EXCLUSIVE;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.