aspose file tools*
The moose likes Beginning Java and the fly likes differ by at most 10-6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "differ by at most 10-6" Watch "differ by at most 10-6" New topic
Author

differ by at most 10-6

Christian Staves
Greenhorn

Joined: Jul 07, 2010
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
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

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Jul 07, 2010
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18


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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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
 
wood burning stoves
 
subject: differ by at most 10-6