• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Trouble comparing fractions

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic