Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Basic Netbeans questions

  1. #1
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Basic Netbeans questions

    There are some questions I have about computer science:

    a) When you assign a value larger than the max. possible value you can to an int. variable, it becomes a negative value. This doesn't make sense although it's correct because what really happens in Netbeans is you get an overflow error.

    b) Look at the following question:

    Which of the following boolean expressions is always true?


    A)
    10 <= x || !( x >= 10 )


    B)
    10 <= x && !( x >= 10 )

    C)
    y == x + y && x == x + y

    D)
    y == x + y || x == x + y

    The right answer is b, but how is that? How does the code work, and why are the other options incorrect? What do the standing equals signs do and what are some other instances they can be used in?

    c) Finally, consider this question:

    What is the value of x when this code is done executing?
    int x = 0;
    for(int i = 0; i < 10; i++){
    if(i == 5){
    continue;
    }
    x++;
    }

    The answer is 9, but what's do the unnecessary continue statement and the if statement do? Why does x equal 10 without them? Also, normally, what is the function of the continue statement and what are some examples of how it's used?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Basic Netbeans questions

    If these are questions about java programming, you should write a small test program to compile and execute all the code that you have questions about.
    For the expressions, put them inside of a System.out.println() statement. It will print either true or false.

    NOTE: Netbeans is a tool for editing and building programs in a computer language. It is not a language like java. There is nothing about these questions that requires the netbeans program.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Basic Netbeans questions

    1) You need to understand that the highest bit (most significant bit) of an integer is the sign bit. If it is set, the number is negative. For a byte,
    the maximum positive value is 127 or 01111111. If you increment that by 1 you get 1000000 which is equal to -128. And if you add 127 to -128 you get
    11111111 which is -1. And if you add 1 to -1 you get 00000001 + 11111111 = 00000000. Both 16, 32, and 64 bit values work the same way. Only their
    maximum and minimum values are different.

    I am not familiar with Netbeans but I wouldn't think it would throw an overflow. I have continually incremented ints in java so they "wrap around" many times with no ill effects.

    2. First, I disagree with your answer. To easily solve it (at least for me), you need to rewrite the expressions in a different form. For example,
    if x == x + y, then what does y equal?

    3. Continue simply means go to continue with the next iteration. For this, you need to sprinkle some print statements in the code to witness its behavior.

    Also, you thread subject is misleading. As you said these are CS related.

    Regards,
    Jim
    Last edited by jim829; 08-16-2015 at 03:41 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Basic Netbeans questions

    If you rewrite the expression '10 <= x' to 'x >= 10', answer A) becomes (x >= 10) || !(x >= 10) which is a Hamlet like expression (to be or not to be) and it is always true. So answer A) is always true; for question C): x is incremented each time the loop body is executed, except when the loop counter is equal to five. The loop body is executed ten times so x will be nine.

    kind regards,

    Jos
    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Basic Netbeans questions

    Thank you for the responses.

    I realized that a is the right answer for 2, not b.

    I will take more care next time to not write "Netbeans" when the question is not specific to Netbeans.

    I have another question:

    A short can store values in this range: -32,767 to +32,767; a byte, 0-255; an integer, -2,147,483,647 to +2,147,483,647; and a long, -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. Floats and doubles can store a virtually infinite range of values. I don't know what it means to write that an integer can store values in the range -2, 147, 483, 647 - does it mean an integer can store values from -2 to 647 or something else? In fact, an integer can store a value of 1000000000 - I experimented in Netbeans and there were no error messages up to this value and some slightly higher values.

    Another related question:

    What variable would you use to store the population of the world? My teacher said long, but in Netbeans, when you type long/ double 7,000,000,000 you get an error although double can store a virtually infinite range of values. And how is long correct considering its range of values?
    Last edited by Anne; 08-16-2015 at 05:48 PM.

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

    Default Re: Basic Netbeans questions

    Quote Originally Posted by Anne View Post
    A short can store values in this range: -32,767 to +32,767; a byte, 0-255; an integer, -2,147,483,647 to +2,147,483,647; and a long, -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
    Not quite. Some of your lower ranges are wrong. You should try printing them out using Byte.MIN_VALUE, Byte.MAX_VALUE, etc. Also, bytes are signed values so it is not 0 -255.

    I don't know what it means to write that an integer can store values in the range -2, 147, 483, 647 - does it mean an integer can store values from -2 to 647 or something else?
    I don't quite understand your question. A ranges is simply the minimum to the maximum that a particular numeric type can store.

    What variable would you use to store the population of the world? My teacher said long, but in Netbeans, when you type long/ double 7,000,000,000 you get an error although double can store a virtually infinite range of values. And how is long correct considering its range of values?
    The compiler assumes integer unless you suffix an L to the number. And omit the commas (you can use underscores in place of them)

    So 7_000_000_000L would be the way to specify 7 billion.

    I recommend you read up in two's complement arithmetic. And once you get that down, you can challenge yourself by seeing how floating point numbers are represented internally by reading IEEE 754.

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

Similar Threads

  1. Basic Questions
    By jmohandos304 in forum New To Java
    Replies: 13
    Last Post: 04-30-2014, 03:14 PM
  2. Some basic questions - Java
    By Fusion in forum New To Java
    Replies: 4
    Last Post: 04-01-2014, 11:54 PM
  3. Basic GUI questions
    By sunde887 in forum New To Java
    Replies: 6
    Last Post: 02-11-2011, 06:11 PM
  4. Basic Image Questions
    By DaRancor in forum AWT / Swing
    Replies: 6
    Last Post: 11-22-2009, 12:20 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
  •