Results 1 to 5 of 5
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Trace the evaluation of expressions, and give their resulting values??

    I'm new to Java and have no clue what to do from here. This is due on the 23rd. I've only worked with Visual Basic before.
    I'm supposed to "trace the evaluation of...expressions, and give their resulting values." Help?

    // double a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t

    public class Lab2Assignment1 {
    public static void main(String[] args) {

    a = 2 + 3 * 4 - 6;
    System.out.println(a);

    b = 14 / 7 * 2 + 30 / 5 + 1;
    System.out.println(b);

    c = (12 + 3) / 4 * 2;
    System.out.println(c);

    d = (238 % 10 + 3) % 7;
    System.out.println(d);

    e = (18 - 7) * (43 % 10);
    System.out.println(e);

    f = 2 + 19 % 5 - (11 * (5 / 2));
    System.out.println(f);

    g = 813 % 100 / 3 + 2.4;
    System.out.println(g);

    h = 26 % 10 % 4 * 3;
    System.out.println(h);

    i = 22 + 4 * 2;
    System.out.println(i);

    j = 23 % 8 % 3;
    System.out.println(j);

    k = 12 - 2 -3;
    System.out.println(k);

    l = 6 / 2 + 7 / 3;
    System.out.println(l);

    m = 6 * 7 % 4;
    System.out.println(m);

    n = 3 * 4 + 2 * 3;
    System.out.println(n);

    o = 177 % 100 % 10 / 2;
    System.out.println(o);

    p = 89 % (5 + 5) % 5;
    System.out.println(p);

    q = 392 / 10 % 10 / 2;
    System.out.println(q);

    r = 8 * 2 - 7 / 4;
    System.out.println(r);

    s = 37 % 20 % 3 * 4;
    System.out.println(s);

    t = 17 % 10 / 4;
    System.out.println(t);
    }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Trace the evaluation of expressions, and give their resulting values??

    I do not believe that you are expected to do this programmatically but use the program to check your answers. This has to do with operator precedence. You start from the left side of the expression and go to the right and process the arguments in order. However, you need to apply higher precedent operators first. Here is an example.

    double a = 4 + 3 * 7 - 2;
    * is higher precedence than + so it would be
    4 + 21 - 2
    + and - are the same precedence so
    25 - 2

    The answer is 23.

    To avoid any ambiguity when writing expressions, always use ()'s. They always have highest priority and are processed inward to outward.

    (2 - (3+4)) + 5
    (2 - 7) + 5
    -5 + 5
    0

    Don't forget about integer division. 15/2 = 7, not 7.5

    For more info check out --> Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards,
    Jim
    Last edited by jim829; 09-20-2013 at 10:58 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Trace the evaluation of expressions, and give their resulting values??

    Thank you SO much!

    How could I show the actual variables though, not just the answers?

    Ex. a. 8.0

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Trace the evaluation of expressions, and give their resulting values??

    System.out.println("a = " + a);

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

  5. #5
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Trace the evaluation of expressions, and give their resulting values??

    Thanks SO much!!

Similar Threads

  1. Jinfo,gc and gcutil give different values
    By newbie14 in forum Advanced Java
    Replies: 0
    Last Post: 02-10-2013, 06:36 PM
  2. Replies: 0
    Last Post: 09-24-2010, 11:03 AM
  3. Replies: 0
    Last Post: 05-28-2009, 05:47 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
  •