2
Hello, I created a vector class and a method to add its elements.
However, in the instantiation of the same, not returning, can help?
public class TesteVetor {
public static void main(){
Vetor vetor1 = new Vetor(10);
Vetor vetor2 = new Vetor(5);
public int produtoPontos(Vetor v1, Vetor v2) {
int soma = 0;
if (v1.getDimensao() == v2.getDimensao()){
for (int i=0; i<v1.getDimensao(); i++){
soma += v1.getValores()[i] * v2.getValores()[i];
}
return soma;
}
else {
return -1;
}
}
System.out.println ("Soma dos elementos: ", +somar(vetor1));
}
}
------> Vector class below:
public class Vetor {
private int dimensao = 0;
private int[] valores;
public int getDimensao() {
return dimensao;
}
public void setDimensao(int dimensao) {
this.dimensao = dimensao;
}
public int[] getValores() {
return valores;
}
public Vetor (int dimensao){
this.dimensao = dimensao;
this.inicializa();
}
public void inicializa (){
valores = new int[dimensao];
for (int i=0; i<dimensao; i++){
valores[i] = (int)(Math.random()*10)+1;
}
}
public int somar(Vetor vetor) {
int soma = 0;
for (int j=0; j<vetor.getDimensao(); j++){
soma += vetor.getValores()[j];
}
return soma;
}
}
Your first error is in declaring the main method, you declared another method within it.
– Macario1983