Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package azaza;
- public class Vector2D implements IVector {
- private final double x,y;
- public Vector2D(double x, double y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public int dimension() {
- return 2;
- }
- @Override
- public double getComponent(int i) {
- switch (i) {
- case 0:
- return x;
- case 1:
- return y;
- default:
- throw new IllegalArgumentException();
- }
- }
- @Override
- public double scalar(IVector v) {
- return x*x + y*y;
- }
- @Override
- public double length() {
- return Math.sqrt(x * x + y * y);
- }
- @Override
- public IVector multiply(double number) {
- return new Vector2D(number * x, number * y);
- }
- @Override
- public IVector add(IVector v) {
- return new Vector2D(x + v.getComponent(0), y + v.getComponent(1));
- }
- @Override
- public IVector sub(IVector v) {
- return new Vector2D(x - v.getComponent(0), y - v.getComponent(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement