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

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...
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 '('.
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
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 - /
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
For the following exercises, compute the value of the expression. C(12, 4)
For the following exercises, compute the value of the expression.C(12, 4)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT