-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorInteger.java
156 lines (135 loc) · 3.19 KB
/
VectorInteger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package prg.es1;
import java.util.ArrayList;
import java.lang.IndexOutOfBoundsException;
import java.lang.ArithmeticException;
public class VectorInteger implements Comparable<VectorInteger>{
private ArrayList<Integer> vector;
//COSTRUTTORI
public VectorInteger(){
this(10);
}
public VectorInteger(int dim){
vector = new ArrayList<>(dim);
for(int i = 0; i < dim; i++){
vector.add(0);
}
}
//METODI GET
public Integer get(int index){
if(index < vector.size()){
return vector.get(index);
} else {
throw new IndexOutOfBoundsException();
}
}
//METODI SET
public VectorInteger set(int index, Integer elem){
if(index < this.size()){
vector.set(index, elem);
} else {
throw new IndexOutOfBoundsException();
}
return this;
}
//OPERAZIONI
//somma
public VectorInteger sum(VectorInteger add){
if(this.size() == add.size()){
VectorInteger result = new VectorInteger(this.size());
for(int i = 0; i < this.size(); i++){
result.set(i, this.get(i) + add.get(i));
}
return result;
} else {
throw new ArithmeticException();
}
}
//differenza
public VectorInteger subtract(VectorInteger add){
if(this.size() == add.size()){
VectorInteger result = new VectorInteger(this.size());
for(int i = 0; i < this.size(); i++){
result.set(i, this.get(i) - add.get(i));
}
return result;
} else {
throw new ArithmeticException();
}
}
//prodotto scalare
public int dotProduct(VectorInteger other){
if(this.size() == other.size()){
int sum = 0;
for(int i = 0; i < this.size(); i++){
sum += this.get(i) * other.get(i);
}
return sum;
} else {
throw new ArithmeticException();
}
}
//prodotto per scalare
public VectorInteger multiplicationByScalar(int fatt){
VectorInteger result = new VectorInteger(this.size());
for(int i = 0; i < this.size(); i++){
result.set(i, this.get(i) * fatt);
}
return result;
}
//modulo
public double module(){
int sumOfSquared = 0;
for(Integer i : vector){
sumOfSquared += Math.pow(i, 2);
}
return Math.sqrt(sumOfSquared);
}
//size
public int size(){
return vector.size();
}
//equals
public boolean equals(Object other){
if(this == other){
return true;
} else if(other instanceof VectorInteger){
VectorInteger otherVector = (VectorInteger) other;
return this.vector.equals(otherVector.vector);
} else {
return false;
}
}
//hashCode
public int hashCode(){
int sum = 0;
for(int i = 0 ; i < this.size(); i++){
sum += Math.pow(-1, i) * this.get(i) * (i + 1);
}
return sum;
}
//interfacicia Comparable<VectorInteger>
//compareTo -> in base al confronto dei moduli
public int compareTo(VectorInteger other){
if(this.equals(other)){
return 0;
} else if (this.module() > other.module()){
return 1;
} else {
return -1;
}
}
//toString
public String toString(){
String st = "[ ";
for(Integer i : this.vector){
st += i + " ";
}
st += "]";
return st;
}
//toPrint
public VectorInteger toPrint(){
System.out.println(this.toString());
return this;
}
}