Commit 03bd2223 authored by Baris Numanoglu's avatar Baris Numanoglu
Browse files

Vektorklasse zur Übungszwecken in Java hinzugefügt

parent a60fc369
Pipeline #10493 passed with stage
in 3 minutes and 25 seconds
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>VectorDemo</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21
package vectorDemo;
public class Main {
public static void main(String[] args) {
Vector2D v1 = new Vector2D(3, 4);
Vector2D v2 = new Vector2D(1, 2);
System.out.println("v1: " + v1);
System.out.println("v2: " + v2);
System.out.println("Länge von v1: " + v1.magnitude());
System.out.println("v1 normalisiert: " + v1.normalize());
Vector2D v3 = v1.add(v2);
System.out.println("v1 + v2: " + v3);
Vector2D v4 = v1.subtract(v2);
System.out.println("v1 - v2: " + v4);
System.out.println("Skalarprodukt von v1 und v2: " + v1.dot(v2));
}
}
package vectorDemo;
// Dummy Vektor.Klasse zur Übungzwecken elementarer Grammatik und merge_request
public class Vector2D {
private double x;
private double y;
// Konstruktor
public Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
// Getter und Setter
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
// Länge des Vektors (Norm)
public double magnitude() {
return Math.sqrt(x * x + y * y);
}
// Normalisierung des Vektors
public Vector2D normalize() {
double mag = magnitude();
if (mag == 0) {
throw new ArithmeticException("Cannot normalize a zero vector");
}
return new Vector2D(x / mag, y / mag);
}
// Vektoraddition
public Vector2D add(Vector2D other) {
return new Vector2D(this.x + other.x, this.y + other.y);
}
// Vektorsubtraktion
public Vector2D subtract(Vector2D other) {
return new Vector2D(this.x - other.x, this.y - other.y);
}
// Skalar-Multiplikation
public Vector2D multiply(double scalar) {
return new Vector2D(this.x * scalar, this.y * scalar);
}
// Skalarprodukt (Dot-Produkt)
public double dot(Vector2D other) {
return this.x * other.x + this.y * other.y;
}
// String-Repräsentation
@Override
public String toString() {
return "Vector2D(" + x + ", " + y + ")";
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment