0

I'm using Eclipse and I'm using Java. My objective it's to sort a vector, with the bogoSort method in one vector( vectorExample ) adapted to my type of vector and use the Java sort on other vector (javaVector) and compare them.

I did the tests but it did't work, so I don't know what is failing. *Note: there are few words in spanish: ordenado = sorted, Ejemplo = Example, maximo = maximun, contenido = content.

EjemploVector class

   package vector;
import java.util.NoSuchElementException;
import java.util.Vector;
import java.util.Iterator;

public class EjemploVector <T> {
protected T[] contenido;
private int numeroElementos;

@SuppressWarnings("unchecked")
public EjemploVector () {
    contenido = (T[]) new Object[100];
    numeroElementos = 0;
}

@SuppressWarnings("unchecked")
public EjemploVector (int maximo) {
    contenido = (T[]) new Object[maximo];
    numeroElementos = 0;
}

public String toString(){
    String toString="[";
     for (int k=0; k<numeroElementos;k++){
         if (k==numeroElementos-1){
             toString = toString + contenido[k].toString();
         } else {
             toString = toString + contenido[k].toString()+", ";
         }
     }
     toString = toString + "]";
     return toString;
}

public boolean equals (Object derecho){
     if (!(derecho instanceof Vector<?>)) {
         return false;
     } else if (numeroElementos != ((Vector<?>)derecho).size()) {
         return false;
     } else {
         Iterator<?> elemento = ((Vector<?>)derecho).iterator();
         for (int k=0; k<numeroElementos;k++){
             if (!((contenido[k]).equals (elemento.next()))) {
                 return false;
             }
         }
         return true;
     }
}

public void addElement (T elemento){
    contenido[numeroElementos++]= elemento;
}

protected T[] getContenido(){
    return this.contenido;
}

protected T getContenido (int k){
    return this.contenido[k];
}

@SuppressWarnings("unchecked")
protected void setContenido (int k, Object elemento){
    this.contenido[k]= (T)elemento;
}

EjemploVectorOrdenadoClass

package vector.ordenado;

import java.util.Arrays;
import java.util.Random;

import vector.EjemploVector;

public class EjemploVectorOrdenado<T extends Comparable<T>> extends EjemploVector<T> {

    private boolean organized;

    public EjemploVectorOrdenado() {
        super();
        organized = true;
    }

    public EjemploVectorOrdenado(int maximo) {
        super(maximo);
        organized = true; //
    }

    public boolean getOrdenado() {
        return this.organized;
    }

    // Method bogoSort
    public void bogoSort() {
        if (!this.organized) {

            if (this.size() > 0) {


                Random generator;
                T tempVariable;
                int randomPosition;

                do {
                    generator = new Random();

                    for (int i = 0; i < this.size(); i++) {
                        randomPosition = generator.nextInt(this.size());

                        tempVariable = contenido[i];
                        contenido[i] = contenido[randomPosition];
                        contenido[randomPosition] = tempVariable;

                    }
                } while (!organized);

            }

        }
        this.organized = true;
    }

