简单的笛卡尔坐标计算器

尼尔·琼斯

我正在尝试减去2个向量的坐标,但是我是一个初学者,无法弄清楚我需要的OOP代码。到目前为止,这就是我所拥有的。

public class practice {

    public static class vector{
        int a;
        int b;
        public vector(int a, int b){
            this.a = a;
            this.b = b;
        }
        public String coordinate(int x, int y){
            x = this.a - a;
            y = this.b - b;
            return x + " " + y;
        }
    }

    public static void main(String[] args) {
        vector vec1 = new vector(2,3);
        vector vec2 = new vector(3,4);

        vector.coordinate?

    }
}

如何从2个矢量对象中减去整数?

维沙尔·赞祖鲁基亚(Vishal Zanzrukia)

我在这里做了一些基本的例子,可以从另一个向量中减去一个向量。

package com.test;

public class Vector {

    private int x;
    private int y;

    public Vector(int x, int y) {
        this.x = x;
        this.y = y;
    }

    /**
     * @return the x
     */
    public int getX() {
        return x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }
    // if you don't want to create new vector and subtract from it self, then return type of this method would be void only.
    public Vector subtract(Vector other) {
        return new Vector(this.x - other.x, this.y - other.y);
    }

    @Override
    public String toString() {
        return this.x + " : "+ this.y;
    }

    public static void main(String[] args) {
        Vector vector1 = new Vector(10, 10);
        Vector vector2 = new Vector(5, 5);
        Vector vector3 = vector1.subtract(vector2); 
        System.out.println(vector3);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章