This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

How do you write this. My professor hinted at using Math.abs, so would it be something like this and would i need an if statement to run it thru twice??

public boolean isSquare()
This returns true, if a and b differ by at most 10-6) and returns false otherwise.

Rob Prime wrote:1) 10^-6 isn't allowed in Java. Use Math.pow(10, -6) instead. . . .

But it is allowed. It means to take the bitwise XOR of the numbers 10 and -6. Probably not what the original poster intended however

Surely 1e-6 is a quicker way to express that exponential? And the == operator would return a boolean, which the compiler would notice and complain about.

Christian Staves
Greenhorn

Joined: Jul 07, 2010
Posts: 26

posted

0

Ok, so it runs and compiles and the tester works, do you guys see anything I messed up on ? Im pretty sure its correct, I mean it runs...

So after testings, my isSquare method is still broken. Did I not do it correctly?

a tester class (RectangleTester) that creates the following three rectangles.
1. a = 100.0 and b = 200.0
2. a = 100.0 and b = 100.0
3. a = 0.0 and b = 100.0
Could I just do Rectangle myRec1 = new Rectangle( 100.0, 200.0) would these values corrospond to a&b?

Its running for me, do you guys see anything wrong in my code.

You can use the notation "1e-6" which is just a numeric literal that means "10 to the power -6", or "0.000001". No need to call a method (Math.pow()).

Also, if (...) return true; else return false; is unnecessarily verbose - the expression already returns a boolean value, no need to check that boolean value in an if-statement and then return true or false. Just return the value of the expression directly.

Jesper Young wrote:You can use the notation "1e-6" which is just a numeric literal that means "10 to the power -6", or "0.000001". No need to call a method (Math.pow()).

I left the Math.pow because it may be easier to read for beginning programmers. And yes, I admit, initially I forgot all about the E way to declare constants