Results 1 to 11 of 11
  1. #1
    JoshuaNathan is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default What's the difference in output of these two statements?

    Hi,
    I'm really quite new to java and have had a little bit of a problem understanding the difference in output (if any) between these two statements.

    public void flip ()
    {
    face = ((int)(Math.random() * 6)) + 1;
    }


    and

    public void flip ()
    {
    face = (int) (Math.random() * 6) + 1;
    }

    As can be seen all that differs is a single set of brackets however, what difference does this make to the output?

    Thank you.
    Josh

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JoshuaNathan View Post
    I'm really quite new to java and have had a little bit of a problem understanding the difference in output (if any) between these two statements.

    public void flip ()
    {
    face = ((int)(Math.random() * 6)) + 1;
    }


    and

    public void flip ()
    {
    face = (int) (Math.random() * 6) + 1;
    }

    As can be seen all that differs is a single set of brackets however, what difference does this make to the output?
    Edit: sorry, I didn't look carefully enough. In both expressions the expression Math.random()*6 is cast to an int value and one is added to the result.

    kind regards,

    Jos
    Last edited by JosAH; 01-05-2011 at 03:36 PM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JoshuaNathan is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Thank you.

    Thank you for the information, it makes sense looking at it in hind-sight.
    One more question if you don't mind answering it.
    As part of the test question i've called the dice class in another class that rolls it (which is all fine). However, we were told to do so editing an existing program which i have done ( and it all works well). Whilst editing it i noticed that they code it as follows:

    public boolean isTwo ()
    {
    return (face == 2);
    }

    public boolean isThree ()
    {
    return (face == 3);
    }

    Is there an easier way of coding this so that instead of having to write out each individual number as shown above i could for example have a 20 sided dice which could be cast and give a string value without having to write out each method for each number?

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JoshuaNathan View Post
    Hi,
    I'm really quite new to java and have had a little bit of a problem understanding the difference in output (if any) between these two statements.

    public void flip ()
    {
    face = ((int)(Math.random() * 6)) + 1;
    }


    and

    public void flip ()
    {
    face = (int) (Math.random() * 6) + 1;
    }

    As can be seen all that differs is a single set of brackets however, what difference does this make to the output?

    Thank you.
    Josh
    why didn't you try them out? first get rid of the Maths.random(), insert a number and see the results.

    Java Code:
    public class Flip {
        public static void main(String[] args){
            flip() ;
            flip1();
        }
        public static void flip1 () {
            int face = ((int)(0.5 * 6)) + 1;
            System.out.println("Flip1: " + face);
        }
        public static void flip ()
        {
            int face = (int) ( 0.5 * 6) + 1;
            System.out.println( "Flip: " + face );
        }
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    They'll give the same result.
    Both have the following in:
    Java Code:
    (int)(Math.random() * 6);
    Which does the cast after the random has been multiplied by 6.

    The only difference is that there's a set of extra brackets around that bit, which will probably be ignored by the compiler.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by JoshuaNathan View Post
    Thank you for the information, it makes sense looking at it in hind-sight.
    One more question if you don't mind answering it.
    As part of the test question i've called the dice class in another class that rolls it (which is all fine). However, we were told to do so editing an existing program which i have done ( and it all works well). Whilst editing it i noticed that they code it as follows:

    public boolean isTwo ()
    {
    return (face == 2);
    }

    public boolean isThree ()
    {
    return (face == 3);
    }

    Is there an easier way of coding this so that instead of having to write out each individual number as shown above i could for example have a 20 sided dice which could be cast and give a string value without having to write out each method for each number?
    Depends what you want to do with those methods. The devil is in the details, but a simple option would be an isNumber(int) method, which simply checks the parameter against the face. But I have no idea whether that would be good or bad in your particular case.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    The only difference is that there's a set of extra brackets around that bit, which will probably be ignored by the compiler.
    Effectively yes, those parentheses are ignored. Technically no, that stupid compiler generates a ParenthesizedTree for every pair of parentheses around expressions in its AST; I tried to make it crash because of this but failed so far ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Yes, but is it ignored in the final byte code?
    I really don't care what it does while it's compiling...the longer it takes the more chance I have for a cup of tea.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Yes, but is it ignored in the final byte code?
    I really don't care what it does while it's compiling...the longer it takes the more chance I have for a cup of tea.
    It is completely gone in the final byte code; I do care what's happening during the compilation of it all though ;-) b.t.w. if you spread your expression over several lines and generate debug code the final byte code does differ; i.e. if you do this:

    int a= (
    1+2
    );

    byte code will differ (because of those line directives).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    That's debugging...that doesn't count.
    ;)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    That's debugging...that doesn't count.
    ;)
    I want to earn a penny from every person who compiles their classes for distribution with debug code in it ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need help with the output statements
    By jayti in forum New To Java
    Replies: 9
    Last Post: 11-26-2010, 04:06 PM
  2. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  3. if else if statements
    By Allspark in forum New To Java
    Replies: 9
    Last Post: 09-28-2010, 06:50 PM
  4. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •