Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Interface problem please help!!

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Interface problem please help!!

    Hi....

    I have 2 files: Interface 'Car' and Class 'MyCar'

    I have never worked with interfaces before but I am getting the error:
    MyCar.java:6: MyCar is not abstract and does not override abstract method compareTo(Car) in java.lang.Comparable

    Please help!!
    /**
     * Class to represent cars
     * @author 
     * @version ONE 05/04/2010
     */
    public class MyCar implements Car
    {
       public String getModel()
    	{
    	 return model;
    	}
     
    	public String getRegNo()
    	{
    	return regNo;
    	}
     
    	public int getYear()
    	{
    	return year;
    	}    
     
       public int getPrice()
    	{
    	return price;
    	}    
     
       public void setPrice(int priceNew)
    	{
    	price=priceNew;
    	}
     
       public int getCompCount()
    	{
    	return 1;
    	}
     
       public boolean match(String model, int year, int price)
       {
    	if(thisModel == model && thisYear == year && thisPrice == price)
    		{ 
    			return true;
    		}
    	} 
     
       public String toString()
    	{
    	String carString = ("Price: " + price + 
    								" Year : " + year + " Reg Number : " + 
    								regNo + " Registration : " + model); 
     
    	return carString;
    	}
    }





    /**
     * Interface for a class to represent cars.
     * @
     * @version 1.4 - 15/1/10
     */
    public interface Car extends Comparable<Car>
    {
        /**
         * Get the car's model description
         * @return  the model
         */
        public String getModel();
     
        /**
         * Get the car's registration number
         * @return  the regNo
         */
        public String getRegNo();
     
        /**
         * Get the car's year of registration
         * @return  the year
         */
        public int getYear();
     
        /**
         * Get the car's price
         * @return  the price
         */
        public int getPrice();
     
        /**
         * Set the car's price
         * @param price  the price
         */
        public void setPrice(int price);
     
        /**
         * Get & reset the comparison counter value
         * @return  the count (before resetting to zero)
         */
        public int getCompCount();
     
        /**
         * Check for "near match" (see assignment brief for details)
         * @param model  ignore if null/empty, else match as substring
         * @param year   ignore if <= 0, else match to within 1 yr
         * @param price  ignore if <= 0, else match to within 10%
         * @return  true if "near match" as above, else false
         */
        public boolean match(String model, int year, int price);
     
        /**
         * Create a string with price, year, regNo & model description
         * concatenated in that order, separated by single tabs
         * @return  a printable string as above 
         */
        public String toString();
    }
    Last edited by Json; April 6th, 2010 at 12:06 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Interface problem please help!!

    Its basically saying that you need to implement the comparTo method since your interface Car is actually extending another interface called Comparable.

    For instance this method could look like this.

        @Override
        public int compareTo(Car otherCar) {
            // Do some magic in here to compare the cars, maybe return 0 only if the model or the reg number are equal.
            return 0;
        }

    // Json

Similar Threads

  1. Interface and Extends problem
    By DannyGT in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2011, 02:01 PM
  2. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 11:36 PM
  3. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM
  4. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 03:49 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 05:38 PM