Open Side Menu Go to the Top

11-29-2011 , 01:37 AM
I'm getting this "object is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

I'm completely lost as to why as I'm positive it's not a spelling error.

here is the code:

Code:
public class Fraction implements Comparable
{
	protected int numer;
	protected int denom;

	
	// ACCESSORS
	public int getNumer()
	{
		return numer;
	}
	public int getDenom()
	{
		return denom;
	}
	public String toString()
	{
		return numer + "/" + denom;
	}

	// MUTATORS
	public void setNumer( int n )
	{
		numer = n;
	}
	public void setDenom( int d )
	{
		if (d!=0)
			denom=d;
		else
		{
			throw new RuntimeException("Fatal Error: Fraction cannot be initialized with denominator of 0\n");
		}
	}

	// DEFAULT CONSTRUCTOR - no args passed in
	public Fraction(  )
	{
		this( 0, 1 ); // "this" means call a fellow constructor
	}

	// 1 arg CONSTRUCTOR - 1 arg passed in
	// assume user wants whole number
	public Fraction( int n )
	{
		this( n, 1 ); // "this" means call a fellow constructor

	}

	// FULL CONSTRUCTOR - an arg for each class data member
	public Fraction( int n, int d )
	{
		setNumer(n);
		setDenom(d);
	}

	// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
	public Fraction( Fraction other )
	{
		this( other.numer, other.denom ); // call my full C'Tor with other Fraction's data
	}

	public int compareTo(Fraction fract)
	{
		double temp1 = this.getNumer() * fract.getDenom();
		double temp2 = fract.getNumer() * this.getDenom();
		
		if ( (temp1 - temp2) > 0)
		{
			return 1;
		}
		else if ( (temp1 - temp2) < 0)
		{
			return -1;
		}
			
		return 0;
	}
	
	
	
}// EOF

Last edited by _dave_; 11-29-2011 at 02:10 AM.
Trouble implementing an interface in Java Quote
Trouble implementing an interface in Java
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Trouble implementing an interface in Java
11-29-2011 , 01:38 AM
I'm sorry, the code I copied in had indents. Idk why this doesn't.
Trouble implementing an interface in Java Quote
11-29-2011 , 02:11 AM
you need to use [ code] tags to preserve indentation, I added the to the OP. sorry I don't know any java to help though
Trouble implementing an interface in Java Quote
11-29-2011 , 05:52 AM
Your class implements interface Comparable so it should have the following method.
Code:
@Override
	public int compareTo(Object o) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
That is your problem , you just wrote code for public int compareTo(Fraction fract).
Trouble implementing an interface in Java Quote
11-29-2011 , 10:35 AM
gefrag is right. Fraction is an Object, but not all Objects are Fractions.
Trouble implementing an interface in Java Quote
11-29-2011 , 01:54 PM
When I change the method parameters, the methods getNumer and getDenom don't work for Object o.
Trouble implementing an interface in Java Quote
11-29-2011 , 03:24 PM
Coming at this from the other end, Comparable is a generic interface. Now you don't need to change anything or deal with casting to Fraction.

Code:
public class Fraction implements Comparable<Fraction> {
  ...
}
Trouble implementing an interface in Java Quote
11-29-2011 , 03:36 PM
By the way what IDE are you using ? for example Netbeans suggests possible fixes when you come up with an error.
Trouble implementing an interface in Java Quote
11-29-2011 , 03:38 PM
Thug, that worked, but why? I'd like to understand what it did. Also, when I declared the method as public int compareTo(Object o) and then used this statement o.getDenom and o.getNumer I got an error that said basically the compiler didn't recognize either statement.

FWIW when I looked up implementing the Comparable interface online, the website did the same thing I did. I think itwas:

public class Person implements Comparable
{
..
}

