Guest User

Untitled

a guest
Dec 23rd, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. // Moim zdaniem najlepiej to zrealizować w ten sposób, że raz stworzonego
  2. // punktu nie można zmienić, tzn. że przypisanie:
  3. //           Punkt p = new Punkt(2,3);
  4. // zawsze będzie punktem (2,3) niezależnie od tego, jakie metody wywołamy.
  5.  
  6. public class Punkt {
  7. // Raz przypisanych zmiennych x,y nie będzie dało się zmienić.
  8. // Dopisujemy modyfikator final.
  9.     final private double x;
  10.     final private double y;
  11.  
  12.     public Punkt(double x, double y) {
  13.         this.x = x;
  14.         this.y = y;
  15.     }
  16.  
  17. // Wszystkie metody wcześniej modyfikujące Punkt, powinny teraz zwracać nowy
  18. // Punkt.
  19.     public Punkt przesuniecie(double a, double b) {
  20.         //! this.x += a;
  21.         //! this.y += b;
  22.         //! return this;
  23.         return new Punkt(x+a, y+b);
  24.     }
  25.     public double odlegloscOdPocz() {
  26.         //! this.x = x; ?
  27.         //! this.y = y; ?
  28.         return Math.sqrt(x*x + y*y);
  29.     }
  30.     public String toString() {
  31.         return "(" + x + ", " + y + ")";
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment