Results 1 to 12 of 12
  1. #1
    zander is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Cannot work out what the missing line of code should be [???]

    //The answer must have balanced parentesis and not use "Integer" "Float" "Double" and 0...9

    public class Exercise{

    public static void main(String [] arg){
    [???]
    assert (Integer.MAX_VALUE==i);
    }
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Cannot work out what the missing line of code should be [???]

    What is the objective of this excercise... to make it not crash?
    Get in the habit of using standard Java naming conventions!

  3. #3
    zander is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Cannot work out what the missing line of code should be [???]

    The objective is to make the assert line true

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Cannot work out what the missing line of code should be [???]

    Ok, you need to think about this. If I want to assert the following is true:

    "You are wearing a red jacket."

    Then what needs to happen? Same thing with the above problem.


    And here is some information that may be useful.

    1. Depending on the IDE, assertions may be off by default (and you may not know it).
    2. If command line, use java -ea to execute your program (that stands for enable assertions).
    3. True assertions don't really do anything exciting so put in a false one to make certain that assertions are
    really enabled.

    The above assumes you need to compile and run this program.

    Regards,
    Jim
    Last edited by jim829; 03-12-2014 at 04:39 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    zander is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Cannot work out what the missing line of code should be [???]

    You seem to have mis-understood the problem. I am trying to find out a line of code that replaces the ??? that will make i==Integer.MAX_VALUE, thus making the assert line return true.
    I have a feeling that the code I need to put in has something to do with using a Long to cause a wrap around when converted to an integer than will result in the max value of an integer.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Cannot work out what the missing line of code should be [???]

    I did not misunderstand the problem. You misunderstood my answer. Do you know what the value Integer.MAX_VALUE is? Look at the following
    problem:

    Java Code:
    //[???]
    if (i == 20) {
       System.out.println("Foo");
    }
    Do you know what to put in place of the ??? to make it print Foo? I hope so. What about the following:
    Java Code:
    //[???]
    if (i == Integer.MAX_VALUE) {
       System.out.println("Foo");
    }
    I have done everything except give you the answer.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,656
    Rep Power
    5

    Default Re: Cannot work out what the missing line of code should be [???]

    Quote Originally Posted by jim829 View Post
    I have done everything except give you the answer.
    Ya lousy bum.

    The most warmhearted and kindest regards,
    Erik
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    904
    Rep Power
    2

    Default Re: Cannot work out what the missing line of code should be [???]

    I think the problem is he is not allowed to use Integer, Float, Double and 0...9. That is the hurdle he has to take.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

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

    Default Re: Cannot work out what the missing line of code should be [???]

    I can't resist:

    Java Code:
    		int x= Byte.SIZE, y= x, z= x;
    		x/= x; y--;
    		int i= (x<<y)<<z<<z<<z;
    		i= ~i;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Cannot work out what the missing line of code should be [???]

    @OP

    My sincere apologies. Surfman corrected me. Apparently I totally glossed over the requirements. Now that I am awake, I should be able to offer something useful.

    You want to create the value where all but the highest bit (the sign bit) are 1's. So you need to
    first create a value of all 1's. Easy, just take a default initialized value (which is zero) and complement it. Then
    take another value and increment it to 1. Then do an unsigned shift (>>>) of your first value by your second.

    You could also use Long.MAX_VALUE and right shift it 32 bits. You need to figure out how to get 32 without using 0..9.
    You can use a similar strategy as above.

    More information than I would normally give but I think I owe you that.

    Regards,
    Jim
    Last edited by jim829; 03-12-2014 at 03:23 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Cannot work out what the missing line of code should be [???]

    He's not allowed to use Integer, but couldn't he use int? The assignment looks too easy to me. I mean, the OP already wrote the answer in one of his comments.
    Get in the habit of using standard Java naming conventions!

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Cannot work out what the missing line of code should be [???]

    I think he can use any primitive and even Long. I guess the exercise is to derive the max value based on bit manipulation. Using Integer lets him use Integer.MAX_VALUE again. The problem with Double is probably some method that is taboo. And he can't use any digits either. The real requirements of the assignment are still not all that clear to me.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Whats missing in this code?
    By Patricia6946 in forum New To Java
    Replies: 3
    Last Post: 02-14-2013, 09:36 AM
  2. Missing code
    By LukasHopkins in forum New To Java
    Replies: 2
    Last Post: 09-09-2012, 09:12 AM
  3. Replies: 10
    Last Post: 01-27-2012, 05:24 PM
  4. Replies: 2
    Last Post: 03-03-2011, 06:55 PM
  5. Constructors pls explan the code line by line in comments
    By vibaviattigala in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 04:03 AM

Posting Permissions

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