and the method header was like mine : public int compareTo(Person p)
Trouble implementing an interface in Java Quote
11-29-2011 , 04:40 PM
When you leave off the generic parameter Java treats it as a raw type. A raw type degrades to using Object as the parameter. Raw types exist because it provided support for the legacy Collection code that existed in Java before Generics were introduced.

Using raw types is frowned upon because it hides information and will lead to more code later. You also run in to fun issues like this:
[php]
List<Fraction> listFractions = new ArrayList<Fraction>():
List listRaw = listFractions;
listFractions = listRaw; //warning
//Type safety: The expression of type List needs unchecked conversion to conform to List<Fraction>
[/php]


o.getDenom() and o.getNumer() don't work because the compiler doesn't know that the instance is actually a Fraction instance. Based on the method signature o could also be a JTable instance (and you can only know that at run time). Because of this, you can only call methods defined by the variable's type. You can do a cast to gain access to the other methods.

[php]
public int compareTo(Object o) {
Fraction f = (Fraction)o;
System.out.println(f.getDenom());
}[/php]
However, this will throw an exception in that case that the instance is not actually a Fraction.

If the website you look at did not include the parameter in the class definition, and only implemented "public int compareTo(Person p)" than that website has an error that it needs to fix.

Last edited by TheIrishThug; 11-29-2011 at 04:48 PM.
Trouble implementing an interface in Java Quote
12-01-2011 , 02:15 PM
Quote:
Originally Posted by gefrag
By the way what IDE are you using ? for example Netbeans suggests possible fixes when you come up with an error.
Sorry to get back to this late, but I haven't been using an IDE. I have Eclipse, but my teacher is encouraging us not to use them. Once I feel more comfortable with the language I will.
Trouble implementing an interface in Java Quote
12-01-2011 , 02:19 PM
Quote:
Originally Posted by TheIrishThug
When you leave off the generic parameter Java treats it as a raw type. A raw type degrades to using Object as the parameter. Raw types exist because it provided support for the legacy Collection code that existed in Java before Generics were introduced.

Using raw types is frowned upon because it hides information and will lead to more code later. You also run in to fun issues like this:
[php]
List<Fraction> listFractions = new ArrayList<Fraction>():
List listRaw = listFractions;
listFractions = listRaw; //warning
//Type safety: The expression of type List needs unchecked conversion to conform to List<Fraction>
[/php]


o.getDenom() and o.getNumer() don't work because the compiler doesn't know that the instance is actually a Fraction instance. Based on the method signature o could also be a JTable instance (and you can only know that at run time). Because of this, you can only call methods defined by the variable's type. You can do a cast to gain access to the other methods.

[php]
public int compareTo(Object o) {
Fraction f = (Fraction)o;
System.out.println(f.getDenom());
}[/php]
However, this will throw an exception in that case that the instance is not actually a Fraction.

If the website you look at did not include the parameter in the class definition, and only implemented "public int compareTo(Person p)" than that website has an error that it needs to fix.

I'm not sure I understand the raw type explanation, but I'm going to look in to it.

As far as casting o as a fraction, do you mean that it won't work if I were to pass in say a string or some other type of object? Otherwise, what would be the "most" correct way to go about doing this problem? Specifically, gaining access to comparable for this "Fraction" class?

Thanks for your help btw.
Trouble implementing an interface in Java Quote
12-01-2011 , 03:02 PM
Code:
public int compareTo(Object o) {
  Fraction f = (Fraction)o;
  return 0;
}

...
new Fraction().compareTo(new Object());
This will throw a ClassCastException because an Object instance is not a Fraction, and thus can not be cast to Fraction.

This is the best way.
Quote:
Originally Posted by TheIrishThug
Coming at this from the other end, Comparable is a generic interface. Now you don't need to change anything or deal with casting to Fraction.

Code:
public class Fraction implements Comparable<Fraction> {
  ...
}
Trouble implementing an interface in Java Quote
Trouble implementing an interface in Java
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Trouble implementing an interface in Java

      
m