Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Moim zdaniem najlepiej to zrealizować w ten sposób, że raz stworzonego
- // punktu nie można zmienić, tzn. że przypisanie:
- // Punkt p = new Punkt(2,3);
- // zawsze będzie punktem (2,3) niezależnie od tego, jakie metody wywołamy.
- public class Punkt {
- // Raz przypisanych zmiennych x,y nie będzie dało się zmienić.
- // Dopisujemy modyfikator final.
- final private double x;
- final private double y;
- public Punkt(double x, double y) {
- this.x = x;
- this.y = y;
- }
- // Wszystkie metody wcześniej modyfikujące Punkt, powinny teraz zwracać nowy
- // Punkt.
- public Punkt przesuniecie(double a, double b) {
- //! this.x += a;
- //! this.y += b;
- //! return this;
- return new Punkt(x+a, y+b);
- }
- public double odlegloscOdPocz() {
- //! this.x = x; ?
- //! this.y = y; ?
- return Math.sqrt(x*x + y*y);
- }
- public String toString() {
- return "(" + x + ", " + y + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment