• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparable interface in java  RSS feed

 
atem frank
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
never mind i got it
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!