Javahackercoding 3: With a Vengeance

This entry is part 3 of 8 in the series Java Class

This week has been mad. Both my girls have had colds and one has been teething1, so finding time to study between looking after them (and myself to make sure I don’t get it too) has been difficult. I’ve read my chapters, but I’m not one ahead like I wanted to be. And I’ve only done two of the five exercises for this chapter. It’s my first real OO introduction too. Until now I’ve understood “int”, “double” etc. but now we’re getting in to what has been behind the veil for me when I’ve tried to program before — how you create classes, and call them from other classes.

So I’ve built my Rectangle class and it looks like this:

Now feel free at this point to tell me where if I’ve gone wrong there. At line 12 – 14 the book tells me to do this.length = length; and so on, but I wanted to try it the other way here just to wrap my head around what was happening. So that’s all well and good and we go on to make a test class, RectangleTest:

Now that seems fine to me too. Maybe you can point out something I’ve missed, because when I run the thing (which works exactly as I’d planned btw) I get this output from NetBeans:

run:
Small rectangle: A=6.0 P=10.0
Default rectangle: A=1.0 P=4.0
Large rectangle: A=624.24 P=102.0
Rectangle not written as double: A=24.0 P=20.0
Stupidly proportioned rectangle: A=1280.3200000000002 P=806.6
BUILD SUCCESSFUL (total time: 0 seconds)

Can you spot the weirdness? Why would the Area be returned as “1280.3200000000002″? I added a method to just display the area alone2, and it’s definitely being calculated as 1280.3200000000002, which is just strange, and I want to know if it’s something I’ve done incorrectly, or a quirk of Java/NetBeans.

So that’s where I am. I’m gonna’ read up on the next chapter before class tonight. And if I get time, I’ll do some more of the exercises too. So if I’m not on the right track, please tell me before I go to far!

Footnotes
  1. I’ll let you guess which 
  2. public void displayArea() {System.out.println(getArea() );} 
  • shaunau
  • shaunau
  • Ben
    The most common storage for floating-point values in programming languages - IEEE singles and doubles - does not have exact representations for most decimal fractions.

    The reason is that they store values in binary floating-point format, rather than decimal floating-point format. The only fractional values which can be represented exactly are those which are sums of negative powers of two. Numbers like:

    * 0.5 (2^-1)
    * 0.125 (2^-3)
    * 0.625 (2^-1 + 2^-3)

    Etc.

    What you are seeing is the fact that representations of numbers like 0.96 are not exactly representable, because they are not expressible as a sum of negative powers of two. Thus, when printed out with full precision as a decimal fraction, they won't match the original value.


    *Borrowed from Stack Overflow
  • I guess the thing that baffled me was that you're told to expect weird stuff like that when you're using division etc. but this was just a simple multiplication. Testing it on my computer's calculator and Google both gave the correct result - how do they correct for it?

    And so is it Java you can't trust for simple multiplication, or something wrong with NetBeans? Is there a way to account for it and stop it happening?
blog comments powered by Disqus