Apr 4, 2013

OOP Tutorial: 2-dimensional Vector as Java Object

When starting to learn object orientated programming, a good first example is the following: Modelling a 2-dimensional vector over the real numbers could result in this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package de.bigdev;
 
public class Vector {
 
    private double x;
    private double y;
 
    public Vector(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
     
    /**
     * Addition of two vectors
     *
     * @param v
     * @return the sum with v
     */
    public Vector add(Vector v) {
        return new Vector(x + v.x, y + v.y);
    }
     
    /**
     * Scalar Mulitplication with a constant
     *
     * @param c
     * @return the scalar multiplication with c
     */
    public Vector scalarMult(double c) {
        return new Vector(c * x, c * y);
    }
 
    /**
     * Length of two vectors
     *
     * @return length
     */
    public double length() {
        return Math.sqrt(x * x + y * y);
    }
 
    /**
     * Scalar Product of two vectors
     *
     * @param v
     * @return the scalar product with v
     */
    public double scalarProd(Vector v) {
        return this.x * v.getX() + this.y * v.getY();
    }
 
    /**
     * Angle between two vectors
     *
     * @param v
     * @return the angle with v in degrees
     */
    public double angle(Vector v) {
        return Math.acos(this.scalarProd(v) / (this.length() * v.length()))
                * 180.0 / Math.PI;
    }
 
    public double getX() { return x; }
    public double getY() { return y; }
 
    @Override
    public String toString() {
        return "[" + x + ", " + y + "]";
    }
 
    /**
     * for testing only. should be factored out into another class... or use
     * JUnit!
     *
     * @param args
     */
    public static void main(String[] args) {
        Vector v = new Vector(1, 1);
        Vector w = new Vector(-1, -1);
 
        System.out.println("Vector algebra");
        System.out.println("==============");
        System.out.println("addition      : " + v + " + " + w + " = " + v.add(w));
        System.out.println("scalar multip.: 2 * " + v + " = " + v.scalarMult(2));
        System.out.println("length        : ||" +v + "|| = " + v.length());
        System.out.println("scalar product: " + v + " * " + w + " = " + v.scalarProd(w));
        System.out.println("angle         : angle(" + v + ", " + w
          + ") = " + Math.round(v.angle(w)) + "°");
    }
}

The output on the console is


Vector algebra
==============
addition      : [1.0, 1.0] + [-1.0, -1.0] = [0.0, 0.0]
scalar multip.: 2 * [1.0, 1.0] = [2.0, 2.0]
length        : ||[1.0, 1.0]|| = 1.4142135623730951
scalar product: [1.0, 1.0] * [-1.0, -1.0] = -2.0
angle         : angle([1.0, 1.0], [-1.0, -1.0]) = 180°


No comments:

Post a Comment