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
Am I right?
And please UseCodeTags (click it).
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).
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...
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 Gutkowski wrote:And, assuming it is the problem, then well spotted. (...)
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