Printing a sort array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jromero
    New Member
    • Sep 2007
    • 16

    #1

    Printing a sort array

    I have a problem with printing the array



    Here is the print out



    Picture a vehicle number 1 : What's it's max speed?

    100

    Picture a vehicle number 2: What's it's max speed?

    20

    Picture a vehicle number 3: What's it's max speed?

    5

    Picture a vehicle number 4: What's it's max speed?

    5

    Picture a vehicle number 5: What's it's max speed?

    6

    Exception in thread "main" java.lang.Class CastException: Vehicle

    at VehicleTest.sor t(VehicleTest.j ava:46)

    at VehicleTest.mai n(VehicleTest.j ava:35)





    Here is the code:

    [







    [CODE=java]import java.util.Scann er;



    class VehicleTest {



    public static void main(String[] args)

    {

    Vehicle[] vh = new Vehicle[5];



    Scanner scanner = new Scanner(System. in);



    for (int i = 0; i < vh.length; i++)

    vh[i] = new Vehicle();



    System.out.prin tln("Picture a vehicle number 1 : What's it's max speed?");

    double temp = scanner.nextDou ble();

    vh[0].setmaxSpeed(te mp);

    System.out.prin tln("Picture a vehicle number 2: What's it's max speed?");

    temp = scanner.nextDou ble();

    vh[1].setmaxSpeed(te mp);

    System.out.prin tln("Picture a vehicle number 3: What's it's max speed?");

    temp = scanner.nextDou ble();

    vh[2].setmaxSpeed(te mp);

    System.out.prin tln("Picture a vehicle number 4: What's it's max speed?");

    temp = scanner.nextDou ble();

    vh[3].setmaxSpeed(te mp);

    System.out.prin tln("Picture a vehicle number 5: What's it's max speed?");

    temp = scanner.nextDou ble();

    vh[4].setmaxSpeed(te mp);



    sort( vh); - line 35 Error problem

    printList(vh);





    }

    public static void sort(Object[]vh){

    Object maxSpeed;

    int currentmaxIndex ;

    for (int i = vh.length - 1; i >= 1; i--){

    maxSpeed = vh[i];

    currentmaxIndex = i;

    for (int j = i -1; j>= 0; j--){

    if (((Comparable)m axSpeed).compar eTo(vh[j]) < 0){ - Line 45 –Error problem

    maxSpeed = vh[j];

    currentmaxIndex = j;

    }

    }

    if(currentmaxIn dex != i){

    vh[currentmaxIndex] = vh[i];

    vh[i] = maxSpeed;

    }

    }

    }

    public static void printList(Objec t []vh){

    for (int i= 0; i < vh.length; i++)

    System.

    out.print(vh[i] + " ");

    System.

    out.println();



    }

    }[/CODE]


    ]

    Please help me
    Last edited by Ganon11; Nov 21 '07, 04:10 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I would guess that Vehicle has not properly implented the Comparable interface. Have you given it a .compareTo() method that works properly? Also, since you're sorting, you should assume the array is able to be compared (otherwise, there's no way to sort), so you can accept an array of Comparable objects rather than an array of Object objects.

    Comment

    • Jromero
      New Member
      • Sep 2007
      • 16

      #3
      Here is the code for Vehicle class

      Can you explain what you wrote about the comparable array, because I still don't get it

      code[


      public class Vehicle {


      private double maxSpeed;
      private double price;
      private String color;

      public Vehicle() {

      }

      public Vehicle(double maxSpeed, double price, String color) {
      this.maxSpeed = maxSpeed;
      this.price = price;
      this.color = color;


      }

      public double getmaxSpeed(){
      return maxSpeed;
      }

      public void setmaxSpeed(dou ble maxSpeed){
      this.maxSpeed = maxSpeed ;
      }

      public double getprice(){
      return price;

      }


      public void setprice(double price){
      this.price = price;
      }

      public String getcolor(){
      return color;

      }


      public void setcolor(String color){
      this.color = color;
      }

      public int compareTo(Objec t o) {
      if (getmaxSpeed () > ((Vehicle)o).ge tmaxSpeed())
      return 1;
      else if (getmaxSpeed () < ((Vehicle)o).ge tmaxSpeed())
      return -1;
      else
      return 0;

      }
      ]

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        1.) Please use code tags when posting code
        2.) You are trying to cast a Vehicle object to a Comparable object but your Vehicle objects are not Comparable objects. One way of making your Vehicles Comparable is by making the Vehicle class to implement the Comparable interface.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          Originally posted by Jromero
          Here is the code for Vehicle class

          Can you explain what you wrote about the comparable array, because I still don't get it

          code[


          public class Vehicle {


          private double maxSpeed;
          private double price;
          private String color;

          public Vehicle() {

          }

          public Vehicle(double maxSpeed, double price, String color) {
          this.maxSpeed = maxSpeed;
          this.price = price;
          this.color = color;


          }

          public double getmaxSpeed(){
          return maxSpeed;
          }

          public void setmaxSpeed(dou ble maxSpeed){
          this.maxSpeed = maxSpeed ;
          }

          public double getprice(){
          return price;

          }


          public void setprice(double price){
          this.price = price;
          }

          public String getcolor(){
          return color;

          }


          public void setcolor(String color){
          this.color = color;
          }

          public int compareTo(Objec t o) {
          if (getmaxSpeed () > ((Vehicle)o).ge tmaxSpeed())
          return 1;
          else if (getmaxSpeed () < ((Vehicle)o).ge tmaxSpeed())
          return -1;
          else
          return 0;

          }
          ]
          You should read/learn more about "interface" and "casting".
          The priciple is: if you cast a dog to an animal, it works. But if you try to cast a stone to an animal ...

          more specific:
          If you cast your object to Comparable in your code with
          Code:
          ...(Comparable)maxSpeed ...
          then your class should implement the Comparable interface, like this:
          Code:
          public class Vehicle implements Comparable

          Comment

          • Jromero
            New Member
            • Sep 2007
            • 16

            #6
            Thank you so much is working now ....

            Comment

            Working...