• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble comparing fractions  RSS feed

 
andrew cassato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I am trying to sort some fractions but i am getting an error with the compare method in the fraction class.


On the 9th line i get the error: non-static method compare(Fraction,Fraction) cannot be referenced from a static context.

I know the fraction class is correct and i understand basically why this isnt working but its just not coming together.

thanks in advance for any input.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message means the "compare" method needs to be called through an instance -- i.e., perhaps something like

A[minIndex].compare(A[smallestIndex])

I've shown it with only one argument because that's how the standard "compareTo()" method is written; yours might be different though.

If you show us declaration of your "compare()" method, we could tell you exactly what to do -- if this isn't enough info already.
 
andrew cassato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im a little confused by what you mean there... im using the standard fractions class, here is the compare method:

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There actually is no "standard Fractions class" -- you won't find a class by that name in the Java APIs. Perhaps your instructor supplied it, or a book?

In any case, you need an instance of the "Fractions" class to be able to call this method. "Calling it in a static context" means calling it just using the class name, as you're doing in your code. Only methods marked "static" can be called this way. That means at some point you have to do something like

Fractions f = new Fractions();

and then later when you call the method, it must look like

f.compare(A[minIndex],A[smallestIndex])

Does that make sense now?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although the responder is right, I'm amused by what appears to be a good instinct on the part of the original poster. The compare method is defined as a regular "instance method", not a static method. But it does not use any instance variable, its entire operation is completely independent of the object on which it is defined.

Based on what the method does, there is no reason why it couldn't be a static method, and if it were my code, I would make it one so it could be called without instancing anything. No instance is needed.

rc
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:Based on what the method does, there is no reason why it couldn't be a static method, and if it were my code, I would make it one so it could be called without instancing anything. No instance is needed.


Yeah, that thought did occur to me, but right now I'm betting that "Fractions" implements java.util.Comparator.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post code which will actually compile; you have, for example, written = instead of == somewhere. Better to use copy-and-paste for code, then you know you have posted exactly what you write.
 
andrew cassato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:There actually is no "standard Fractions class" -- you won't find a class by that name in the Java APIs. Perhaps your instructor supplied it, or a book?

In any case, you need an instance of the "Fractions" class to be able to call this method. "Calling it in a static context" means calling it just using the class name, as you're doing in your code. Only methods marked "static" can be called this way. That means at some point you have to do something like

Fractions f = new Fractions();

and then later when you call the method, it must look like

f.compare(A[minIndex],A[smallestIndex])

Does that make sense now?


Sorry for the long delay here, time is not coming cheap.

My prof. made it seem like the fractions class which he had us use is much more universal than what I am gathering. the class is here.

Sorry about the code not compiling, if i could make it compile i would be that much closer to finishing! i used copy and paste, would have taken forever to type it all out again...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!