Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts
Jul 11, 2013
May 1, 2013
MOOC: Introduction to Programming (Java)
starting Jun 3rd 2013.
Labels:
Java SE,
MOOC,
Online Learning,
OOP,
Udacity
Apr 4, 2013
OOP Tutorial: 2-dimensional Vector as Scala Object
As before in Java: 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 (pay attention to the overloading of * and +. Compare to the verbose Java variant):
For execution we need a main method (pay attention to the usage of *, + and angle):
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°
package de.bigdev
class Vector(val x: Double, val y: Double) {
/**
* Addition of two vectors
*
* @param v
* @return the sum with v
*/
def +(v: Vector) = new Vector(x + v.x, y + v.y)
/**
* Scalar Mulitplication with a constant
*
* @param c
* @return the scalar multiplication with c
*/
def *(c: Double) = new Vector(c * x, c * y)
/**
* Length of two vectors
*
* @return length
*/
def length = math.sqrt(x * x + y * y)
/**
* Scalar Product of two vectors
*
* @param v
* @return the scalar product with v
*/
def *(v: Vector) = x * v.x + y * v.y
/**
* Angle between two vectors
*
* @param v
* @return the angle with v in degrees
*/
def angle(v: Vector) = math.acos(this * v / (this.length * v.length)) *
180.0 / Math.PI
override def toString = "[" + x + "," + y + "]"
}
For execution we need a main method (pay attention to the usage of *, + and angle):
package de.bigdev
object VectorTest {
/**
* for testing only... or use ScalaTest!
*/
def main(args: Array[String]) {
val v = new Vector(1, 1)
val w = new Vector(-1, -1)
println("Vector algebra")
println("==============")
println("addition : " + v + " + " + w + " = " + (v + w))
println("scalar multip.: 2 * " + v + " = " + v * 2)
println("length : ||" +v + "|| = " + v.length)
println("scalar product: " + v + " * " + w + " = " + v * w)
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°
Labels:
Linear Algebra,
OOP,
Scala,
Tutorial,
Vector Spaces
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
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°
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°
Labels:
Java SE,
Linear Algebra,
OOP,
Tutorial,
Vector Spaces
Subscribe to:
Posts (Atom)
