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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to output the result of a compareTo method?

Ranch Hand
Posts: 167
2
So I was inspired by this little math problem I received today from Brilliant.com

A = 9^9 / 10^9

B = 9^10 / 10^10

Which is bigger?

So, like any good novice Java developer, I created the following Java class to solve it:

I know that compreTo is supposed to give me a -1, 0, or a 1. Which essentially means less than, equal, or greater than..... but how to print this out? Currently, all that shows in my console view is this:

a is: 0.387420489
b is: 0.3486784401

Also, is there a better way to compare "a" and "b"?

Sheriff
Posts: 4866
136
• 2
I'm confused.  Did you try System.out.println(compareTo(a, b)); ?  What happened?

Mark Richardson
Ranch Hand
Posts: 167
2

Knute Snortum wrote:I'm confused.  Did you try System.out.println(compareTo(a, b)); ?  What happened?

Ah, that works. I wrongly assumed that the following line would give me a textual representation of the result:

Bartender
Posts: 2166
46

Mark Richardson wrote:I know that compreTo is supposed to give me a -1, 0, or a 1.

This is not true. This method can return any int.
you should check whether the value is zero, or is positive, or is negative.
You shouldn't check whether the value is 1 (or -1).

Master Rancher
Posts: 2658
91

Mark Richardson wrote:So, like any good novice Java developer, I created the following Java class to solve it:
(...)
Also, is there a better way to compare "a" and "b"?

If X(p) = a^p / b^p, what is the relation between X(n) and X(n+1)? Can you now devise an easier way to compare the two? (if still needed).