Bom dia, estava tentando resolver um exercicio mas não consigo devolver os valores dentro do vetor para a classe main, alguém teria alguma solução?
Estou tentando gerar números aleatórios para ordenar em Bubble Sort, mas não consigo passar os valores, desde já agradeço.
public class Ordenacao {
public static void printVetor(tipoOrdenacao ord){
System.out.println("Vetor Desordenado = " + (ord.getVetor()));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Bubble Sort");
Ordenacao.printVetor(new BubbleSort());
System.out.println("\n");
System.exit(0);
}
}
public abstract class tipoOrdenacao {
public abstract int[] getVetor();
}
import java.util.Random;
public class BubbleSort extends tipoOrdenacao{
Random r = new Random();
private int[] bubble = new int [100];
public int[] getBubble() {
return bubble;
}
public void setBubble(int bubble) {
for (int k=0; k < 5; k++){
this.bubble[k]=r.nextint(11);
}
for(int i=0; i < 5; i++) {
this.bubble[i] = bubble;
}
}
@Override
public int[] getVetor() {
return bubble[];
}
}
// neste lçao você está preenchendo o array bubble com valores aleatórios
for (int k=0; k < 5; k++){
this.bubble[k]=r.nextint(11);
}
// e logo em seguida você está fazendo outro laço, matando os valores aleatorios com o valor do parâmetro bubble
for(int i=0; i < 5; i++) {
this.bubble[i] = bubble;
}
Dei uma boa olhada no seu código e está muiiiiiiiiiiito confuso, tanto que não sei pra q ele serve, tentei ajeitar o melhor que pude, porém seria melhor apagar e começar tudo de novo, tinham muitas partes que não serviam para absolutamente nada.
public abstract class TipoOrdenacao {
public abstract int[] getVetor();
public abstract void gerarBubble();
}
public class BubbleSort extends TipoOrdenacao {
private Random r;
private int[] bubble;
public BubbleSort() {
r = new Random();
bubble = new int[100];
}
@Override
public void gerarBubble() {
for (int k = 0; k < 100; k++) {
this.bubble[k] = r.nextInt(100);
}
}
@Override
public int[] getVetor() {
return bubble;
}
}
public final class Ordenacao {
private BubbleSort bs;
public Ordenacao() {
gerarVetor();
}
private void gerarVetor() {
bs = new BubbleSort();
bs.gerarBubble();
System.out.println("Bubble Sort");
printVetor(bs);
System.out.println("\n");
System.exit(0);
}
private void printVetor(TipoOrdenacao ord) {
for (int v : ord.getVetor()) {
System.out.println("Vetor Desordenado = " + v);
}
}
}
public class Teste {
public static void main(String[] args) {
new Ordenacao();
}
}