Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Confusing Problem with some Java code from a book - deals with Classes and Constructors

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Stamford, CT
    Posts
    4
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Confusing Problem with some Java code from a book - deals with Classes and Constructors

    Hello!

    I have been trying to figure out exactly what is wrong with the code I copied from an ebook. I have tried everything I can think of and haven't got a clue!
    It has to do with my Class Constructor and it isn't making any sense to me because it is just another section of the code where a variable is re-used when it's
    public throughout the whole Class.

     
    import java.math.*;
    import java.util.*;
     
    public class PairOfDice {
     
    	public int die1; // Number showing on the first die.
    	public int die2; // Number showing on the second die .
     
    	public PairOfDice() {
    		// Constructor. Rolls the dice, so that they initially
    		// show some random values .
    		roll(); //Call the roll() method to roll the dice.
    }
    	public PairOfDice(int val1, int val2) {
    		// Constructor. Creates a pair of dice that
    		// are initially showing the values val1 and val2.
    		die1 = val1; // Assign specified values
    		die2 = val2; // to the instance variables.
    		}
     
    	public void roll() {
    		// Roll the dice by setting each of the dice to be
    		// a random number between 1 and 6.
    		die1 = (int)(Math.random()∗6) + 1; //These variables are giving me 
    		die2 = (int)(Math.random()∗6) + 1; // the following error below
    //Multiple markers at this line
    //	- Syntax error on token "Invalid Character", invalid 
    //	 AssignmentOperator
    //	- The left-hand side of an assignment must be a variable
     
     
     } // end class Pair Of Dice package;
    }
     
    public class RollTwoPair {
     
    	/**
    	 * @param args
    	 */
     
    	public static void main(String[] args) {
     
    		PairOfDice firstDice; // Refers to the first pair of dice.
    		firstDice = new PairOfDice();
     
    		PairOfDice secondDice; // Refers to the second pair of dice.
    		secondDice = new PairOfDice();
     
    		int countRolls; // Counts how many times the two pairs of
    		// dice have been rolled.
     
    		int total1; // Total showing on first pair of dice.
     
    		int total2; // Total showing on second pair of dice.
     
    		countRolls = 0;
     
    		do { // Roll the two pairs of dice until totals are the same.
     
    			firstDice.roll(); // Roll the first pair of dice.
    			total1 = firstDice.die1 + firstDice.die2; // Get total.
    			System.out.println( " F i r s t pair comes up " + total1);
    			secondDice.roll(); // Roll the second pair of dice.
    			total2 = secondDice.die1 + secondDice.die2; // Get total.
    			System.out.println( " Second pair comes up " + total2);
    			countRolls++; // Count this roll .
    			System.out.println(); // Blank line.
    		} while (total1 != total2);
     
    		System.out.println( " It took " + countRolls + " rolls until the totals were the same. " );
    	} // end main ()
      } // end class RollTwoPairs.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    what is wrong with the code
    Please explain. If there are error messages, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Stamford, CT
    Posts
    4
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    Quote Originally Posted by Norm View Post
    Please explain. If there are error messages, copy the full text and paste it here.
    Multiple markers at this line
    - Syntax error on token "Invalid Character", invalid
    AssignmentOperator
    - The left-hand side of an assignment must be a variable

    Sorry Norm I posted the error message in the code in the PairOfDice section of code.
    Please let me know if I posted incorrectly. Thank you for such a quick response!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    Please post the FULL TEXT of the error messages. What you posted looks like a summary vs the full message.
    The message should show the source with a ^ under the location of the error.
    Here is a sample of an error message from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    Stamford, CT
    Posts
    4
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    OK! I finally got it to work but I don't understand why it made a difference.

    I changed the way the public integers were calculated in the PairOfDice section to:

    die1 = (int) (6 * Math.random()) + 1;
    die2 = (int) (6 * Math.random()) + 1;

    Instead of:

    die1 = (int)(Math.random()∗6) + 1;
    die2 = (int)(Math.random()∗6) + 1;

    I have no idea why it didn't like the other way as it is almost exactly the same.

    Thank you Norm for your suggestions. I guess it's just one of those compiler problems that seem to crop up and bite you when you least expect it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    Look at the code in post#1. Could your keyboard have entered a different character for the *?
    The forum's formatting code has put in &# 8727 (no space) where you must have thought you had a *
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Location
    Stamford, CT
    Posts
    4
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    Quote Originally Posted by Norm View Post
    Look at the code in post#1. Could your keyboard have entered a different character for the *?
    The forum's formatting code has put in &# 8727 (no space) where you must have thought you had a *
    The coding in the post is an HTML version of the * sign. I just looked it up. I am guessing that the software for
    this site got confused when looking at all of it crunched up without any space. As I intimated previously every new
    version of a compiler has its own peculiarities.

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Confusing Problem with some Java code from a book - deals with Classes and Constructors

    8727 is the unicode character 'asterisk operator'. Java multiplication uses the ASCII character corresponding to unicode 0042.

    Visual appearance and the unicode name notwithstanding they are different characters.

Similar Threads

  1. Peice of code confusing
    By BLUEJ in forum Java Theory & Questions
    Replies: 2
    Last Post: February 18th, 2013, 05:48 PM
  2. [SOLVED] Basic help with Constructors and Classes
    By Harrald in forum What's Wrong With My Code?
    Replies: 27
    Last Post: August 2nd, 2012, 07:15 AM
  3. Replies: 3
    Last Post: July 8th, 2012, 03:44 PM
  4. Need help with classes and constructors!!
    By rayp4993 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 14th, 2011, 01:02 PM
  5. Problem to organize my code in classes
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 21st, 2010, 12:13 PM