Browse Source

Leo: Javadoc Kommentare hinzugefügt

master
fdai7303 1 year ago
parent
commit
ad1eba67eb
  1. 340
      CheckSparseVector.java
  2. 80
      SparseVector.java

340
CheckSparseVector.java

@ -1,171 +1,177 @@
public class CheckSparseVector {
public static void main(String[] args) {
//(ausgelagert)
// erstellen des this.Vektors = control Vector
SparseVector controlVector = new SparseVector(10);
controlVector.setElement(0, 10.0);
controlVector.setElement(5, 123.213);
controlVector.setElement(8, 65.01);
controlVector.setElement(9, 112.79);
controlVector.setElement(10, -212.79);
System.out.println("\n");
// erstellen der testVektoren (the 5 "others")
SparseVector otherVector1 = new SparseVector(10);
otherVector1.setElement(0, 10.0);
otherVector1.setElement(5, 123.213);
otherVector1.setElement(8, 65.01);
otherVector1.setElement(9, 112.79);
otherVector1.setElement(10, -212.79);
otherVector1.setElement(12, 12);
System.out.println("\n");
SparseVector otherVector2 = new SparseVector(5);
otherVector2.setElement(0, 0);
otherVector2.setElement(1, 2);
otherVector2.setElement(2, 5);
otherVector2.setElement(3, 11.0);
otherVector2.setElement(4, 22.2);
System.out.println("\n");
SparseVector otherVector3 = new SparseVector(20);
otherVector3.setElement(0,123);
otherVector3.setElement(5,11);
otherVector3.setElement(6,20);
otherVector3.setElement(7,7);
otherVector3.setElement(11,2);
otherVector3.setElement(14,3);
otherVector3.setElement(18,4);
System.out.println("\n");
SparseVector otherVector4 = new SparseVector(10); // ein leerer other. Vektor mit Länge 10
System.out.println("der Wert des otherVector4 an der Position 0 ist: "+ otherVector4.getElement(0)); // 0.0
System.out.println("der Wert des otherVector4 an der Position 5 ist: "+ otherVector4.getElement(5)); // 0.0
System.out.println("der Wert des otherVector4 an der Position 7 ist: "+ otherVector4.getElement(7));// 0.0
System.out.println("der Wert des otherVector4 an der Position 9 ist: " + otherVector4.getElement(9));// 0.0
System.out.println("die Länge des otherVector4 ist: " + otherVector4.getLength()); // 10
try{
System.out.println(otherVector4.getElement(99));
} catch(Exception e){
System.out.println(e);
}
try{
System.out.println(otherVector4.getElement(-1));
} catch(Exception e){
System.out.println(e);
}
System.out.println("\n");
SparseVector otherVector5 = new SparseVector(20); // ein leerer other. Vektor mit Länge 20
System.out.println("der Wert des otherVector5 an der Position 0 ist: "+ otherVector5.getElement(0)); // 0.0
System.out.println("der Wert des otherVector5 an der Position 5 ist: "+ otherVector5.getElement(5)); // 0.0
System.out.println("der Wert des otherVector5 an der Position 17 ist: "+ otherVector5.getElement(17));// 0.0
System.out.println("der Wert des otherVector5 an der Position 19 ist: " + otherVector5.getElement(19));// 0.0
System.out.println("die Länge des otherVector5 ist: " + otherVector5.getLength()); // 20
try{
System.out.println(otherVector5.getElement(20));
} catch(Exception e){
System.out.println(e);
}
try{
System.out.println(otherVector5.getElement(-1));
} catch(Exception e){
System.out.println(e);
}
System.out.println("\n");
// Testen des this. Vektors --> testen Methoden wie getLength(), getElement(), setElement(), removeElement()
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 123.213
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); //10.0
System.out.println("der Wert des controlVector an der Position 1 ist: " + controlVector.getElement(1)); //0.0
try{
System.out.println(controlVector.getElement(99));
} catch(Exception e){
System.out.println(e);
}
try{
System.out.println(controlVector.getElement(-1));
} catch(Exception e){
System.out.println(e);
}
System.out.println("\n");
// testen equal(), wenn die beiden Vektoren identisch sind should be true, not anymore :)
System.out.println(controlVector.equals(otherVector1));
System.out.println("\n");
controlVector.removeElement(5);
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 0.0
System.out.println("Die Länge nach remove ist: "+ controlVector.getLength()); // 10
System.out.println("\n");
controlVector.removeElement(7);
System.out.println("der Wert des controlVector an der Position 7 ist: "+ controlVector.getElement(7)); // 0.0
System.out.println("\n");
controlVector.setElement(5, 100);
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); //100.0
System.out.println("\n");
// testen equals(other) --> nicht identisch
System.out.println(controlVector.equals(otherVector1)); // should be false, weil der Wert an Index 5 zu 100 gesetzt wurde
System.out.println(controlVector.equals(otherVector2)); // should be false
System.out.println(controlVector.equals(otherVector3)); // should be false
System.out.println(controlVector.equals(otherVector4)); // should be false
System.out.println("\n");
otherVector1.setElement(5, 100);
System.out.println("der Wert des otherVector1 an der Position 5 ist: "+ otherVector1.getElement(5)); //100.0
System.out.println(otherVector1.equals(controlVector));// should be true, weil der Wert an Index 5 von this und other Vektor gleich 100
System.out.println("\n");
// testen add()
controlVector.add(otherVector1);
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 10 + 10 = 20
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 100 + 100 = 200
System.out.println("der Wert des controlVector an der Position 6 ist: " + controlVector.getElement(6)); // 0.0
System.out.println("der Wert des controlVector an der Position 8 ist: " + controlVector.getElement(8)); // 65.01 + 65.01 = 130.02
System.out.println("der Wert des controlVector an der Position 9 ist: " + controlVector.getElement(9)); // 112.79 + 112.79 = 225.58
System.out.println("\n");
controlVector.add(otherVector2);
controlVector.setElement(3,90.1);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 3 ist: " + controlVector.getElement(3)); // 90.1
System.out.println("\n");
controlVector.add(otherVector3);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("der Wert des controlVector an der Position 9 ist: "+ controlVector.getElement(9)); // 225.58
System.out.println("\n");
controlVector.add(otherVector4);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0));
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5));
System.out.println("\n");
controlVector.removeElement(0);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("\n");
controlVector.add(otherVector5);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("\n");
public static void main(String[] args) {
// (ausgelagert)
// erstellen des this.Vektors = control Vector
SparseVector controlVector = new SparseVector(10);
controlVector.setElement(0, 10.0);
controlVector.setElement(5, 123.213);
controlVector.setElement(8, 65.01);
controlVector.setElement(9, 112.79);
controlVector.setElement(10, -212.79);
System.out.println("\n");
// erstellen der testVektoren (the 5 "others")
SparseVector otherVector1 = new SparseVector(10);
otherVector1.setElement(0, 10.0);
otherVector1.setElement(5, 123.213);
otherVector1.setElement(8, 65.01);
otherVector1.setElement(9, 112.79);
otherVector1.setElement(10, -212.79);
otherVector1.setElement(12, 12);
System.out.println("\n");
SparseVector otherVector2 = new SparseVector(5);
otherVector2.setElement(0, 0);
otherVector2.setElement(1, 2);
otherVector2.setElement(2, 5);
otherVector2.setElement(3, 11.0);
otherVector2.setElement(4, 22.2);
System.out.println("\n");
SparseVector otherVector3 = new SparseVector(20);
otherVector3.setElement(0, 123);
otherVector3.setElement(5, 11);
otherVector3.setElement(6, 20);
otherVector3.setElement(7, 7);
otherVector3.setElement(11, 2);
otherVector3.setElement(14, 3);
otherVector3.setElement(18, 4);
System.out.println("\n");
SparseVector otherVector4 = new SparseVector(10); // ein leerer other. Vektor mit Länge 10
System.out.println("der Wert des otherVector4 an der Position 0 ist: " + otherVector4.getElement(0)); // 0.0
System.out.println("der Wert des otherVector4 an der Position 5 ist: " + otherVector4.getElement(5)); // 0.0
System.out.println("der Wert des otherVector4 an der Position 7 ist: " + otherVector4.getElement(7));// 0.0
System.out.println("der Wert des otherVector4 an der Position 9 ist: " + otherVector4.getElement(9));// 0.0
System.out.println("die Länge des otherVector4 ist: " + otherVector4.getLength()); // 10
try {
System.out.println(otherVector4.getElement(99));
} catch (Exception e) {
System.out.println(e);
}
try {
System.out.println(otherVector4.getElement(-1));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("\n");
SparseVector otherVector5 = new SparseVector(20); // ein leerer other. Vektor mit Länge 20
System.out.println("der Wert des otherVector5 an der Position 0 ist: " + otherVector5.getElement(0)); // 0.0
System.out.println("der Wert des otherVector5 an der Position 5 ist: " + otherVector5.getElement(5)); // 0.0
System.out.println("der Wert des otherVector5 an der Position 17 ist: " + otherVector5.getElement(17));// 0.0
System.out.println("der Wert des otherVector5 an der Position 19 ist: " + otherVector5.getElement(19));// 0.0
System.out.println("die Länge des otherVector5 ist: " + otherVector5.getLength()); // 20
try {
System.out.println(otherVector5.getElement(20));
} catch (Exception e) {
System.out.println(e);
}
try {
System.out.println(otherVector5.getElement(-1));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("\n");
// Testen des this. Vektors --> testen Methoden wie getLength(), getElement(),
// setElement(), removeElement()
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 123.213
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); // 10.0
System.out.println("der Wert des controlVector an der Position 1 ist: " + controlVector.getElement(1)); // 0.0
try {
System.out.println(controlVector.getElement(99));
} catch (Exception e) {
System.out.println(e);
}
try {
System.out.println(controlVector.getElement(-1));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("\n");
// testen equal(), wenn die beiden Vektoren identisch sind should be true, not
// anymore :)
System.out.println(controlVector.equals(otherVector1));
System.out.println("\n");
controlVector.removeElement(5);
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 0.0
System.out.println("Die Länge nach remove ist: " + controlVector.getLength()); // 10
System.out.println("\n");
controlVector.removeElement(7);
System.out.println("der Wert des controlVector an der Position 7 ist: " + controlVector.getElement(7)); // 0.0
System.out.println("\n");
controlVector.setElement(5, 100);
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 100.0
System.out.println("\n");
// testen equals(other) --> nicht identisch
System.out.println(controlVector.equals(otherVector1)); // should be false, weil der Wert an Index 5 zu 100 gesetzt
// wurde
System.out.println(controlVector.equals(otherVector2)); // should be false
System.out.println(controlVector.equals(otherVector3)); // should be false
System.out.println(controlVector.equals(otherVector4)); // should be false
System.out.println("\n");
otherVector1.setElement(5, 100);
System.out.println("der Wert des otherVector1 an der Position 5 ist: " + otherVector1.getElement(5)); // 100.0
System.out.println(otherVector1.equals(controlVector));// should be true, weil der Wert an Index 5 von this und
// other Vektor gleich 100
System.out.println("\n");
// testen add()
controlVector.add(otherVector1);
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); // 10 + 10 =
// 20
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 100 + 100
// = 200
System.out.println("der Wert des controlVector an der Position 6 ist: " + controlVector.getElement(6)); // 0.0
System.out.println("der Wert des controlVector an der Position 8 ist: " + controlVector.getElement(8)); // 65.01 +
// 65.01 =
// 130.02
System.out.println("der Wert des controlVector an der Position 9 ist: " + controlVector.getElement(9)); // 112.79 +
// 112.79 =
// 225.58
System.out.println("\n");
controlVector.add(otherVector2);
controlVector.setElement(3, 90.1);
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 3 ist: " + controlVector.getElement(3)); // 90.1
System.out.println("\n");
controlVector.add(otherVector3);
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 200
System.out.println("der Wert des controlVector an der Position 9 ist: " + controlVector.getElement(9)); // 225.58
System.out.println("\n");
controlVector.add(otherVector4);
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0));
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5));
System.out.println("\n");
controlVector.removeElement(0);
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 200
System.out.println("\n");
controlVector.add(otherVector5);
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 200
System.out.println("\n");
}
}

