How to pass the array from a method to Another method using interface

Asked

Viewed 72 times

-4

I want to create a program that uses interfaces to create a vector class with name and size and have the methods below:

<> +Definirnometamanhovetor +PreencherVetorCriadoInformandoPosicaoValor +Orderer +Exibirvetor

I cannot pass the vector of a method to using the interface.

Follows the class of the vector: package classa06Cencapsulation training;

import java.util.Arrays;

public class Vetor Implements Interfacecontroladora {

private int posicaovetor = 0;
private int tamanhovetor = 0;
private String nomevetor = "";


/**
 * @return the nome
 */
public String getNome() {
    return nomevetor;
}

/**
 * @param nome the nome to set
 */
public void setNome(String nome) {
    this.nomevetor = nome;
}

/**
 * @param posicao
 * @param tamanho
 */
public Vetor(int posicao, int tamanho) {
    super();
    this.posicaovetor = posicao;
    this.tamanhovetor = tamanho;
    this.nomevetor = nomevetor;
}

/**
 * @return the posicao
 */
private int getPosicao() {
    return posicaovetor;
}

/**
 * @param posicao the posicao to set
 */
private void setPosicao(int posicao) {
    this.posicaovetor = posicao;
}

/**
 * @return the tamanho
 */
private int getTamanho() {
    return tamanhovetor;
}

/**
 * @param tamanho the tamanho to set
 */
private void setTamanho(int tamanho) {
    this.tamanhovetor = tamanho;
}

@Override
public void metodoDefinicaoVetor(String nomevetor, int tamanhovetor) {
    // TODO Auto-generated method stub
    setNome(nomevetor);
    setTamanho(tamanhovetor);
    int v[] = new int[getTamanho()];
    Arrays.fill(v, 0);
    System.out.println("Nome do vetor = " + getNome());
    // Impressão dos valores dentro da posição do vetor.
    for (int i = 0; i < v.length; i++) { //
        System.out.printf(" " + v[i]);
    }
    System.out.println("");
}

@Override
public void metodoPreencimentoVetor() {
    // TODO Auto-generated method stub

}

/*
 * @Override public void metodoOrdenacaoVetor() { // TODO Auto-generated method
 * stub int v[] = new int [getTamanho()]; Arrays.sort(v); }
 * 
 * @Override public void metodoBuscarValorVetor() { // TODO Auto-generated
 * method stub
 * 
 * }
 */

}

I can’t pass the vector methodDefinicaViewer to the methodPreencimentViewer

How can I do that with this program? Can I link to git with my program here?

  • I didn’t understand the question. Before clarifying it, read a little about SOLID and Design Pattern maybe help you in reformulating.

  • Follow Main to understand what I need: package classes 06CencapsulationTraining; import java.io.Objectinputstream.Getfield; public class Programmain { /** * @param args */ public Static void main(String[] args) { // TODO Auto-generated method stub // Instantiating and creating a vector with name and size via constructor. Vector 1 = new Vector("created vector01", 5); // Defining the name and size of the new vector vector 1.methodDefiniVetor("created vector02", 3); vector 1.methodPreencimentoVetor(); //vector 1.methodOrdenacaoVetor(); } }

  • Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

1 answer

-1


If you need to use interface, just add the interface as a parameter of your method. All objects of the classes that implement their interface can be passed as parameters. Another way is to declare your "v[]" as a variable outside the method.

    @Override
public void metodoDefinicaoVetor(String nomevetor, int tamanhovetor) {
    // TODO Auto-generated method stub
    setNome(nomevetor);
    setTamanho(tamanhovetor);
    v[] = new int[getTamanho()]; // declara o "int v[]" fora desse método. Aqui faz somente a inicilização dele.
    Arrays.fill(v, 0);
    System.out.println("Nome do vetor = " + getNome());
    // Impressão dos valores dentro da posição do vetor.
    for (int i = 0; i < v.length; i++) { //
        System.out.printf(" " + v[i]);
    }
    System.out.println("");
}

@Override
public void metodoPreencimentoVetor() {
    // TODO Auto-generated method stub

}
  • I improved the question and posted the entire program to facilitate... the idea is to use the vector created in the filling method for operations, search and sorting. [https://answall.com/questions/473266/como-associar-um-vetor-%C3%a1-uma-Vari%C3%a1vel-para-use-among-m%C3%a9all]

Browser other questions tagged

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