    public void addElement(T elemento) {
        super.addElement(elemento);
        if (organized && this.size() > 1) {
            T penultimo = this.getContenido(this.size() - 2);
            T ultimo = this.getContenido(this.size() - 1);
            organized = penultimo.compareTo(ultimo) <= 0;
        }
    }
}

ElementoTest class

package elementos;

import java.io.Serializable;

public class ElementoTest implements Comparable<ElementoTest>, Serializable {
private static final long serialVersionUID = -7683744298261205956L;

private static int numeroElementosTest = 0;
private int clave;
private int  valor;

public ElementoTest(int i){
    this.clave = i;
    this.valor = numeroElementosTest;
    numeroElementosTest++;
}

public String toString(){
    return ("(" + this.clave + "," + this.valor + ")");
}

public boolean equals (Object derecho){
     if (!(derecho instanceof ElementoTest)) {
         return false;
     } else {
         return clave == ((ElementoTest)derecho).clave;
     }
}

public char getClave(){
      return this.clave;
}

public int getValor(){
      return this.valor;
}

@Override
public int compareTo(ElementoTest elemento) {
    if (elemento == null){
        return -1;
    } else if (this.equals(elemento)){
        return 0;
    } else if (clave < elemento.clave){
        return -1;
    } else {
        return 1;
    }
}

}

TESTS The first it's a stupid test, because it puts elements in order so... really the methods aren´t doing anything, java just compare and it gives correct

I tried to make an unsorted vector adding elements but there appears the java.lang.ClassCastException: [Ljava.... etc.

package vector.ordenado;

import static org.junit.Assert.*;

import java.util.Collections;
import java.util.Vector;

import org.junit.Before;
import org.junit.Test;

import elementos.ElementoTest;

public class EjemploVectorOrdenadoTest {

    private Vector<ElementoTest> vectorJava;
    private EjemploVectorOrdenado<ElementoTest> vectorExample;

    @Before
    public void setUp() throws Exception {
        vectorJava = new Vector<ElementoTest>(100);
        vectorExample = new EjemploVectorOrdenado<ElementoTest>(100);
    }

    @Test
    public void testSortFailTest() {
        for (char c = 'a'; c < 'g'; c++) {
            vectorJava.addElement(new ElementoTest(c));
            vectorExample.addElement(new ElementoTest(c));

        }
        Collections.sort(vectorJava);
        vectorExample.bogoSort();
        assertTrue(vectorExample.equals(vectorJava));
        assertTrue(vectorExample.getOrdenado());
    }

    @Test
    public void testSort() {
        {
            vectorJava.addElement(new ElementoTest(1));
            vectorJava.addElement(new ElementoTest(3));
            vectorJava.addElement(new ElementoTest(2));
            vectorExample.addElement(new ElementoTest(3));
            vectorExample.addElement(new ElementoTest(2));
            vectorExample.addElement(new ElementoTest(1));

        }
        Collections.sort(vectorJava);
        vectorExample.bogoSort();
        assertTrue(vectorExample.equals(vectorJava));
        assertTrue(vectorExample.getOrdenado());
    }
}

Sorry, for the problems and thanks.

1
  • Post the full stack trace Commented Apr 7, 2013 at 12:33

2 Answers 2

1

The problem is that your test class ElementoTest should implement the Comparable interface. Or you need to provide a Comparator during your comparison.

1
  • :/ have this in ElementoTest public class ElementoTest implements Comparable<ElementoTest>, Serializable { private static final long serialVersionUID = -7683744298261205956L; I'm not sure if you meant that Commented Apr 7, 2013 at 12:48
1

Does your class ElementtoTest implement Comparable?

If not, it needs to.

I'm suspecting it doesn't, because that's exactly what would cause this error. you'll need to add implements Comparable and then override the int compareTo(Elementtotest e) method, where you specify what criteria you'd like to order the objects based on.

16
  • :/ have this in ElementoTest public class ElementoTest implements Comparable<ElementoTest>, Serializable { private static final long serialVersionUID = -7683744298261205956L; I'm not sure if you meant that Commented Apr 7, 2013 at 12:47
  • Have you added a method public int compareTo(ElementoTest) ? Commented Apr 7, 2013 at 12:50
  • And also : @Override public int compareTo(ElementoTest elemento) { if (elemento == null){ return -1; } else if (this.equals(elemento)){ return 0; } else if (clave < elemento.clave){ return -1; } else { return 1; } } Commented Apr 7, 2013 at 12:50
  • you need to read up on how to write a compareTo() method. Remember, its purpose is to decide how to order a list of objects, usually based on some integral field. That being said, as long as you implement comparable you shouldn't be getting a ClassCastException. Please post the full stack trace (the full error message printed by your IDE) Commented Apr 7, 2013 at 12:55
  • java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable; at vector.ordenado.EjemploVectorOrdenado.bogoSort(EjemploVectorOrdenado.java:47) at vector.ordenado.EjemploVectorOrdenadoTest.testSort(EjemploVectorOrdenadoTest.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) Commented Apr 7, 2013 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.