Simple Cartesian Coordinate Calculator

I am trying to subtract the coordinates of two vectors, but I am a newbie who cannot understand the OOP code I need. This is what I have so far.

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?

    }
}

      

How can I subtract an int from two vector objects?

+3


source to share


3 answers


I've made some basic example here that will allow you to subtract one vector from another.



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);
    }
}

      

+4


source


I think you just want to call a method coordinate

from vec1

using data from vec2

. if so, this is one way to do it:

System.out.println(vec1.coordinate(vec2.a,vec2.b));

      



in this case you also need to change your coordinates method to:

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

      

+2


source


You can create a method XsubtractY

for which you enter two vectors, then internally it gets each vector a and b and subtracts them from each other to return a new vector. As shown below:

public class practice {

public static class vector{
    int a;
    int b;
    public vector(int a, int b){
        this.a = a;
        this.b = b;
    }
    public static vector XsubtractY(vector x, vector y){
        vector newVector = new vector(x.a-y.a, x.b - y.b);
        return newVector;
    }

}

public static void main(String[] args) {
    vector vec1 = new vector(2,3);
    vector vec2 = new vector(3,4);
    vector newVector = vector.XsubtractY(vec1, vec2);
    System.out.println("New vector a: " + String.valueOf(newVector.a));
    /* New vector a: -1 */
    System.out.println("New vector b: " + String.valueOf(newVector.b));
    /* New vector b: -1 */

}
}

      

The new vector becomes (-1, -1)

0


source







All Articles