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
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
Comment