In: Computer Science
Model 2 Relational Operators
In Model 1, you determined the top three attributes by comparing percentages. We can declare variables to represent these percentages in Java:
double written = 82.0; // Communication skills (written)
double problem = 80.9; // Problem-solving skills
double teamwork = 78.7; // Ability to work in a team
In the table below, predict the result of each expression and identify the operator (if any). The first three rows are completed for you.
Expression | Result | Operator |
written |
82.0 | none |
written > problem | true | > |
problem < teamwork | false | < |
82.0 < written | ||
82.0 > written | ||
82.0 == written | ||
problem == written | ||
teamwork == problem | ||
teamwork = problem | ||
teamwork == problem | ||
problem | ||
teamwork |
5. A relational operator compares two values; the result is either true or false. Identify the three relational operators used in the table above.
6. Explain why the same expression teamwork == problem resulted with two different values in the table.
7. What is the difference between = and == in Java?
8. The != relational operator means “not equals”. Give an example of a boolean expression that uses != and evaluates to false.
9. The >= relational operator means “greater than or equal to”. Give an example of a boolean expression that uses >= and evaluates to true.
10. Java has six relational operators. Only five have been mentioned, but you should be able to guess the sixth. List all six below, and explain briefly what each one means.
Expression
Result
Operator
----------
------
--------
written
82.0
none
written > problem
true >
problem < teamwork
false <
82.0 < written
false <
82.0 > written
false >
82.0 == written
true ==
problem == written false
==
teamwork == problem
false ==
teamwork = problem 80.9
assignment, not a relational operator
teamwork == problem true
==
problem
80.9
none
teamwork
80.9 none
5. The relational operators are ==, > and <
6. The assignment expression, teamwork = problem, makes the value of teamwork same as problem; hence the same logical expression, teamwork == problem has different values before and after the assignment operation.
7. The = is an assignment operation, assigning the value from the right hand side to the variable on the left hand side. The == is a relational operator comparing two values for equality.
8. teamwork != problem is the required expression; it evaluates to false because both are equal to 80.9.
9. written >= problem is a boolean expression that evaluates to true.
10. The relational operators are:
== equals
!= not equals
> greater than
< less than
<= less than or equal to
>= greater than or equal to