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 4 of 4

Thread: Need to write a public instance method for a simple dice program

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to write a public instance method for a simple dice program

    Hi,

    Not sure where to start here or what detail you will need, as you can guess, I'm pretty darn new to all this! Ok, here goes. I am working through an assignment, in this particular section I need to write

    "...a public instance method which takes a single int argument and returns no value. The method should first check that its argument is in the range 1 to 6 (inclusive). If it isn't the method should display in a dialogue box a message, if the value of the argument is in the correct range the mehod should send one of the 'face' messages to the recevier, this. For example if the value of the argument is 1, the method should send the message face1() to the receiver etc.."

    So what I guess I need to do here is something along the lines of:

    if face is >= 6 or <=0
    display the message
    else
    send the message facex() to the receiver

    I've tried different ways to acheive this, but I'm getting no where fast.

    The latest way I have written this is:

     public int changeFace()
       {
            int n;
            if ((n >= 6) || (n <= 0))
             {
                OUDialog.alert("Argument to changeFace() message " +  (n) +" is out of range");
             }
            else
             {
                return this.changeFace(n);
             }
        }

    but when I try to compile in BlueJ I get an error: "changeFace() in Dice cannot be applied to (int)"

    I'm not sure how much detail is required, so I've tried to include what I think is relevant, but if there is anything else that is of use, please let me know

    Thanks!


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Need to write a public instance method for a simple dice program

    The method criteria is: "a public instance method which takes a single int argument and returns no value". Your method signature does not match the criteria. (Signature refers to the return type and number+type of arguments) As to what face1() refers to, it's hard to say, but the format would suggest a method. Some more information may help to clear that up.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to write a public instance method for a simple dice program

    Hi,

    This is a copy of the code for face1()

    /**
        * Sets the face of the receiver to 1 spot
        */
       public void face1()
       {
          this.getTopLeftSpot().setColour(OUColour.ORANGE);
          this.getTopMiddleSpot().setColour(OUColour.ORANGE);
          this.getTopRightSpot().setColour(OUColour.ORANGE);
          this.getMiddleSpot().setColour(OUColour.BLUE);
          this.getBottomLeftSpot().setColour(OUColour.ORANGE);
          this.getBottomMiddleSpot().setColour(OUColour.ORANGE);
          this.getBottomRightSpot().setColour(OUColour.ORANGE);
       }

    There is one these for each face of the dice, 1 - 6

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need to write a public instance method for a simple dice program

    Quote Originally Posted by akira25 View Post
    "...a public instance method which takes a single int argument and returns no value. The method should first check that its argument is in the range 1 to 6 (inclusive). If it isn't the method should display in a dialogue box a message, if the value of the argument is in the correct range the mehod should send one of the 'face' messages to the recevier, this. For example if the value of the argument is 1, the method should send the message face1() to the receiver etc.."
     public int changeFace()
       {
            int n;
            if ((n >= 6) || (n <= 0))
             {
                OUDialog.alert("Argument to changeFace() message " +  (n) +" is out of range");
             }
            else
             {
                return this.changeFace(n);
             }
        }
    Hello akira25!
    I think you should first make your changeFace() method match the criteria you are given (which does not as post #2 said). With the int signature your method should return an int value but your criteria want it to return no value. And also right now it takes no argument instead of an int argument that is supposed to.
    I would suggest you to read the javadoc for methods and a useful tutorials.

Similar Threads

  1. Why main(String args[]) method needs to be public?
    By rohan22 in forum Java Theory & Questions
    Replies: 11
    Last Post: December 7th, 2011, 11:41 PM
  2. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  3. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  4. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM
  5. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM