Instantiation of class

2

Hello, I created a vector class and a method to add its elements.

However, in the instantiation of it, it is not returning, can it 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;
    }



}
    
asked by anonymous 06.09.2015 / 01:40

3 answers

1

Actually your code will not be compiled by some errors in the programming.

I did not quite understand your goal but I hope this code helps you:

public static void main(String[] args) {
    Vetor vetor1 = new Vetor(10);
    Vetor vetor2 = new Vetor(5);
    int soma = produtoPontos(vetor1, vetor2);

    System.out.println(vetor1.somar(vetor1));
    System.out.println(vetor2.somar(vetor2));
    System.out.println("Soma dos elementos: " +soma);

}

public static 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;
    }

}
    
06.09.2015 / 02:12
0

You can not create a second method within the main method! The Henry's answer is very good. But I would like to leave another:
Because the numbers are entered randomly you could print the numbers that were randomly generated to be able to check the sum, or you could just enter the numbers manually and check the process with the results obtained.

    
06.09.2015 / 02:07
0

Your two classes have some errors. The first is that you will not be able to create a method within another method similar to some languages, such as Delphi.

Another thing is to divide the responsibilities among the objects, that is, its Vector class is responsible for initializing the vector and also adding the same ones. Ideally, the Vector class is only responsible for storing the values in the Vector, that is, the dimension and the value . Following, not fully, the JavaBeans specification.

Your add vector method ... is getting an object of type Vector in the same class, that is, you are passing itself to itself (not confused?). I made a small example, improving it a bit, I hope you understand the code:

Main Class:

public class TesteVetor {
    public static void main(String args[]) {
        new TesteVetor();
    }

    public TesteVetor(){
        Vetor vetor1 = new Vetor(10);
        Vetor vetor2 = new Vetor(5);

        System.out.println("Soma dos elementos: " +somar(vetor1));
        System.out.println("Soma dos elementos: " +somar(vetor2));

        System.out.println("ProdutosPontos(): " + produtoPontos(vetor1, vetor2));
    }

    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;
        }
    }


    //Pode jogar este método aqui, tirando a responsabilidade da classe Vetor:
    public int somar(Vetor vetor) {
        int soma = 0;
        for (int j=0; j<vetor.getDimensao(); j++){
            soma += vetor.getValores()[j];
        }
        return soma;
    } }

Vector Class:

public class Vetor {

private int dimensao = 0;
private int[] valores;

//Construtor:
public Vetor (int dimensao){
    this.dimensao = dimensao;
    inicializa();
}

public int getDimensao() {
    return dimensao;
}

public void setDimensao(int dimensao) {
    this.dimensao = dimensao;
}

public int[] getValores() {
    return valores;
}

//Ideal é tirar esse método e "jogar" na classe TesteVetor:
public void inicializa(){
    valores = new int[dimensao];

    for (int i=0; i<dimensao; i++){
        valores[i] = (int)(Math.random()*10)+1;
    }
}  }
    
06.09.2015 / 20:46