Problem with variable access

This is a discussion on Problem with variable access within the Tech Board forums, part of the Community Boards category; I get a syntax error for calling the constructor `PairOfDice()' in main. What's wrong here? (This isn't C++, but I ...

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Problem with variable access

    I get a syntax error for calling the constructor `PairOfDice()' in main. What's wrong here?

    (This isn't C++, but I believe the reasoning is the same--that's why I'm unloading this question on the C++ board.)

    Code:
     
    public class MiniMC3 {
    
            public static void main( String[] args ) {
    
                    PairOfDice theDice = new PairOfDice(); // Error Error Error
                    int rollCount = 0;
    
                    do {   
                            theDice.roll();
                            rollCount++;
                    } while( theDice.getDie1() + theDice.getDie2() != 2 );
    
                    System.out.println("You got snake eyes! It took " + rollCount
                            + " tries.");
    
            } // End main()
    
                    /** Monte-Carlo simulation.
                     *
                     *      Simulates rolling two dice until they
                     *      add to `sum'.
                     *
                     *  Precondition:  Total lies in range [2, 12]
                     *  Postcondition: Returns number of trials needed to           
                     *                 achieve `total'.
                     *
                     *  @param total Target total of two dice
                     *  @return Number of trials to achieve total.
                     */
    
            public class PairOfDice {
    
                    private int die1;
                    private int die2;
    
                    public PairOfDice() {
                         roll(); 
                    }
    
                    public void roll() {
                            // Roll the dice
                           die1 = (int)(Math.random()*6) + 1;
                           die2 = (int)(Math.random()*6) + 1;
                    }
    
                    // Getter methods for variables `val1' and `val2'
                    // (which are declared `private' here).
    
                    public int getDie1() {
                            return die1;
                    }
    
                    public int getDie2() {
                            return die2;
                    }
    
                    // Setter methods for variables `val1' and `val2'.
    
                    public void setDie1( int die ) {
                            die1 = die;
                    }
    
                    public void setDie2( int die ) {
                            die2 = die;
                    }
    
            } // End class PairOfDice()
    
    } // End class MiniMC3

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    It looks like Java code. Based on how you are using it, I daresay you want the nested class to be static: public static class PairOfDice. If not, you have to instantiate a MiniMC3 object, and then use it to instantiate a PairOfDice object. Or, perhaps you actually do not want PairOfDice to be a nested class.

    Moved to Tech board since it is about Java and has nothing to do with C++.
    Last edited by laserlight; 09-18-2007 at 10:54 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    If not, you have to instantiate a MiniMC3 object, and then use it to instantiate a PairOfDice object. Or, perhaps you actually do not want PairOfDice to be a nested class.
    So would the following be legal?
    Code:
    MiniMC3 myMC = new MiniMC3();
    PairOfDice theDice = (PairOfDice) myMC;
    Also, do you need to independent classes in separate Java files? I moved PairOfDice to a file of the same name, but with the disadvantage of now having MiniMC3 dependent on a file called PairOfDice.java (or, more appropriately, the associated .class file).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    So would the following be legal?
    For starters, try it out and see.

    do you need to independent classes in separate Java files?
    No, but that is probably good practice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    Resolved. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop with variable problem
    By Todd88 in forum C++ Programming
    Replies: 33
    Last Post: 04-06-2008, 07:36 PM
  2. Replies: 9
    Last Post: 11-23-2007, 11:28 AM
  3. Problem with structure with a float variable
    By j.sreejit in forum C Programming
    Replies: 9
    Last Post: 12-29-2006, 12:23 AM
  4. Replies: 2
    Last Post: 09-10-2002, 09:03 PM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 03:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21