• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

differ by at most 10-6

 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic