• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparable interface in java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a class Compare3 that provides a static method largest. Method largest should
take three Comparable parameters and return the largest of the three (so its return type will
also be Comparable). Recall that method compareTo is part of the Comparable interface,
so largest can use the compareTo method of its parameters to compare them.
(4) Write a class Comparisons whose main method tests your largest method above.
(a) First prompt the user for and read in three strings, use your largest method to find
the largest of the three strings, and print it out (It's easiest to put the call to largest
directly in the call to println.) Note that since largest is a static method, you will call it
through its class name, e.g.,
compare3.largest(val1, val2, val3) and
(b) Add code to also prompt the user for three integers and try to use your largest
method to find the largest of the three integers. Does this work? If it does, it's thanks to
autoboxing, which is Java's automatic conversion of ints to Integers.(10 Points)

how do i call this two classes in one program? i am having a had time calling in on program. any help will be great
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like like you have an assignment to do an someone else's solution and you can't run it.
Am I right?
And please UseCodeTags (click it).
 
atem frank
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nope, the was a similar one in class concerning comparable interface, but my problem is from the lab exercise i dont know how to call it differently. I know there's a code but i cannot figureit out. everything looks fine to me so far, just having error with regards to class.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

atem frank wrote:I know there's a code but i cannot figureit out. everything looks fine to me so far, just having error with regards to class.


No, there's not "code" to figure it out; there's your brain.

Suppose someone handed you three balls from a pool table. How would YOU (not Java) work out which ball has the largest number? Stop thinking in Java, and solve the problem.

And the first step: StopCoding (←click).

Winston
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, the method "largest" is impeccable, can't fault it, and it is beautiful in its generics.

So that's not the problem. Problem is that there are two public classes. If these are in the same file,
then you get an error.

And why would you call your two classes in one program? Isn't the class "Comparisons" meant to test
the "largest" method? So, what class are you supposed to run, you think?

So, one class should not be public, and the class to run must contain a "main" method.

Just make sure that this class-to-be-run does not have some spurious braces and other characters...

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:So that's not the problem. Problem is that there are two public classes. If these are in the same file,
then you get an error.


And, assuming it is the problem, then well spotted. We (and by that I mean, I) often spend too much time on issues other than the question.

@atem: Has that answered your question? If so, then your problem is solved. If not, then read the whole thread, because there is other useful information.

Winston
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:And, assuming it is the problem, then well spotted. (...)

Winston


Thanks, but too much credit! I happened to drop the code into NetBeans to test it, and it
gave me that error before I had time to take a breath. It looked to me that that method
was allright (if a is not the largest, then that largest must be either b or c, and so on).

That method does not scale very well, though, so I bet the next question in that course
will be to make a general version of the question

Greetings,
Piet
 
atem frank
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never mind i got it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic