Chapter 3
Write an expression that returns a random integers between 0 and 9, inclusive.
(int)(Math.random() * 10)
Write an expression that evaluates to true if the value of the int variable numberOfPrizes is divisible (with no remainder) by the int variable numberOfParticipants. Assume that numberOfParticipants is not zero.
(numberOfPrizes % numberOfParticipants == 0)
What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5? T/F
There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
==
equal to
What is (int)Math.random() * 10?
0
What is the output from System.out.println((int)Math.random() * 4)?
0
Which of the following is a possible output from invoking Math.random()?
0 to 1
What is the possible output from System.out.println((int)(Math.random() * 4))?
0,1,2,3
The "less than or equal to" comparison operator in Java is __________.
<=
The equal comparison operator in Java is __________.
==
Selection Statements
A statement that uses IF or switch(ELSE?) statement to control the execution(RUN) of the program.
Multiple-Choice Question 3.5.3 Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? Which one is well written? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");
All 2
Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; boolean even = (number % 2 == 0);
Both Code 1 and Code 2 are correct, but Code 2 is better.
Suppose x = 1, y = -1, and z = 1. What is the output of the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");
x < 0 and z > 0;
Suppose income is 4001, what is the output of the following code? if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); }
Income is greater than 3000
what kinda of words are true/false in programming?
are literals, which means they're reserved words.
Analyze the following code. boolean even = false; if (even) { System.out.println("It is even!"); }
The code displays nothing.
Write an expression that evaluates to true if the value x is greater than or equal to y.
x >= y
Given a double variable called average, write an expression that is true if and only if the variable's value is less than 60.0.
average < 60.0
Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even"); }
The program runs fine and displays It is even.
What is 1 + 1 + 1 + 1 + 1 == 5? T/F
True
In Java, the word true is ________.
a Boolean literal
>
greater than
>=
greater than or equal to
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
if (isPrime)
Which of the following code displays the area of a circle if the radius is positive.(write)
if (radius > 0) System.out.println(radius * radius * 3.14159);
Boolean expression/Boolean Data Type/Boolean Variable
in programming, an expression that evaluates to True or False.
what is a flow chart
is a diagram that describes and algorithm or process, showing the steps as boxes of various kinds and their order by connecting these with arrows.
"Not divisible" means
it has a remainder, you use != 0 or > 0 and %
Relational Operators
known as comparison operators
<
less than
<=
less than or equal to
"Is divisible" means
no remainder ==0 and %
!=
not equal to
Write an expression that evaluates to true if the value of the int variable widthOfBox is not divisible by the value of the int variable widthOfBook. Assume that widthOfBook is not zero. ("Not divisible" means has a remainder.)
widthOfBox % widthOfBook != 0
Assume x is an integer. Write a simple expression that returns true if x is even or false if x is odd
x % 2 == 0