Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Understanding the outputs of this specific code.

  1. #1
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Understanding the outputs of this specific code.

    I have a question regarding the outputs of this code:

    Java Code:
    import static java.lang.System.out;
    class A { // by default, extends Object
    protected int x = 0xF0;
    protected int z;
    public A() {
    z = fun( x );
    
    out.printf("%x ", z);
    }
    public int fun(int i) { return (i + 1); }
    public static int staticFun(int i) { return (i + 3); } 
    }
    class B extends A {
    protected int y = 0x11; 
    public B() {
    z = fun( z );
    out.printf("%x ", z);
    }
    public int fun(int i) { return (i + 2); } 
    public static int staticFun(int i) { return (i + 4); } 
    
    public static void main(String args[]) {
    int i = 0;
    A a = new B(); 
    out.print( a.fun( i ) + " "); 
    out.println( a.staticFun( i ) ); 
    B b = new B(); 
    out.print( b.fun( i ) + " "); 
    out.println( b.staticFun( i ) ); 
    }
    }
    "When run, it outputs eight values. Fill in the blanks with correct values in order." These were the answers:
    1.) f2
    2.) f4
    3.) 2
    4.) 3
    5.) same as in 1.)
    6.) same as in 2.)
    7.) 2
    8.) 4

    However I am preparing for my final, and I'm trying to figure out how the answers for 1 and 2 have "f" in front and how do I read the code or follow it without compiling it, to reach the same answers as above. Thanks.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding the outputs of this specific code.

    I haven't followed the logic for this but 'f' is a hex value equal to 15 decimal. Notice the print statements where you have a "%x". That means print the value in hex. So f2 in decimal would be 16*15 + 2 or 242. The variable x and the format specification of "%x". are unrelated.

    Regards,
    Jim
    Eat_Whatever likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding the outputs of this specific code.

    BTW, not certain who provided the code but it won't compile. Notice that line 6 is trying to redeclare
    a variable that was used in line 3. That's a no-no.

    Java Code:
    public static void main(String args[]) {
          int i = 0;
          A b = new B();
          out.print(b.fun(i) + " ");
          out.println(b.staticFun(i));
          B b = new B();
          out.print(b.fun(i) + " ");
          out.println(b.staticFun(i));
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Understanding the outputs of this specific code.

    It should have been "A a = new B();" As shown in the code provided.
    and the "b." should have been "a."
    Like so:
    Java Code:
    public static void main(String args[]) {
    int i = 0;
    A a = new B(); 
    out.print( a.fun( i ) + " "); 
    out.println( a.staticFun( i ) ); 
    B b = new B(); 
    out.print( b.fun( i ) + " "); 
    out.println( b.staticFun( i ) ); 
    }
    Last edited by Eat_Whatever; 12-12-2016 at 02:50 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding the outputs of this specific code.

    Now that is really strange. I thought I did a simple cut and paste. I must have changed it myself somehow. Mea culpa.

    Regards,
    Jim
    Eat_Whatever likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Understanding the outputs of this specific code.

    Also where does the number 16 come from? I understand that f is a hex which converted to decimal is 15 but where does 16 come from?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding the outputs of this specific code.

    Think about how it's done for base 10. Then substitute 16 for 10 and simply do the math to convert to decimal.

    Understanding the outputs of this specific code.-hex.jpg

    You can do this for any base b. And remember that b^0 equals 1.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Understanding the outputs of this specific code.

    hmm looks like I need to go watch a youtube video on this stuff. How does the answer come to f2? Like how would I go by determining the outcome from the code, without compilation. What line do I start and follow to know that my first output will be f2, etc. Sorry I am asking for to much, but I wanna do great on my final.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding the outputs of this specific code.

    Look at your code. The value of x is initialized as follows in line 3.
    Java Code:
    protected int x = 0xF0;  // the x says treat the number as hex.
    that is the same as initializing x to 240 in decimal.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Understanding the outputs of this specific code.

    Ah I see, since f to decimal is 240, which is going to be our "x". Do I head to line 10 afterwards?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding the outputs of this specific code.

    Quote Originally Posted by Eat_Whatever View Post
    Ah I see, since f to decimal is 240, which is going to be our "x". Do I head to line 10 afterwards?
    Not quite. F0 to decimal is 240. Just F to decimal is 15. The hex values are 0 thru 9 and A-F are 10 - 15.

    Regards,
    Jim
    Last edited by jim829; 12-12-2016 at 02:52 PM. Reason: s/is/to and s/to/is
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Understanding the outputs of this specific code.

    Oh yeah, totally forgot about the zero. I'ma try run down the code and see how the got their outputs to f2, f4, 2, 3 etc. Thanks a lot!

Similar Threads

  1. Help in Understanding the code
    By CodeX Pro in forum New To Java
    Replies: 17
    Last Post: 05-19-2013, 07:32 AM
  2. Understanding Problem in Code !
    By Jack_Tauson_Sr in forum New To Java
    Replies: 3
    Last Post: 03-25-2013, 02:20 AM
  3. Need help understanding steganography code
    By nrott in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 11:20 AM
  4. same code different outputs
    By nikhil_me in forum New To Java
    Replies: 4
    Last Post: 01-20-2012, 07:35 AM
  5. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 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
  •