Hermetyzacja klasy Punkt

package pl.am.object1.lesson18;

public class Hermetyzacja {
    public static void main(String[] args) {
        Punkt p1 = new Punkt();
        Punkt p2 = new Punkt();
        p1.setKartezjusz(5, 5);
        p2.setPolarne(5, 0.2);

        System.out.println(p1);
        System.out.println(p2);
    }
}
package pl.am.object1.lesson18;

public class Punkt {
    private double x;
    private double y;

    public void setKartezjusz(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public void setPolarne(double r, double fi) {
        this.x = r * Math.cos(fi);
        this.y = r * Math.sin(fi);
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double getR() {
        return Math.sqrt(this.x*this.x + this.y*this.y);
    }

    public double getFi() {
        return Math.atan(this.y/this.x);
    }

    public String toString() {
        return "x: " + this.x + ", y: " + this.y + ", r: " + this.getR() + ", fi: " + this.getFi();
    }
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *