[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Error executing


Hi! (back again =/ )

i'm trying to implement a simple application consisting of this classes:

/*
 * Concesionari.java
 *
 * Created on 14 de abril de 2006, 2:40
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */



package concesionario;

import java.io.*;
import java.util.*;



/**
 *
 *
 */
public class Concesionari {

    String name;
    Store concesionari;
   
   
       
    /** Creates a new instance of Concesionari */
    public Concesionari() {
         this.name = name;
     concesionari = new Store();
   
    }
 

 

public boolean add(Vehicle vehiculo) {
          return Store.add(vehiculo);
     }
 
}



/*
 * Vehicle.java
 *
 * Created on 13 de abril de 2006, 3:59
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package concesionario;

/**
 *
 * @author jolivan
 */


public class Vehicle {
   
    private float preu;
    private int any;
    private String model;
    private String numplate;
   
   
    /** Creates a new instance of Vehicle */
    public Vehicle(float preu, int any, String model, String numplate) {
                       
        setPreu(preu);
        setAnyFabricacio(any);
        setModel(model);
        setMatricula(numplate);
        getMatricula();
    }
   
   
    void setPreu(float preu) {
    this.preu = preu;
    }
   
    void setAnyFabricacio(int any) {
    this.any = any;
    }
   
    void setModel(String model) {
    this.model = model;
    }
   
    void setMatricula(String numplate) {
    this.numplate = numplate;
    }
   
    String getMatricula(){
        return this.numplate;
    }

}



/*
 * Store.java
 *
 * Created on 15 de abril de 2006, 5:57
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package concesionario;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 * Aggregate of products for sale
 *
 * @author UOC
 * @version 1.0
 */
public class Store {
     /**
      * Product container
      *
      * @see Collection
      */
     private List vehiculos;

     /**
      * By default, the store uses an <code>ArrayList</code> as container
      */
     public Store() {
          vehiculos = new ArrayList();
     }
     
     /**
      * Class constructor. The store is created as the contained implementation
      * is specified (this must have been created)
      *
      * @param products
      *            Container to be used
      * @throws NullCollectionException
      *             The container has not been created
      */
     public Store(List vehiculos) {
          if (vehiculos != null) {
               this.vehiculos = vehiculos;
          }
     }

     /**
      * Adds a product to the store
      *
      * @param product
      *            Product to add
      * @return <code> true </code> if correct. Otherwise <code> false </code>
      */
     public boolean add(Vehicle vehiculo) {
          return vehiculos.add(vehiculo);
     }

     /**
      * Removes a product from the store
      *
      * @param product
      *            Product to remove
      * @return <code> true </code> if correct. Otherwise <code> false </code>
      */
     public boolean remove(Vehicle vehiculo) {
          return vehiculos.remove(vehiculo);
     }

     /**
      * Shows the list of products in the store.
      */
     public void show() {
          List copy = new ArrayList(vehiculos);
          Collections.sort(copy);
          Iterator iterator = copy.iterator();
          while (iterator.hasNext()) {
               System.out.println((Vehicle)iterator.next());
          }
     }

}

It compiles cool, but when i'm testing it and try to show List after adding some objects, i receive this:

Exception in thread "main" java.lang.ClassCastException: concesionario.Car
        at java.util.Arrays.mergeSort(Arrays.java:1156)
        at java.util.Arrays.sort(Arrays.java:1080)
        at java.util.Collections.sort(Collections.java:117)
        at concesionario.Store.show(Store.java:82)
        at concesionario.Main.main(Main.java:72)

Thx for your help!
0
morsa804tons
Asked:
morsa804tons
  • 5
  • 3
  • 2
1 Solution
 
sriki77Commented:
Sort expects your objects to be Comparable. Make your vehicle class implement Comparable interface. Or provide a comparator during sort.
0
 
morsa804tonsAuthor Commented:
thx but... how i can provide a comparator during sort?
0
 
BogoJokerCommented:
Hi morsa804tons,

Just an improvment with java 1.5 enhanced forloop, this should work for your show() method:
public void show() {
  List copy = new ArrayList(vehiculos);
  for (Vehicle v : copy)
    System.out.println(v);
}

Joe P
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sriki77Commented:
use the method "public static void sort(List list, Comparator c)" method of Collections
0
 
BogoJokerCommented:
To use the mergeSort, you Objects must have a compareTo method defined.  That means they should implement Comparable, and you should define the compareTo() method.

Some tips:
1) Generics, you Vehicle class could be:
public class Vehicle implements Comparable<Vehicle>

2) define compareTo method:
public int compareTo(Vehicle otherVehicle) {
// you write the definition here (A)
}

3) You write the code for A.  That is normally comparing some vehicle code.  Like the vehicle has float preu, int any, String model, String numplage.  How do you want your list ordered?  By numplate?  Then you would just compare this.numplate agaist otherVehicle.getNumPlate().  Something like that.  The most common results are:
return -1 if this is less then other
return 0 if this is equal to other
return 1 if this is greater then other
You could have different degrees, but basically its negative, zero, or positive.

Joe P
0
 
BogoJokerCommented:
Whoop, you should probably ignore my post and do what sriki77 said.
Joe P
0
 
morsa804tonsAuthor Commented:
No, i was trying to implement your show method but it didn't work because incompatible types. I've done what u said me in your last post and it works... now, when i execute show method, it returns me objects saved in the list, i'm tryin' to print all objects attributtes.

Thx a lot! =)
0
 
BogoJokerCommented:
This did not really solve your main problem though did it?
Joe P
0
 
morsa804tonsAuthor Commented:
It helped me a lot to understand this kind of situations between objects, methods, etc... i'll try by myself to get every object attributes every time iterator advances.
0
 
BogoJokerCommented:
Generics, those ugly braces <> will really take a burden off of casting and allow java to identify objects AT COMPILE time and make your life much easier.  I myself am sitting down coding a OrderedLinkedList of Nodes all with generics right now, and in between methods posting here at EE =)
I am learning a heck of a lot too, and I like generics!

Joe P
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now