1- public class Coordenada {
1+ public class Coordenada {
22 private int x ;
33 private int y ;
44
5- public Coordenada (int x , int y ){
5+ public Coordenada (int x , int y ) {
66 this .x = x ;
77 this .y = y ;
88 }
99
10- public int getLinha () { return x ; }
11- public int getColuna () { return y ; }
10+ public int getLinha () {
11+ return x ;
12+ }
13+
14+ public int getColuna () {
15+ return y ;
16+ }
17+
18+ public void setLinha (int x ) {
19+ if (x < 0 )
20+ throw new IllegalArgumentException ("Valor inválido para coordenada x: " + x );
21+ this .x = x ;
22+ }
1223
13- public void setLinha (int x ) { this .x = x ; }
1424 public void setColuna (int y ) {
25+ if (y < 0 )
26+ throw new IllegalArgumentException ("Valor inválido para coordenada y: " + y );
1527 this .y = y ;
1628 }
1729
1830 @ Override
1931 public boolean equals (Object obj ) {
20- if (this == obj ) return true ;
32+ if (this == obj )
33+ return true ;
2134
22- if (obj == null ) return false ;
23-
24- if (this .getClass () != obj .getClass ()) return false ;
35+ if (obj == null || this .getClass () != obj .getClass ())
36+ return false ;
2537
2638 Coordenada cord = (Coordenada ) obj ;
2739
28- if (this .x != cord .x ) return false ;
29- if ( this . y != cord . y ) return false ;
40+ if (this .x != cord .x || this . y != cord . y )
41+ return false ;
3042
3143 return true ;
3244 }
@@ -35,42 +47,40 @@ public boolean equals(Object obj) {
3547 public int hashCode () {
3648 int ret = 666 ;
3749
38- ret = ret * 7 + Integer .valueOf (this .x ).hashCode ();
39- ret = ret * 7 + Integer .valueOf (this .y ).hashCode ();
50+ ret = ret * 7 + Integer .valueOf (this .x ).hashCode ();
51+ ret = ret * 7 + Integer .valueOf (this .y ).hashCode ();
4052
41- if (ret <0 ) ret =-ret ;
53+ if (ret < 0 )
54+ ret = -ret ;
4255
4356 return ret ;
4457 }
4558
4659 @ Override
4760 public String toString () {
48- String coordX = this . x < 10 ? "0" + this . x : this . x + "" ;
49- String coordY = this . y < 10 ? "0" + this . y : this . y + "" ;
61+ String coordX = String . format ( "%02d" , x ) ;
62+ String coordY = String . format ( "%02d" , y ) ;
5063
5164 return "(" + coordX + "," + coordY + ")" ;
5265 }
5366
5467 // construtor de copia
55- public Coordenada (Coordenada modelo ) throws Exception
56- {
57- if (modelo == null )
68+ public Coordenada (Coordenada model ) throws Exception {
69+ if (model == null )
5870 throw new Exception ("Modelo ausente" );
5971
60- this .x = modelo .x ;
61- this .y = modelo .y ;
72+ this .x = model .x ;
73+ this .y = model .y ;
6274 }
6375
64- public Object clone ()
65- {
76+ @ Override
77+ public Object clone () {
6678 Coordenada ret = null ;
6779
68- try
69- {
80+ try {
7081 ret = new Coordenada (this );
82+ } catch (Exception ignored ) {
7183 }
72- catch (Exception ignored )
73- {}
7484
7585 return ret ;
7686 }
0 commit comments