Class instantiation

Asked

Viewed 77 times

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



}
  • 1

    Your first error is in declaring the main method, you declared another method within it.

3 answers

1

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

I don’t quite understand your purpose, but I hope this code will help 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;
    }

}

0

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

0

Your two classes have some errors. The first is that you cannot 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. The ideal is that the class Vector is only responsible for storing the values referring to the Vector, ie dimension and the value. Following, not fully, the specification of the Javabeans.

Your method somar(Vector vector)... is receiving an object of type Vector in the same class, ie, you are passing yourself to yourself (kind of confused not?). I made a small example, improving a little 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;
    }
}  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.