Mark Richardson

Ranch Hand

Posts: 99

1

posted 2 weeks ago

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"?

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"?

Mark Richardson

Ranch Hand

Posts: 99

1

posted 2 weeks ago

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).

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).

OCPJP 6, 7, 8, OCMJD 6

Piet Souris

Rancher

Posts: 1916

66

posted 2 weeks ago

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).

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).