• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Constructor create instance, and that constructor create another instance

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

The task I'm attempting to solve is part of a school assignment. I've already solved it one way - yet that solution is rather ugly - so I'm attempting to solve it another way.

The task requires me to create a new class PairOfDice that creates two Dice objects, rolls them, checks if they're a pair, and then writes the result. I'm trying to have the PairOfDice class create two Dice classes that roll themselves, save their value to themselves, and I then have the PairOfDice class check if they rolled a pair, and lastly write out the result. Yet when I attempt to have the PairOfDice class create two Dice objects, they are created with a wrong number of sides (value), yet I don't understand why.

I've attempted to search this forum, and the Internet, yet I still don't understand what the error is - would you please help me?

Dice class:


PairOfDice class:


PairOfDiceTestRun class:



/Regards,
Nils

EDIT: I've done some debugging - when I run:


dice01 gets the value:
dice01 Dice(6, 2) Dice

which I assume has something to do with toString - does it automatically rewrite dice01's value when it's created?
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what do you see from running this and what do you expect to see?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just seen your edit.
Why are you expecting something other than a 6 for the number of sides?
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, welcome to the ranch...

Hint: What is the difference between declaring an instance variable versus declaring a local variable? And what do you thinks happens when you do both?

Henry
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, and now I see why I didn't spot that, as I would have expected a NullPointerException...except the code that populates the int[] isn't actually called.
 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dave: I think it displays what the Dice rolled - so in this case 2.

@Henry: local variables don't get default values. Instance variables get default values. If they're created at the same time - would the instance variable then overwrite the local variabel?

I just also found another error in the code: the "nsides" variabel in PairOfDice should be "nSides".

Also, thanks for the welcome .
 
Marshal
Posts: 77157
370
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I suggest you avoid Math.random(). I know the Java® Tutorial say it is more convenient than a Random instance, but I am not convinced. Using the nextInt method is more robust than the (int) cast.You should mark all method called from the constructor private or final; there is some discussion about that problem here.

Never write == true or == false. Not
if (b == true) ... but if (b) ...
Not
if (b == false) ... but if (!b) ...
Using == is error‑prone; we see people who write = by mistake.
Don't write
if (...) return true; else return false;
You should writeYou can simplify the method which returns the arrayNow you no longer need the array field at all.

What you are getting back from the printing call is determined by the toString method which you wrote. It is confusing because you don't say what the numbers mean. If you wrote this as your toString method, it would be easier to understand:-That will give a very similar output to
"A " + nsides + "-sided die showing " + value + "."
 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the extensive help!
 
Campbell Ritchie
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Our pleasure
 
reply
    Bookmark Topic Watch Topic
  • New Topic