Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    My professor is a bit ambiguous. Need help with a simple problem

    Te book isn't very helpful with this situation, and to me, this seems like basic math. Before I send it in to him, I just want to know if this snippet of code really is as easy as it looks.

    1 int number1;

    2 int number2;

    3 int result;

    4

    5 number1 = 5 * ( 4 + 6 );

    6 number2 = 2 * 2;

    7 result = number1 / number2;

    Answer: number1 =

    number2 =

    result =


    My answers are:

    number1 = 50

    number2 = 4

    result = 12.5


    The second piece would be this: find one logic error, and one syntax error

    1 int number1;

    2 int number2;

    3 int result;

    4

    5 number1 = (4 * 6 - 4) / (10 % 4 – 2) ;

    6 number2 = (16 \ 3) - 2 * 6 + 1;

    7 result = number1 - number2;

    My answer:

    Syntax error is the "%"

    Logic error is the "\"



    This is a very basic java class, and for some reason, it's GUI design driven... which seems a little bass ackwards, but oh well.

    Thanks in advance!
    Last edited by M_Coupe; 11-12-2008 at 06:20 AM.

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    1) This is a javascript forum which is an entirely different language with an entirely different purpose and environment. Only things the same are vague similarities in syntax and object structure (however JS isn't even technically object oriented, it's prototype driven, but that's a different discussion).

    2) Next time place your code in [ code][ /code] tags and don't number your lines.

    3) You can combine your variable declarations to:
    Code:
    int number1,number2,result;
    4) Your result will not equal 12.5 because result is an int, not a double. I forget whether that throws an error or if it floors the value, you'll want to check that out yourself.

    5) "\" is the syntax error, not the logic error. Your logic error is in this line:
    Code:
    number1 = (4 * 6 - 4) / (10 % 4 – 2) ;
    The red evaluates to 0 which most compilers won't detect when you compile your code, that's dangerous because it causes a runtime error. Well not dangerous, just bad coding.

    6) Modulus (%) is a legitimate Java operator.

    7) I'm not doing your homework again for you, that is unless you e-mail me the questions and paypal some cash
    Last edited by itsallkizza; 11-12-2008 at 07:00 AM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by M_Coupe View Post
    Te book isn't very helpful with this situation, and to me, this seems like basic math. Before I send it in to him, I just want to know if this snippet of code really is as easy as it looks.
    You'd think he would have mentioned that JavaScript and Java are different languages
    find one logic error, and one syntax error

    1 int number1;

    2 int number2;

    3 int result;

    4

    5 number1 = (4 * 6 - 4) / (10 % 4 – 2) ;

    6 number2 = (16 \ 3) - 2 * 6 + 1;

    7 result = number1 - number2;

    My answer:

    Syntax error is the "%"

    Logic error is the "\"
    You're wrong - why do you think '%' is a syntax error? Consult your list of mathematical operators and you should see the problem in executing line 5.

    '\' isn't an operator so that's a syntax error.

  • #4
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by itsallkizza View Post
    1) This is a javascript forum which is an entirely different language with an entirely different purpose and environment. Only things the same are vague similarities in syntax and object structure (however JS isn't even technically object oriented, it's prototype driven, but that's a different discussion).

    2) Next time place your code in [ code][ /code] tags and don't number your lines.

    3) You can combine your variable declarations to:
    Code:
    int number1,number2,result;
    4) Your result will not equal 12.5 because result is an int, not a double. I forget whether that throws an error or if it floors the value, you'll want to check that out yourself.

    5) "\" is the syntax error, not the logic error. Your logic error is in this line:
    Code:
    number1 = (4 * 6 - 4) / (10 % 4 – 2) ;
    The red evaluates to 0 which most compilers won't detect when you compile your code, that's dangerous because it causes a runtime error. Well not dangerous, just bad coding.

    6) Modulus (%) is a legitimate Java operator.

    7) I'm not doing your homework again for you, that is unless you e-mail me the questions and paypal some cash
    Thanks for the info- and I do know Jscript and Java are different, I accidentally clicked the wrong section. That, and my brain is a little frazzled from doing other homework.

    Since the result is an integer, would it be rounded to 13, or would it simply omit the decimal place and display 12?

    I rarely ask for help when it comes to homework, but the book is just as obtuse as my professor was on this chapter, and I was confused.

    Could you elaborate a little on why (10 % 4 – 2) evaluates to zero? This is both my first programming language, and a very basic coding course

    Upon consulting my list of operators, % is described as "take reminder" which is a little more complex of an operation than what is trying to be described to us, so I now get why it's the logic error.
    Last edited by M_Coupe; 11-12-2008 at 07:36 AM.

  • #5
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    so wouldn't it be a logic error (correctly compiling code that displays an incorrect operation from what the programmer intended?)
    That's not a logic error, a logic error is almost synonymous with a runtime error (ie the compiler will let your compile the program with no error messages, or just a warning). In this case the red piece of code evaluates to 0, and you can't divide anything by zero (this would throw an error in any language).

    Modulus returns the remainder from a division of two numbers. If you can remember back to long division in high school (or was it junior high), it's that number at the end you divide by your divisor. However with modulus, you don't divide it by the divisor, you just return it.

    Here's some examples:

    2%2 = 0
    4%2 = 0
    6%2 = 0

    3%2 = 1
    5%2 = 1
    7%2 = 1

    5%4 = 1
    5%3 = 2
    5%2 = 1
    5%1 = 0

    It's most common use is finding out whether a given integer is even or odd (num%2 returns either a 1 or 0).

    4) Your result will not equal 12.5 because result is an int, not a double. I forget whether that throws an error or if it floors the value, you'll want to check that out yourself.
    It's been awhile since I've done Java so I forget whether or not it floors it or throws an error. It never rounds it up though. It doesn't even technically floor it, it "first-end"s it. You can always typecast it to be sure by throwing (int) right after the equals assignment.
    Last edited by itsallkizza; 11-12-2008 at 07:42 AM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #6
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by itsallkizza View Post
    That's not a logic error, a logic error is almost synonymous with a runtime error (ie the compiler will let your compile the program with no error messages, or just a warning). In this case the red piece of code evaluates to 0, and you can't divide anything by zero (this would throw an error in any language).

    Modulus returns the remainder from a division of two numbers. If you can remember back to long division in high school (or was it junior high), it's that number at the end you divide by your divisor. However with modulus, you don't divide it by the divisor, you just return it.

    Here's some examples:

    2%2 = 0
    4%2 = 0
    6%2 = 0

    3%2 = 1
    5%2 = 1
    7%2 = 1

    5%4 = 1
    5%3 = 2
    5%2 = 1
    5%1 = 0

    It's most common use is finding out whether a given integer is even or odd (num%2 returns either a 1 or 0).


    It's been awhile since I've done Java so I forget whether or not it floors it or throws an error. It never rounds it up though. It doesn't even technically floor it, it "first-end"s it. You can always typecast it to be sure by throwing (int) right after the equals assignment.
    I never used a modulus in long division, actually. So it means to divide the two integers and take the remainder?

    So to double check, we have 10/4, retrieving a remainder of 2, and then subtracting two, correct? I am unaware of the order with which the compiler follows, does it do M/D/A/S rule, or does it just go through the statement in order?

  • #7
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    I never used a modulus in long division
    Never said you did, only asked you to remember long division - it's the same remainder concept.

    Java follows normal order of operations, same as in math class in high school. A modulus is the same as division.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #8
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Quote Originally Posted by itsallkizza View Post
    A modulus is the same as division.
    I beg to disagree.

    modulus (%) returns a division remainder while division (/) divides:
    Code:
    alert('5 modulus of 2 is: '+(5%2)+'\n5 divided by 2 is: '+(5/2));
    For further reading:
    http://www.w3schools.com/js/js_operators.asp
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #9
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rangana View Post
    I beg to disagree.

    modulus (%) returns a division remainder while division (/) divides:
    Code:
    alert('5 modulus of 2 is: '+(5%2)+'\n5 divided by 2 is: '+(5/2));
    For further reading:
    http://www.w3schools.com/js/js_operators.asp
    Now I'm confused


    % Modulus (division remainder) x=y%2 x=1

    so 10%4 still does equal 2, correct?


    And can someone explain why the 12.5 output would be incorrect, and how the result would be displayed as an integer? I'm doing a lot of searching online and through my book, and I just can't seem to find what I need
    Last edited by M_Coupe; 11-12-2008 at 10:04 AM.

  • #10
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rangana View Post
    12.5 will be an incorrect output for what equation? I don't know your basis.


    Code:
    var x=12.5; // This is a float
    var convert=parseInt(x); // Already converted to integer
    alert(convert); // Outputs 12 since integer don't have decimal point
    Hope that makes sense.
    5 number1 = 5 * ( 4 + 6 );

    6 number2 = 2 * 2;

    7 result = number1 / number2;

    Answer: number1 =

    number2 =

    result =

    He's just asking for the answers to those fields, and I want to understand why 12.5 in the result field is incorrect. And what would converting it into an integer do? round up, or simply omit the decimal?


    Again, thanks to everyone for the help. I don't want answers, per se, I want to understand why the answers were gotten.

  • #11
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    • Number1= 50. Basic MDAS (Multiplication Division Addition Subtraction). Number within the parenthesis are computed first. So, 5*(4+6) would be disected into:
      Code:
      5*(4+6);
      5*10
      // answer is 50
    • Number2 = 4. Obviously, 2*2 equals 4.
    • Number3 = answer should be 12.5. Not unless you do it this way:
      Code:
      alert(5*(4+6)/2*2); // results 50 as to 5*10*2/2
      Should be:
      Code:
      alert((5*(4+6))/(2*2)); // results 12.5
      ...or, if you are expecting an integer, the value should only be 12 as integer don't have decimal points:
      Code:
      alert(parseInt((5*(4+6))/(2*2))); // Outputs 12


    Hope that makes sense.
    Last edited by rangana; 11-12-2008 at 10:38 AM.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    M_Coupe (11-12-2008)

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,251
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by M_Coupe View Post
    5 number1 = 5 * ( 4 + 6 );

    6 number2 = 2 * 2;

    7 result = number1 / number2;

    Answer: number1 = 5 * 10 = 50 but (5*4+6) evaluates to 26
    number2 = 4

    result = 12.5 truncated to 12 as the variable result is defined as an integer.

    He's just asking for the answers to those fields, and I want to understand why 12.5 in the result field is incorrect. And what would converting it into an integer do? round up, or simply omit the decimal?

    Conversion of a real (decimal) value to an integer truncates the value, so parseInt(12.99) = 12.

    Javascript contains a good many built-in Math methods, including:-
    Math.floor() - round a number down 12.99 > 12
    Math.round() - round to the nearest integer 12.99 > 13 12.49 > 12
    Math.ceil() - round a number up 12.01 > 13


    Again, thanks to everyone for the help. I don't want answers, per se, I want to understand why the answers were gotten.
    Google for more information

  • Users who have thanked Philip M for this post:

    itsallkizza (11-12-2008)

  • #13
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rangana View Post
    • Number1= 50. Basic MDAS (Multiplication Division Addition Subtraction). Number within the parenthesis are computed first. So, 5*(4+6) would be disected into:
      Code:
      5*(4+6);
      5*10
      // answer is 50
    • Number2 = 4. Obviously, 2*2 equals 4.
    • Number3 = answer should be 12.5. Not unless you do it this way:
      Code:
      alert(5*(4+6)/2*2); // results 50 as to 5*10*2/2
      Should be:
      Code:
      alert((5*(4+6))/(2*2)); // results 12.5
      ...or, if you are expecting an integer, the value should only be 12 as integer don't have decimal points:
      Code:
      alert(parseInt((5*(4+6))/(2*2))); // Outputs 12


    Hope that makes sense.
    Truncation. Got'cha. Thanks!

  • #14
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Modulus is the same as division
    I beg to disagree.
    If you had read my entire post and it's context you wouldn't disagree. We were talking about order of precedence, in which case it is the same as division. I had already told him exactly what modulus does in a previous post.

    And it seems like everyone so far has done nothing but repeat my initial post (and incorrectly at that), except for Philip who cleared up the decimal assigned to int issue.
    Last edited by itsallkizza; 11-12-2008 at 04:08 PM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #15
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    rangana, everything you've given them deals with javascript solutions. There is no alert in javascript. To parse a float or double as an int its Integer.parseInt(someint).
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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