Docsity

Relational Operators in CS 2401: Elementary Data Structures and Algorithms, Study notes of Computer Science

Toggle sidebar
bg1
Class: CS 2401 โ€“ Elementary Data Structures and Algorithms
Length of session: 50 minutes
Topic: Relational Operators
Relational Operators
Because last week they just took a test, this section will provide a little fun. I would like to
try out the game technique this week.
1. Divide them into 3-4 groups (there are 17 students) 2-5 minutes
2. Five questions about relational operators will be given to each team. Each member of the
group will be in charge of one problem. However, they should work as a group in solving
these problems. (25 minutes)
3. I will state one of the problems and pick one student randomly to present the problem to
the whole class. After stating the problem, I will give 30 seconds to the student to process
the question. Then, the presenters have to come on the board after 30s and write down the
answer (2-3 minutes). Each point will be given to every right answer.
4. Chocolate will be the prize for the winning group.
Questions:
a. What is the output for this code?
double b = 3.0/7.0 + 2.0/7.0 + 2.0/7.0;
if (b = = 1.0)
System.out.println("Value of a equals 1.0");
else
System.out.println("Value of a does not equal 1.0");
Answer: Value of a does not equal 1.0 (reference page 171 of textbook)
pf2

Partial preview of the text

Download Relational Operators in CS 2401: Elementary Data Structures and Algorithms and more Study notes Computer Science in PDF only on Docsity!

Class: CS 2401 โ€“ Elementary Data Structures and Algorithms Length of session: 50 minutes Topic: Relational Operators

Relational Operators

Because last week they just took a test, this section will provide a little fun. I would like to try out the game technique this week.

  1. Divide them into 3-4 groups (there are 17 students) 2-5 minutes
  2. Five questions about relational operators will be given to each team. Each member of the group will be in charge of one problem. However, they should work as a group in solving these problems. (25 minutes)
  3. I will state one of the problems and pick one student randomly to present the problem to the whole class. After stating the problem, I will give 30 seconds to the student to process the question. Then, the presenters have to come on the board after 30s and write down the answer (2-3 minutes). Each point will be given to every right answer.
  4. Chocolate will be the prize for the winning group.

Questions:

a. What is the output for this code? double b = 3.0/7.0 + 2.0/7.0 + 2.0/7.0; if (b = = 1.0) System.out.println("Value of a equals 1.0"); else System.out.println("Value of a does not equal 1.0"); Answer: Value of a does not equal 1.0 (reference page 171 of textbook)

b. Given x and y are floating-point numbers, what does Math.abs(x-y) method do? What is the practical purpose of this method (clue: What is the purpose of this expression Math.abs(x-y) < 0.000001 )? Answer: This method will determine whether x equals to y.

c. What is the result of these two expressions? Explain your answers a. 8 < '5' b. 8 < 5 Answer: a. true (integer compare with char, using Unicode character set) b. false

d. What is the difference between equals() and compareTo()? Answer: equals() returns Boolean type compareTo() returns Integer type

e. Put these operators in precedence order:

  • && (6)
  • = (8)
  • !, +, - (unary operators) (1)
  • +, - (3)
  • = =, != (5)
  • || (7)
  • *, /, % (2)
  • <, <=, >=, > (4)