- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinate.java
More file actions
Latest commit
50 lines (41 loc) · 2.08 KB
/
Copy pathCoordinate.java
File metadata and controls
50 lines (41 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
packagechess;
publicclassCoordinate {
privateintrow; // 0 is the bottom horizontal row
privateintcolumn; // 0 is the left vertical column
publicCoordinate(intcolumn, introw) throwsIndexOutOfBoundsException
{
if ( (column<0)|| (column>7)) thrownewIndexOutOfBoundsException("column must be between 0 and 7,inclusive");
if ( (row<0)|| (row>7)) thrownewIndexOutOfBoundsException("row must be between 0 and 7,inclusive");
this.row = row;
this.column = column;
}
publicCoordinate(charcolumn, charrow) throwsIndexOutOfBoundsException
{
if ( (column<'a')|| (column>'h')) thrownewIndexOutOfBoundsException("column must be between a and h,inclusive");
if ( (row<'1')|| (row>'8')) thrownewIndexOutOfBoundsException("row must be between 1 and 8,inclusive");
this.column = column - 'a';
this.row = row - '1';
}
publicCoordinate(Stringcoordinate) throwsIndexOutOfBoundsException
{
if (coordinate.length() != 2) thrownewIllegalArgumentException ("Coordinate is a 2-character string");
charcolumn = coordinate.charAt(0);
charrow = coordinate.charAt(1);
if ( (column<'a')|| (column>'h')) thrownewIndexOutOfBoundsException("x must be between a and h, inclusive");
if ( (row<'1')|| (row>'8')) thrownewIndexOutOfBoundsException("y must be between 1 and 8, inclusive");
this.column = column - 'a';
this.row = row - '1';
}
publicintgetColumnNumber() { returnthis.column; }
publicintgetRowNumber() { returnthis.row; }
publicchargetColumn() { return (char)(column + 'a'); }
publicchargetRow() { return (char)(row+'0'+1); }
@Override
publicStringtoString() { return"("+getColumnNumber()+","+getRowNumber()+")"; }
publicStringname() { return"" + getColumn()+ getRow() ; }
}