• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to output the result of a compareTo method?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused.  Did you try System.out.println(compareTo(a, b)); ?  What happened?
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!