programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# differ by at most 10-6

Christian Staves
Greenhorn
Posts: 26
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 Spoor
Sheriff
Posts: 21135
87
1) 10^-6 isn't allowed in Java. Use Math.pow(10, -6) instead.

2) Math.abs takes a number; that == would turn it into a float.

3) you need to check if the difference is at most 10^-6. Difference means subtraction.

With these hints you should be able to work it out.

Campbell Ritchie
Marshal
Posts: 56578
172
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
Posts: 26

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.

Rob Spoor
Sheriff
Posts: 21135
87
Christian Staves wrote:

There is one small flaw in that logic. Your professor asked for at most, you are checking for exactly. Change the == into <=.

Christian Staves
Greenhorn
Posts: 26
Thank you all so much. It helps so much, when I can ask questions and work to resolve these things.

Cheers

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88

I'd write that like this:

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.

Rob Spoor
Sheriff
Posts: 21135
87
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