80
SparseVector.java

@ -1,6 +1,13 @@
/**
* Sparse Implementierung einer verkettete Liste
*/
public class SparseVector {
//Paul:
/**
* [Beschreibung]
*
* @author Paul
*/
private class Node {
int index;
double value;
@ -14,27 +21,45 @@ public class SparseVector {
}
//Elif: Standard Konstruktor mit einem leeren Vektor initialisieren
// Elif: Standard Konstruktor mit einem leeren Vektor initialisieren
private Node head = null;
private int length = 0;
/**
* Konstruktor mit Vektor länge 0
*
* @author Elif
*/
public SparseVector() {
this.head = null;
this.length = 0;
}
//Elif: Konstruktor mit Vektor länge n
/**
* Konstruktor mit Vektor länge n
*
* @author Elif
* @param n Vektor länge
*/
public SparseVector(int n) {
this.head = null;
this.length = n;
}
//Elif: den value in index hinzufügen - Aktualiiseren des Wertes, wenn der head an dem Index mit einem Wert exisitiert - Neuer head mit neuem Wert hinzufügen, wenn der head an dem Index nicht existiert
/**
* den value in index hinzufügen - Aktualiiseren des Wertes, wenn der head an
* dem Index mit einem Wert exisitiert - Neuer head mit neuem Wert hinzufügen,
* wenn der head an dem Index nicht existiert
*
* @author Elif
* @param index [Beschreibung]
* @param value [Beschreibung]
*/
public void setElement(int index, double value) {
removeElement(index);
int maxIndex = this.getLength()-1;
int maxIndex = this.getLength() - 1;
if (index > maxIndex) {
System.out.println("Fehler: Der Index " + index + " ist außerhalb des erlaubten Bereichs.");
@ -65,7 +90,14 @@ public class SparseVector {
}
//Paul: getElement: return the Wert value of that index input das Index, und return den entsprechenden Wert des Index
/**
* return the Wert value of that index input das Index, und return den
* entsprechenden Wert des Index
*
* @author Paul
* @param index [Beschreibung]
* @return double [Beschreibung]
*/
public double getElement(int index) {
if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
@ -82,11 +114,15 @@ public class SparseVector {
now = now.next;
}
return 0.0;
}
//Leonhard: entfernt Element nach Index
/**
* entfernt Element nach Index
*
* @author Leonhard
* @param index [Beschreibung]
*/
public void removeElement(int index) {
// previous = pre
Node now = this.head;
@ -103,13 +139,19 @@ public class SparseVector {
}
if (now != null && now.index == index) {
pre.next = now.next;
// System.out.println("index " + index + " found and the value " + now.value + " deleted.");
// System.out.println("index " + index + " found and the value " + now.value + "
// deleted.");
} else {
// System.out.println("Element not found with index " + index);
}
}
//Leonhard: Länge des Vektors ausgeben
/**
* Länge des Vektors ausgeben
*
* @author Leonhard
* @return int [Beschreibung]
*/
public int getLength() {
int length = SparseVector.this.length;
@ -118,10 +160,16 @@ public class SparseVector {
}
//Terry: bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
/**
* testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
*
* @author Terry
* @param other [Beschreibung]
* @return boolean [Beschreibung]
*/
public boolean equals(SparseVector other) {
if(this.getLength() != other.getLength()){
if (this.getLength() != other.getLength()) {
return false;
@ -152,10 +200,14 @@ public class SparseVector {
return true;
}
// Terry: void add: to add two vectors together and renew (overwrite) the this.vector
/**
* to add two vectors together and renew (overwrite) the this.vector
*
* @author Terry
* @param other [Beschreibung]
*/
public void add(SparseVector other) {
Node thisnow = this.head;
Node othernow = other.head;
Node thispre = null;

Loading…
Cancel
Save