Friday 15 June 2012

java - Having a little trouble with compareto and clone method -



java - Having a little trouble with compareto and clone method -

i having little problem implementing compareto , clone method in program, error says: "car not abstract , not override abstract method compareto(java.lang.object) in java.lang.comparable". help appreciated:)

this have far:

import java.util.*; public class auto implements comparable, cloneable { private string model; private string color; private int year; private int vin_number; private double price; public car(){} public car(string model, string color, int year, int vin_number, double price) { this.model = model; this.color = color; this.year = year; this.vin_number = vin_number; this.price = price; } public object clone() { // shallow re-create seek { homecoming super.clone(); } grab (clonenotsupportedexception e) { homecoming null; } } public string getmodel(){return model;} public string getcolor(){return color;} public int getyear(){return year;} public int getvinnumber(){return vin_number;} public double getprice(){return price;} public void setmodel(string newmodel){model = newmodel;} public void setcolor(string newcolor){color = newcolor;} public void setyear(int newyear){year = newyear;} public void setvinnumber(int newvinnumber){vin_number = newvinnumber;} public void setprice(double newprice){price = newprice;} public string tostring() { homecoming "model: " + model + " color: " + color + " year: " + year + " vin number: " + vin_number + " price: " + price; } public int compareto(car o) { if(this.getmodel().compareto(car.getmodel())) //gives 1 homecoming 1; if(car.getmodel().compareto(this.getmodel())) //gives -1 homecoming -1; else { homecoming 0; } } public static void main(string [] args) { } }

you must specify type beingness compared when implement comparable:

class auto implements comparable<car>, cloneable

raw types utilize object. you're using raw type, right now, compareto must declare object parameter, not car.

to prepare error, alter implements comparable implements comparable<car>

java

No comments:

Post a Comment