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:
[
Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- 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.println("Picture a vehicle number 1 : What's it's max speed?");
- double temp = scanner.nextDouble();
- vh[0].setmaxSpeed(temp);
- System.out.println("Picture a vehicle number 2: What's it's max speed?");
- temp = scanner.nextDouble();
- vh[1].setmaxSpeed(temp);
- System.out.println("Picture a vehicle number 3: What's it's max speed?");
- temp = scanner.nextDouble();
- vh[2].setmaxSpeed(temp);
- System.out.println("Picture a vehicle number 4: What's it's max speed?");
- temp = scanner.nextDouble();
- vh[3].setmaxSpeed(temp);
- System.out.println("Picture a vehicle number 5: What's it's max speed?");
- temp = scanner.nextDouble();
- vh[4].setmaxSpeed(temp);
- 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)maxSpeed).compareTo(vh[j]) < 0){ - Line 45 –Error problem
- maxSpeed = vh[j];
- currentmaxIndex = j;
- }
- }
- if(currentmaxIndex != i){
- vh[currentmaxIndex] = vh[i];
- vh[i] = maxSpeed;
- }
- }
- }
- public static void printList(Object []vh){
- for (int i= 0; i < vh.length; i++)
- System.
- out.print(vh[i] + " ");
- System.
- out.println();
- }
- }
]
Please help me