/**
The Zipcode class builds a Zipcode object containing the geographic location data corresponding with the zipcode.
*/
public class Zipcode implements Comparable<Zipcode> {
/** variables to store data in the Zipcode object */
protected int Zipcode;
protected String Latitude;
protected String Longitude;
protected String City;
protected String State;
protected String County;
protected String ZipType;
/**
Constructor Zipcode builds a new Zipcode object with the 7 parameter values passed to it.
@param iZipcode String representation of postal zipcode.
@param iLatitude String representation of latitude.
@param iLongitude String representation of longitude.
@param iCity String representation of city.
@param iState String representation of state.
@param iCounty String representation of county.
@param iZipType String representation of zip code type.
*/
public Zipcode (String iZipcode, String iLatitude, String iLongitude, String iCity, String iState, String iCounty, String iZipType) {
Zipcode = Integer.parseInt(iZipcode);
Latitude = iLatitude;
Longitude = iLongitude;
City = iCity;
State = iState;
County = iCounty;
ZipType = iZipType;
}
public int getzip()
{
return Zipcode;
}
/**
Returns all of the Zipcode data: zipcode, latitude/longitude, city, county, state, and zipcode type.
@return Zipcode data.
*/
public String toString()
{
return (Zipcode+" "+Latitude+" "+Longitude+" "+City+" "+County+" "+State+" "+ZipType);
}
public int compareTo(Object zip)
{
if(((Zipcode)this).getzip()>=((Zipcode)zip).getzip())
return 1;
if(((Zipcode)this).getzip()==((Zipcode)zip).getzip())
return 0;
else
return -1;
}
}
is not abstract and does not override abstract method compareToI swear I have the method in my class
Page 1 of 1
6 Replies - 7573 Views - Last Post: 22 March 2010 - 04:58 PM
#1
is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:01 PM
First, I apologize for starting so many topics in such a short period of time. I swear this will be my last one. When I try to compile the class below, it gives me the error: Zipcode is not abstract and does not override abstract method compareTo(Zipcode), but I have defined compareTo. Does anyone know why this might be? Thank you in advance for your help. This is the last error and once I figure this out I can stop flooding the java forum with questions 
Replies To: is not abstract and does not override abstract method compareTo
#2
Re: is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:06 PM
you use Generics:
so you have to override the compareTo method as follows:
Comparable<Zipcode>
so you have to override the compareTo method as follows:
public int compareTo(Zipcode zip)
{
if(this.getzip()>= zip.getzip())
return 1;
if(this.getzip()==zip.getzip())
return 0;
else
return -1;
}
#3
Re: is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:08 PM
Since you're using generics, you shouldn't use Object as a parameter, you should use the generics object. Since you made the generic be Zipcode, so your code must also be:
public int compareTo(Zipcode zip)
{
if (this.getzip()>= zip.getzip())
return 1;
if (this.getzip()== zip.getzip())
return 0;
else
return -1;
}
#4
Re: is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:19 PM
japanir, on 22 March 2010 - 05:06 PM, said:
you use Generics:
so you have to override the compareTo method as follows:
Comparable<Zipcode>
so you have to override the compareTo method as follows:
public int compareTo(Zipcode zip)
{
if(this.getzip()>= zip.getzip())
return 1;
if(this.getzip()==zip.getzip())
return 0;
else
return -1;
}
Thank you. Your solution worked. However, I am still confused about generics in general. Isn't the idea that the object to be compared could be anything, not that it has to be a Zipcode, or T, whatever that may be? I'm just confused about why that worked. Thank you for the solution.
#5
Re: is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:24 PM
Because Comparable is an interface, you have to implement all of its methods. Hence, you must implement its only method: compareTo()
#6
Re: is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:32 PM
zim1985, on 22 March 2010 - 05:24 PM, said:
Because Comparable is an interface, you have to implement all of its methods. Hence, you must implement its only method: compareTo()
I understand that, but I don't understand why the object type within the parentheses had to be Zipcode. Normally I just make it Object and cast later. I understand that the point of generics is to be able to avoid unnecessary casting, but I don't understand why this restricts the type of object that I can pass to my compareTo.
#7
Re: is not abstract and does not override abstract method compareTo
Posted 22 March 2010 - 04:58 PM
The *point* of generics IS to restrict the type and to throw an error if that type is not met. This allows code to be far more robust and less bug-prone. Without generics, you could pass any object to compareTo() and the cast would would fail. With generics, the value is simply restricted to prevent such occurences.
Maybe you need to look at generics again...
http://java.sun.com/...rics/intro.html
Maybe you need to look at generics again...
http://java.sun.com/...rics/intro.html
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|