Question

In: Computer Science

The Following Java expression: S/10 * 10 + 10 – S = C Show that this...

The Following Java expression:

S/10 * 10 + 10 – S = C

Show that this expression gives a value of 2 when S contains the int value 78. You must show every step in your working, performing exactly one operation on each line.

I have tried to no end to make sense of this and I know there is an order of precedence in the operators but I can't make any sense of it. Could someone please explain this to me?

Solutions

Expert Solution

Please rate the answer. Feel free to contact me in case of any more clarifications required.

Answer =>
The operator precedence is as follows:
* / %    Multiplication, Division,    Modulo    Left-to-right means whichever comes in the left will get higher precedence.
+ -      addition,       subtraction            Left-to-right

So the expression S/10 * 10 + 10 – S = C  will be performed by Java as follows

Step 01. => Replace variables with the original value (S =78). So expression becomes.
            78/10 * 10 + 10 – 78

Step 02. => 78/10 will be done first and decimal will be truncated. Division and multiplication have the highest precedence than addition and subtraction. 
            Here the division (/) will have the highest precedence over multiplication (*) since division is on the left side of the expression. 
            so the division operator will execute first.
            78/10 = 7.8 truncated to 7 and expression will be like 
            7*10 + 10 -78   

Step 03. => 7*10 will be executed as multiplication (*) is precedence over the other operators (+ and -) and expression will become
            70 + 10 -78

Step 04. => 70 + 10 will be executed as operator addition (+) is on the left-hand side and will have higher precedence. And the expression will become
            80 -78

Step 05. => last the minus (-) operator will be executed and the result will become 2.

Why the java is giving 78/10 as 7 in Step 02., is because the S is an integer. The illustration is given below.

Output


Related Solutions

java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expres
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expression                                                 Stack                             Postfix Expression ---------------------------------------------------------------------------------------------------------------------------- 8 - 9*(2 + 1/4) + 3*7                                                     (empty)                            (blank) Evaluate the following Postfix expression. Once again, show all work.            Postfix                                               Stack                     Result           ---------------------------------------    --------------------    --------------            2 7 + 12 4 / * 8 5 + -
Show step-by-step how Java will evaluate the following expression: 8 % 3 + 3 % 2...
Show step-by-step how Java will evaluate the following expression: 8 % 3 + 3 % 2 – 9 % 2.0 * 5 15/15 % 7 *2   – 11. /4 * 5
Define a regular expression in JAVA for the following An "Ent" is an at sign '@',...
Define a regular expression in JAVA for the following An "Ent" is an at sign '@', followed by one or more decimal digits '0' through '9' or uppercase letters 'R' through 'W', followed by an octothorpe sign '#'
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
Using Java The C expression m % n yields the remainders of m divided by n....
Using Java The C expression m % n yields the remainders of m divided by n. Define the greatest common divisor (GCD) of two integers x and y by: gcd(x, y) = y if (y <= x && x % y == 0) gcd(x, y) = gcd(y, x) if (x < y) gcd(x, y) = gcd(y, x % y) otherwise You will submit a program listing (properly commented) and the output using the following data sets: x y 32 8...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis...
create a regular expression in Java that defines the following. A "Stir" is a right brace...
create a regular expression in Java that defines the following. A "Stir" is a right brace '}' followed by zero or more lowercase letters 'a' through 'z' and decimal digits '0' through '9', followed by a left parenthesis '('.
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
c program Evaluate the expression 1 && (30%10 >=0) && (30 %10<=3) Is either set of...
c program Evaluate the expression 1 && (30%10 >=0) && (30 %10<=3) Is either set of parentheses required? Write the complement of the expression two ways. First, add one operator and one set of parentheses. For the second version, use DeMorgan’s theorem.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT