473,698 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing a sort array

16 New Member
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:

[







Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5. class VehicleTest {
  6.  
  7.  
  8.  
  9.       public static void main(String[] args)
  10.  
  11.       {
  12.  
  13.       Vehicle[] vh = new Vehicle[5];
  14.  
  15.  
  16.  
  17.       Scanner scanner = new Scanner(System.in);
  18.  
  19.  
  20.  
  21.       for (int i = 0; i < vh.length; i++)
  22.  
  23.       vh[i] = new Vehicle();
  24.  
  25.  
  26.  
  27.       System.out.println("Picture a vehicle number 1 : What's it's max speed?");
  28.  
  29.       double temp = scanner.nextDouble();
  30.  
  31.       vh[0].setmaxSpeed(temp);
  32.  
  33.       System.out.println("Picture a vehicle number 2: What's it's max speed?");
  34.  
  35.       temp = scanner.nextDouble();
  36.  
  37.       vh[1].setmaxSpeed(temp);
  38.  
  39.       System.out.println("Picture a vehicle number 3: What's it's max speed?");
  40.  
  41.       temp = scanner.nextDouble();
  42.  
  43.       vh[2].setmaxSpeed(temp);
  44.  
  45.       System.out.println("Picture a vehicle number 4: What's it's max speed?");
  46.  
  47.       temp = scanner.nextDouble();
  48.  
  49.       vh[3].setmaxSpeed(temp);
  50.  
  51.       System.out.println("Picture a vehicle number 5: What's it's max speed?");
  52.  
  53.       temp = scanner.nextDouble();
  54.  
  55.       vh[4].setmaxSpeed(temp);
  56.  
  57.  
  58.  
  59.       sort( vh); - line 35 Error problem 
  60.  
  61.       printList(vh);
  62.  
  63.  
  64.  
  65.  
  66.  
  67.       }
  68.  
  69.       public static void sort(Object[]vh){
  70.  
  71.       Object maxSpeed;
  72.  
  73.       int currentmaxIndex;
  74.  
  75.       for (int i = vh.length - 1; i >= 1; i--){
  76.  
  77.       maxSpeed = vh[i];
  78.  
  79.       currentmaxIndex = i;
  80.  
  81.       for (int j = i -1; j>= 0; j--){
  82.  
  83.       if (((Comparable)maxSpeed).compareTo(vh[j]) < 0){  - Line 45 –Error problem 
  84.  
  85.       maxSpeed = vh[j];
  86.  
  87.       currentmaxIndex = j;
  88.  
  89.       }
  90.  
  91.       }
  92.  
  93.       if(currentmaxIndex != i){
  94.  
  95.       vh[currentmaxIndex] = vh[i];
  96.  
  97.       vh[i] = maxSpeed;
  98.  
  99.       }
  100.  
  101.       }
  102.  
  103.       }
  104.  
  105.       public static void printList(Object []vh){
  106.  
  107.       for (int i= 0; i < vh.length; i++)
  108.  
  109.       System.
  110.  
  111.       out.print(vh[i] + " ");
  112.  
  113.       System.
  114.  
  115.       out.println();
  116.  
  117.  
  118.  
  119.       }
  120.  
  121.       }

]

Please help me
Nov 21 '07 #1
5 1798
Ganon11
3,652 Recognized Expert Specialist
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.
Nov 21 '07 #2
Jromero
16 New Member
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;

}
]
Nov 21 '07 #3
r035198x
13,262 MVP
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.
Nov 21 '07 #4
chaarmann
785 Recognized Expert Contributor
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
Expand|Select|Wrap|Line Numbers
  1. ...(Comparable)maxSpeed ...
then your class should implement the Comparable interface, like this:
Expand|Select|Wrap|Line Numbers
  1. public class Vehicle implements Comparable
Nov 21 '07 #5
Jromero
16 New Member
Thank you so much is working now ....
Nov 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3753
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
4853
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 = Blank Page 3 = Print OK Page 4 = Blank
5
2223
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of "compile the questions" into a theoretical problem to see what people think should be done to solve it. Maybe it will be a worthwhile discussion, but more importantly maybe it will find out the very best way to sort this kind of problem out so that...
4
9285
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And this differences increases with the increase number of labels. The code is as follwods: Here rdr = OleDbDataReader Font is Times New Roman, 12pt
21
3205
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
6
4084
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if the user wants all pages will produce 3 pages. I want to offer the user the ability to select via the print dialog that only pages 1 and 2 of it are printed or possibly pages 1 and 3 but not 2. At the moment I can produce all three pages...
3
6642
by: matpublic | last post by:
Hello. I've searched the web to find a 'easy' way to output a PDF to a printer (probably non-Postscript 3). However I have had no success. I'm using reporting services to generate a three dimential byte array to represent the PDF documents, then I want to output the documents to the printer (depending on the document the printer trays may differ or duplex may be on/off).
48
4469
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
9
19806
by: mosullivan | last post by:
I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. #include <stdio.h> #define MAXWORD 101 void swap(int *i, int *j); int main(void) { int sort;
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.