Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Urgent Help Nedded!!!

  • 11-08-2004 10:33pm
    #1
    Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭


    hi,
    this is a java related question. i have to repeat this exam in college!!!

    int w=6, x=7, y=8, x=9, result;

    Compute the following results and show step by step how the result is achieved:

    result=w*y/x%z+7;

    heres what i got.
    result=6*8/7%9+7;
    result=48/7%9+7;
    result=6%9+7;
    result=3+7;
    result=10;

    is this answer correct


«1

Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    No. The operator precedence is correct, but the final answer is incorrent.


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    Your mistake is with the modulus operator, here some instructions..

    http://www.cs.umd.edu/~clin/MoreJava/Intro/expr-mod.html


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Whereas I was leaving it open for him to find the answer himself... Like a good programmer should learn to do, rather than have the solution handed on a silver platter.


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    Whereas I was leaving it open for him to find the answer himself... Like a good programmer should learn to do, rather than have the solution handed on a silver platter.

    I was trying to do the same, but with a thread title Urgent Help Nedded!!!, I thought that a little more help was called for


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    fragile,
    this makes no sense to me! can you please explain? i keep giong over and over it and i still keep getting 10 for the answer!!!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    Please Can Someone Help Me! I Have To Repeat This Exam Next Week And I Cant Afford To Spend Ages Looking For Answers!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    Please Im In Need Of Some Urgent Help Here!!!!!!!!


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    You ran the Modulus operator the wrong way.

    9%6 == 3
    6%9 == ?


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    Galwaydude in all fairness this is only one potential question in your exam, did you read and understand the explanations in the link I posted? I have already told you that your mistake is with your understanding of how the modulus operator works.....come on you are 90% there, just re-read how the modulus operator works and try to figure it out...


    I dont mean to be cruel, if you are still stuck just reply and I will post up a full explanation, but you really should give it another genuine try


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    fragile 6%9 give a number less than 1. this cannot be correct according to the instructions in the link you gave me!! (the result is always greater than or equal to 0)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    (the result is always greater than or equal to 0)

    So, if the result cant be less than 0 what is the remainder of 6%9? and remember this is not the same as 6/9..


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    fragile the remainder would be 0 is this correct because if it is that would mean the answer is 7 right?


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    PM sent


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    fragile i didnt get your pm!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    fragile how about this

    int w=6, x=7, y=8, x=9, result;

    result=(w-y)/(x-z)+51%(x+5);

    i got result=3; for the answer!


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    10 % 2 = 0

    Why?
    Take 2 from 10 until you can't do it fully any more ...
    10 - 2 = 8
    8 - 2 = 6
    6 - 2 = 4
    4 - 2 = 0
    2 - 2 = 0
    0
    --> 10%2=0

    11 % 2 = ?
    11 - 2 = 9
    9 - 2 = 7
    7 - 2 = 5
    5 - 2 = 3
    3 - 2 = 1
    1 < 2 so stop here. Your MOD answer is 1 (your remainder)

    Try it like this so:
    15 % 8 = ?
    15 - 8 = 7
    7 < 8 so stop
    7 == your answer

    Does that help you to understand it?
    Daniel


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    thanks! i understand it now! do you know what this means?

    int w=6,x=7,y=8,z=9, result;

    result=(x>=y)?x-y:y-x;

    i do not understand it!!


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    thanks! i understand it now! do you know what this means?

    int w=6,x=7,y=8,z=9, result;

    result=(x>=y)?x-y:y-x;

    i do not understand it!!

    PM sent


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    fragile how about the pm i sent you? what do they mean??? Thanks for your help so far!!!!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    what do these mean?

    int w=6,x=7,y=8,z=9, result;

    result=x*((y==8)?100:999);

    result=(--x*w++)/z++ - ++y;

    i do not understand them!!

    here's the answers i got! can someone please explain them in more detail?

    result=x*((y==8)?100:999);
    result=7*((8==8)?100:999);
    result=7*100;
    result=700;

    Ok, for the next one, if the operator is before the variable (--y) you calculate it straight away, if it is after the variable (x++) you ignore it (it does matter in loops, but not in these examples)

    result=(--x*w++)/z++ - ++y;
    result=(--7*6++)/9++ - ++8;
    result=(36)/9 - 9;
    result=4-9;
    result=-5;


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    int w=6,x=7,y=8,z=9, result;
    result=x*((y==8)?100:999);
    int w = ...... <-- declaration of variables
    how? ::
    eg: result = x*(y==8)?100:99;
    -->
    if(y == 8)
    result = x*100;
    else
    result = x*99;
    <--

    result=x*((y==8)?100:999);
    IF y == 8, THEN result = x* 100, ELSE result = x * 999

    result=(--x*w++)/z++ - ++y;
    x = x - 1;
    result = x * w;
    w = w + 1;
    result = result / z;
    z = z + 1;
    y = y + 1;
    result = result - y;
    Ok, for the next one, if the operator is before the variable (--y) you calculate it straight away, if it is after the variable (x++) you ignore it (it does matter in loops, but not in these examples)
    If you come across --y, you calculate it before moving on. If you come across y++, you calculate it immediately after using the variable y as it is at that moment.
    eg:
    x = 1;
    y = 3;

    ++x + --y + --x + y++ = ??
    2 + 2 + 1 + 2 = 7
    after the calculation:
    x = 2;
    y = 3;

    Is that any clearer now?

    *edit*
    galwaydude18: The only reason that you're receiving this level of help is that you're obviously stressed out over your upcoming exam. As a rule, programmers generally don't give out real code when someone asks for help - the fact that you're having difficulty grasping these basic elements of Java, shows that you probably lack a deeper understanding of the language in general ... and that's something that we can't help you with.

    Remeber that we won't be at your exam with you, and also that at one time, all of us knew absolutely NOTHING about Java. It takes time to learn and master all of these small things - but when you do you'll find that you'll look back on this thread and thing "Damn ... how did I not know that?!"

    Daniel


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    you could write a small java program and then run each step to see what the output is. Might be easier for you to understand.


  • Closed Accounts Posts: 537 ✭✭✭JohnnyBravo


    Whereas I was leaving it open for him to find the answer himself... Like a good programmer should learn to do, rather than have the solution handed on a silver platter.


    Dont over estimate the silver platter way
    Best way to learn is have a working example


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    DRJava is a very useful tool for anybody trying to get to grips with the Java language.
    One of the key distinguishing features of DrJava is its Interactions Pane, which allows you to enter and evaluate Java statements and expressions on the fly. This is remarkably useful for beginning students, who no longer have to learn to write main methods, recompile, and run programs from a command line simply to test how a new class or method behaves. From a teaching standpoint, the Interactions Pane is a very easy way to help students learn to write Java without having to explain the full meaning of syntax like "public static void main", and it also provides an ideal way to perform demonstrations in class. The Interactions Pane can also be used to experiment with your own programs or new libraries, or even to create graphical user interfaces interactively

    It can be downloaded here


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Dont over estimate the silver platter way
    Best way to learn is have a working example

    It is in the short term, but if the person can't explain what something is doing all a working example is going to provide is a cut + paste. This is counter productive for learning.

    It is quite clear from what he has posted so far that working examples will mean nothing to him.


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    Hobbes wrote:
    It is in the short term, but if the person can't explain what something is doing all a working example is going to provide is a cut + paste. This is counter productive for learning.

    It is quite clear from what he has posted so far that working examples will mean nothing to him.

    Thanks to help of everyone eles i now do understand it!!! i went through other past papers this morning and did them all without any problems!!!! :D


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    Well done galwaydude18 - best of luck in your exams!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    how do you do this? i can not figure it out to save my life! even using my notes is no help to me!!!

    Rewrite the following program sgement using a for loop

    int x=11, y=14;
    while (x<=27)
    {
    System.out.println ("The value of y-x is:" +y-x);
    x+=2;
    }

    Guys your help would be appreciated on this!!!!


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Honestly, read the loop structure carefully, disect what each line does and re-work it into the format of a for loop instead, its really a very simple example.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    Dude its simple when you know what your talking about and what it means but I honestly do not know what I do to transform it in to a for loop!!! can you please help me!!! PLEASE???????!!!!


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    When is your exam? Monday? Tuesday?
    You've still got a lot of work to do man ... really, it's simple logic to figure this out.

    What does a for loop mean?
    for (int i = 0; i < 10; i++)
    { }

    The first part, int i = 0, declares the variable that will be used to control the for loop.

    Next, i < 10, creates the bounds for the loop ... ie: when it will stop. In my case, it will stop when i >= 10

    Next, i++, shows the calculation to be completed after each loop that relates to the increment (or decrement) of the counter. It can be i++, i--, i + 10, i + 672, i - 1, i/2, i/33, i*5 ..... and so on

    Now have a look at your while loop and see if you can fill in the gaps.
    int x=11, y=14;
    while (x<=27)
    {
    System.out.println ("The value of y-x is:" +y-x);
    x+=2;
    }
    
    You have the second variable, the bounds - as it's in the while loop. so variable 2 = x <= 27
    You also have the last variable, the calculation - variable 3 = x += 2
    Now, if you look closely, you also have the first variable. Variable 1 = int x = 11

    So now that you have all that you need to create the for loop - put up what you think is the answer. Taking for granted that I've almost given you the answer, it's not that hard!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    the ans is
    y=14
    for (int x=11, x<=27; x+=2)
    {
    System.out.println ("The value of y-x is:" +y-x);
    }


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    the ans is
    y=14
    for (int x=11, x<=27; x+=2)
    {
    System.out.println ("The value of y-x is:" +y-x);
    }

    Yup pretty much! :)
    Now, wasn't that difficult was it? :p Break the problem up into smaller parts as I have done, then put them all together into the bigger picture.

    One thing to note is that alot of programmers tend to avoid the = sign in loops unless neccessary - ie: x <= 27 becomes x < 28

    We're all taught to obey the off-by-one thingy, such as in an array, the first element is 0, not 1, and I suppose that that's the reason that <28 just seems more natural than <=27 ..... but ignore my ramblings :p


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    What is the execution result of the following piece of code?
    (Show clearly how you calculate the output)

    int x=0;
    int y=0;
    for (int i=2; i<8; i+=2)
    {
    while (x<4)
    {
    x+=1;
    }
    y+=1;
    }
    System.out.println ("x=" +x+", y="+y);

    how do you do this? any help would be appreciated!!!


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    What is the execution result of the following piece of code? (Show clearly how you calculate the output)
    01: int x=0;
    02: int y=0;
    03: for (int i=2; i<8; i+=2)
    04: {
    05:    while (x<4)
    06:    {
    07:       x+=1;
    08:    }
    09:    y+=1;
    10: }
    11: System.out.println ("x=" +x+", y="+y);
    
    I think it's time that you worked it out galwaydude18 ...

    Step through it slowly. What exactly do you think is happening to the variables at each line of code?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    Well you're not being very responsive - here's a nudge in the right direction.

    Everything in the for loop is going to be repeated a number of times. Inside that, the while loop will be repeated a number of times.

    As both x and y are in different scopes in the two loops, and they are both incrementing, x will increment at a faster rate than y.....


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    A friend helped me with this. he explained it to me but i still not fully understand it. from what he told me the while loop runs until it reaches 4 then it stops and comes out of the equation then the for loop continues on until it reaches8 and then your result is finally displayed! correct me if i am wrong here? heres the results i got

    i x y
    2 0 0
    2 1 0
    2 2 0
    2 3 0
    2 4 1
    4 4 2
    6 4 3
    8 4 4


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    Actually, my apologies, I misread the loop variables.
    i   x  y
    2  0  0
    2  1  0
    2  2  0
    2  3  1
    4  3  2
    6  3  3
    8 -> exit loop as bound: i < 8 (I must be less than 8 to continue the loop)
    
    So, that should print:

    x=3,y=3

    according to your program


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    zoro my friend is absolutly 100% convienced he is right!! can you explain to me how you got them results! my friend did it twice and got two different sets of results. your help will be appreciated in this!!!


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    ok, lets check it out so
    int x, y=0;
    for (int i=2; i<8; i+=2)
    {
       while (x<4)
       {
          x+=1;
       }
       y+=1;
    }
    System.out.println ("x=" +x+", y="+y);
    

    When you enter the for() loop, x and y are 0, and i = 2... 2-0-0
    On the first iteration of the for() loop, you reach a while() loop, while increments x by 1 while it is < 4....
    2-1-0
    2-2-0
    2-3-0
    2-4-0 <-- I've just added this as I forgot to increment x in my previous answer

    The while() loop now exits as x no longer satisfies the "x<4" boundary.
    Onto y+=1:
    2-4-1

    Now, on the next iteration of the for() loop, i = 4. BUT x = 4 too ... so the while() is never entered...
    4-4-2
    then...
    6-4-3

    now, i is 8, and no longer satisfies the "i<8" boundary, so the loop exits.

    From what I can see, the correct answer is:
    x=4,y=3
    :)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    can you please verify the very last part of the answer? my friend still reckons the last line on the table will read

    i x y
    8 4 4


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    That's one of the parts that I assume is intended to trick you.

    As the program never enters the for() loop once i = 8, y cannot be incremented to 4, and so stays at 3.

    Does that make sense?


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    And I've just tested it in java ...

    x=4, y=3


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    why can you not increment y to be 4?


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    Just before i is incremented to 8, y = 3
    The for loop states, that it will continue to proceed through the loop until ( i < 8 ) is no longer true.

    As soon as i becomes 8, this evaluation becomes false, so the for() loop is never entered again, and so y is never incremented (as that increment is inside the for() loop)


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    ah right!!! i see what you mean now!!


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    *slaps forehead* :)


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    thanks man! i will try another example or two from past papers when i finish work today and will get back on to you with how i got on!!!!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    zoro can you change this so it is in java language! the reason i am asking this is because they only started teaching java to first years this year. this question out the paper is in C++ as far as i can figure. here it is:


    int y=13;
    for (int x=5; x<30; x+=5)
    {
    y=y+x;
    cout<< "The value of y is:" <<endl;
    }

    thanks a million!!!!


  • Registered Users, Registered Users 2 Posts: 3,695 ✭✭✭galwaydude18


    can someone please change this so it is in java language! the reason i am asking this is because they only started teaching java to first years this year. this question out the paper is in C++ as far as i can figure. here it is:


    int y=13;
    for (int x=5; x<30; x+=5)
    {
    y=y+x;
    cout<< "The value of y is:" <<endl;
    }

    thanks a million!!!!


  • Advertisement
Advertisement