[Chicago] Can anyone explain this behavior in Java?

Lewit, Douglas d-lewit at neiu.edu
Mon Mar 23 14:40:46 CET 2015


Hi Phillip,

The "off by 1" sounds like a great suggestion, but the catch is this.  My
1000,000 (or whatever number I enter) is *final*.  Therefore the variable's
value is not allowed to change during the program's execution.  The
variable's value shouldn't just magically change to 999,999, although that
would explain a lot.

I didn't say that *printf *and *format *didn't work!  I just said that they
didn't work for my specific output.  I've used printf and format before,
and yes, they work really well!  But something "funny" is happening with my
variables to cause this "off by 1" behavior that you're talking about.  I
will look into this further, and thanks for letting me know about it--or
reminding me of this problem.

Best,

Douglas.

P.S.  This isn't really homework.  It wasn't assigned.  It's just something
extra that I'm doing for my class.

On Mon, Mar 23, 2015 at 7:39 AM, Philip Doctor <diomedestydeus at gmail.com>
wrote:

> Doing your homework and learning how to code is a struggle every software
> professional needs to overcome.
>
> I won't be reading your source code, but a common problem among
> programmers is an "off by one" problem, where you started or stopped
> counting before you thought you did.  Imagine you had an off by 1 problem
> for your 1000000 and divided 775 by 999999 instead... .000775000775 hmmm
>  look familiar?  You should look for where you start/stop counting and
> iteration in your code for bugs.
>
> *System.out.format* but they don't work
>
> If this sounds harsh I apologize.  Yes, they work.  They work, and are
> being used extensively in industry daily due to their reliable nature of
> working.  When you're taking your first java class and encounter behavior
> you don't understand, it's an opportunity to learn something about java
> rather than blame java for not working.
>
> >   Well for this problem I don't want to start at 0; I want to start at
> 4, so it makes more sense to use some type of dictionary, right?
>
> choosing a datastructure in trivial code doesn't really matter.  In
> non-trivial code when you have a performance or memory goal, you can pick
> data structures based on their performance or memory usage (they differ
> greatly).  Finding java hash maps is a good first step, next in your
> research you can try reading about when to use a hashmap vs an array.  Most
> of the discussions will not revolve around API friendliness to the
> problem.  Although in the spirit of Spinal Tap ("this one goes to 11!") you
> can just say that the first spot in an array in 4 and make 4 your first
> entry.  Right?
>
> Best.
>
> On Mon, Mar 23, 2015 at 1:49 AM, Lewit, Douglas <d-lewit at neiu.edu> wrote:
>
>> Hey guys,
>>
>> I'm pretty impressed with Python's dictionary data structure, so I did
>> some research and found out that Java has something equivalent called a
>> "HashMap".  (Is that the same as a hash table or lookup table?  I guess it
>> is.)  I'm also interested in simulating games of chance, so I decided to
>> write a couple programs to simulate the game of rolling four dice.  (In
>> statistics textbooks they always talk about rolling two dice, but that gets
>> really boring after a while.  Why not three or four dice, right?)  If you
>> roll four dice and record the sums, the sums can range from 4 (so you
>> rolled all 1's) to 24 (which means you rolled all 6's).  The domain or
>> sample space for this problem thus includes all integers from 4 to 24,
>> inclusive.  But then you have to compute the probability values.  I decided
>> to compute empirical probabilities by creating a random object that
>> simulates throwing a die.  Pretty simple, right?
>>
>> I'm attaching two different but very closely related .java files.  In one
>> I just use an array to store the probabilities.  In the other I use the
>> Java version of a dictionary or "HashMap".  Arrays are great, but with an
>> array all the keys are sequentially ordered integers starting at 0.  Well
>> for this problem I don't want to start at 0; I want to start at 4, so it
>> makes more sense to use some type of dictionary, right?
>>
>> Everything compiles and runs just great!  That's not the problem.  The
>> problem is this.  If you look at my program that uses a HashMap, check out
>> the empirical probabilities.  The final digits of the decimal are just
>> screwed up!  For example, let's say that I run my simulation 1,000,000
>> times, and in 1,000,000 throws of four dice I get a sum of 4 775 times,
>> okay?  So the probability for rolling a 4 should be 0.000775, right?  When
>> I use an array, that's exactly what I get.  But with a HashMap I'm getting
>> 0.0007750000775.  ?????   What the hell is that?  I tried using
>> *System.out.printf* and *System.out.format* but they don't work!  I'm
>> really at a loss.  I just don't get it.  It's not a big tragedy and the
>> program still basically works, but I guess I'm disappointed because my
>> first HashMap program has a bug in it!  If anyone can shed light on this
>> bug, hey, then please enlighten me cause I can't figure it out.
>>
>> Take it easy and thanks for the suggestions.
>>
>> Best,
>>
>> Douglas Lewit
>>
>> P.S.  I can just hear someone out there saying, "Doug, throw Java away
>> and just stick with Python!"  While that is extremely tempting, right now
>> that's not an option.  I want to pass my Java course with at least a B!
>> :-)
>>
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150323/1edf139f/attachment.html>


More information about the Chicago mailing list