This commit is contained in:
Matti 2024-04-19 14:46:25 +02:00
parent c2cc7330d5
commit 7fd326360f
3 changed files with 102 additions and 1 deletions

View File

@ -13,7 +13,7 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
- Vorlesung 7 komplett
- Vorlesung 8 komplett
- Vorlesung 9 Aufgabe 2
- Vorlesung 10 Aufgabe 1
- Vorlesung 10 komplett
## Angefangene unfertige Lösungen:
- Damenproblem: VL 5 Aufgabe 2

View File

@ -0,0 +1,7 @@
package part10.aufg2;
public class NegativeValueException extends RuntimeException{
NegativeValueException(String s) {
super(s);
}
}

View File

@ -0,0 +1,94 @@
package part10.aufg2;
import java.util.Arrays;
public class PositiveVector {
double[] values;
PositiveVector(int size){
this.values = new double[size];
Arrays.fill(values, 0);
}
public int length(){
return this.values.length;
}
public void setValues(double[] values) {
for (int i=0; i<values.length; i++){
assert (values[i] >= 0) : "Value cannot be negative";
}
this.values = values;
}
public double[] getValues() {
return values;
}
public void setSingleValue(double value, int index){
if (value < 0){
System.out.println(value + " is negativ and cannot be assigned!");
return;
}
this.values[index] = value;
}
public double getSingleValue(int index){
return this.values[index];
}
public void print(){
System.out.println(Arrays.toString(this.values));
}
public PositiveVector vectorAddition(PositiveVector vector2){
PositiveVector result = new PositiveVector(this.length());
if (this.values.length == vector2.getValues().length){
for (int i=0; i<this.values.length; i++){
result.setSingleValue((this.getSingleValue(i) + vector2.getSingleValue(i)), i);
}
}
return result;
}
public PositiveVector vectorSubtraction(PositiveVector vector2) throws NegativeValueException {
PositiveVector result = new PositiveVector(this.length());
if (this.values.length == vector2.getValues().length){
for (int i=0; i<this.values.length; i++){
if ((this.getSingleValue(i) - vector2.getSingleValue(i)) < 0){
throw new NegativeValueException(this.getSingleValue(i) + " - " + vector2.getSingleValue(i) + " ist negativ");
}
result.setSingleValue((this.getSingleValue(i) - vector2.getSingleValue(i)), i);
}
}
return result;
}
public double dotProduct(PositiveVector vector2) {
double result = 0;
if (this.length() == vector2.length()) {
for (int i = 0; i < this.length(); i++) {
result += this.getSingleValue(i) * vector2.getSingleValue(i);
}
}
return result;
}
public static void main(String[] args){
PositiveVector vector1 = new PositiveVector(3);
PositiveVector vector2 = new PositiveVector(3);
vector1.setValues(new double[] {1,0,5});
vector2.setValues(new double[] {0,0,1});
PositiveVector vector3 = vector1.vectorSubtraction(vector2);
vector3.print();
System.out.println(vector1.dotProduct(vector2));
}
}