Java: Find the value of specified expression
Evaluate Expressions
Write a Java program to find the value of a specified expression.
a) 101 + 0) / 3
b) 3.0e-6 * 10000000.1
c) true && true
d) false && true
e) (false && false) || (true && true)
f) (false || false) && (true && true)
Sample Solution:
Java Code:
public class Solution {
public static void main(String[] args) {
// Calculate the result of (101 + 0) divided by 3
int r1 = (101 + 0) / 3;
// Calculate the result of 3.0e-6 multiplied by 10000000.1
double r2 = 3.0e-6 * 10000000.1;
// Determine if both operands (true and true) are true using the AND operator
boolean r3 = true && true;
// Determine if one operand is false among true and false using the AND operator
boolean r4 = false && true;
// Determine if at least one operand is true among different combinations using the OR and AND operators
boolean r5 = (false && false) || (true && true);
// Determine if both sets of operands separately evaluate to false and true, using OR and AND operators
boolean r6 = (false || false) && (true && true);
// Display the results of the calculations
System.out.println("(101 + 0) / 3) -> " + r1);
System.out.println("(3.0e-6 * 10000000.1) -> " + r2);
System.out.println("(true && true) -> " + r3);
System.out.println("(false && true) -> " + r4);
System.out.println("((false && false) || (true && true)) -> " + r5);
System.out.println("(false || false) && (true && true) -> " + r6);
}
}
Sample Output:
(101 + 0) / 3)-> 33 (3.0e-6 * 10000000.1)-> 30.0000003 (true && true)-> true (false && true)-> false ((false && false) || (true && true))-> true (false || false) && (true && true)-> false
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to evaluate a complex expression mixing arithmetic, bitwise, and logical operators without using extra parentheses.
- Write a Java program to compute the value of an expression that combines integer division, modulus, and floating-point multiplication in one statement.
- Write a Java program to evaluate a nested ternary expression that includes relational and bit-shift operations.
- Write a Java program to test Java operator precedence by evaluating an expression containing arithmetic, relational, and logical operators.
Go to:
PREV : Check Subtree in Binary Tree.
NEXT : Check Four Numbers Equal.
Java Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.