Java Chapter 3
if (radius >= 0); same as if (radius >= 0) { } What type of error is this?
empty block
you can compare whether they are close enough by testing whether the difference of the two numbers is less than some threshold. That is, two numbers x and y are very close if |x−y| < e for a very small value, e. e, a Greek letter pronounced _, is commonly used to denote a very small value
epsilon
The _ testing operator is two equal signs (==), not a single equal sign (=). The latter symbol is for _.
equality, assignment
Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as _.
fall-through behavior
(age > 34) ^ (weight > 140) is _, because (age > 34) and (weight > 140) are both false.
false
setup the EPSILON variable for a floating variable
final double EPSILON = 1E-14;
double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1; System.out.println(x == 0.5); Here, x is not exactly 0.5, but is 0.5000000000000001. You cannot reliably test equality of two _ values.
floating-point
if statement to check whether 0.5000000000000001 (x) is approximately 0.5
if (Math.abs(x - 0.5) < EPSILON)
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is:
if (boolean-expression) { statement; }
if (even == true) is redundant. Better:
if (even)
(1) If the number is a multiple of 5, the program displays HiFive. If the number is divisible by 2, it displays HiEven. (Just write if statement)
if (number % 5 == 0)
// If number1 < number2, swap number1 with number2. First line in program (ignoring setup parameters)
if (number1 < number2) {
Common Error 2: Wrong Semicolon at the
if Line
An _ is a construct that enables a program to specify alternative paths of execution.
if statement
An _ statement decides the execution path based on whether the condition is true or false.
if-else
if (number % 2 == 0 || number % 3 == 0) System.out.println(number + "__"); <<<if the above selection statement is TRUE>>
is divisible by 2 or 3
if (radius >= 0); same as if (radius >= 0) { }. This mistake is hard to find, because it is neither a compile error nor a runtime error; it is a _.
logic error
||
logical disjunction
^
logical exclusion
!
logical negation
Suppose you want to assign the larger number of variable num1 and num2 to max. You can simply write a statement using the conditional expression:
max = (num1 > num2) ? num1 : num2;
The previous programs generate random numbers using System.currentTimeMillis(). A better approach is to use the random() _ in the Math _.
method, class
style, called _, avoids deep indentation and makes the program easy to read
multi-way if-else statements
The inner if statement is said to be _ inside the outer if statement.
nested
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. A _ status code indicates abnormal termination.
nonzero
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated _.
normally
the __ operator, which negates true to false and false to true.
not (!)
!(number == 2 || number == 3) is better written as
number != 2 && number != 3
// If number1 < number2, swap number1 with number2 line 3
number1 = number2;
// If number1 < number2, swap number1 with number2 line 4 (Swap)
number2 = temp;
Java has several types of selection statements: _ if statements, _ if-else statements, _ statements, _ if-else statements, _ statements, and _ expressions.
one-way, two-way, nested if, multi-way, switch, conditional expressions
The keyword break is _. The break statement immediately ends the switch statement.
optional
The __ of two Boolean operands is true if at least one of the operands is true
or (||)
Note that p1 ^ p2 is the same as _
p1 != p2
Operator _ and _ determine the order in which operators are evaluated.
precedence, associativity
delete
d
A variable that holds a Boolean value is known as a Boolean variable. The boolean _ is used to declare Boolean variables.
data type
The value1, . . ., and valueN must have the same __ as the value of the switchexpression. Value1, . . ., & valueN are ___ expressions, meaning that they cannot contain variables, such as 1 + x.
data type, constant
The _ which is optional, can be used to perform actions when none of the specified cases matches the _.
default case, switch-expression
The _ case, which is _, can be used to perform actions when none of the specified cases matches the switch-expression
default, optional
Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; __ }
default: statement(s)-for-default;
if (number % 2 == 0 ^ number % 3 == 0) System.out.println(number + "is __");
divisible by 2 or 3 but not both
A better approach is to use the random() method in the Math class. Invoking this method returns a random value of the type _.
double
The __ of two Boolean operands is true if and only if the two operands have different Boolean values
exclusive or (^)
True and false are _, just like a number such as 10. They are treated as reserved words and cannot be used as identifiers in the program.
literals
&&
logical conjunction
less than or equal to (as relational operator)
<=
equal to (as relational operator)
==
The following errors are common among new programmers. Common Error 1: forgetting _
Forgetting Braces
Here is the full syntax for the switch statement: switch (switch-expression) { ___ statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
case value1:
To avoid programming errors and improve code maintainability, it is a good idea to put a _ in a case clause if break is purposely omitted.
comment
An initial value of 0 is assigned to tax (line 20). A _ error would occur if it had no initial value, because all of the other statements that assign values to tax are within the if statement. The compiler thinks that these statements may not be executed and therefore reports a compile error.
compile
if (even = true) is wrong but even so, won't show a _ error. Should be:
compile, ==
A __ evaluates an expression based on a condition
conditional expression
The symbols ? and : appear together in a conditional expression. They form a _ and also called a _ operator because it uses three operands.
conditional operator, ternary (It is the only ternary operator in Java.)
Example (assume age = 24, weight = 140) is false, because (age > 18) is _.
!(age > 18), true
_ is true, because (weight == 150) is false.
!(weight == 150)
not equal to (as relational operator)
!=
(age > 18) _ (weight >= 140) is true, because (age > 18) and (weight >= 140) are both true
&&
1 <= no <= 31 should be
(1 <= no) && (no <= 31)
Like all high-level programming languages, Java provides _ :statements that let you choose actions with alternative courses.
selection statements
In programming language terminology, && and || are known as the _ or _ operators
short-circuit, lazy
The block braces can be omitted if they enclose a _. For example, the following statements are equivalent. if (i > 0) System.out.println("i is positive");
single statement
How do you compare two values, such as whether a radius is greater than 0, equal to 0, or less than 0? Java provides __ operators (also known as _ operators), shown in Table 3.1, which can be used to compare two values
six relational, comparison
A _ statement executes statements based on the value of a variable or an expression.
switch
Here is the full syntax for the switch statement: ____ case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
switch (switch-expression) {
(age > 34) ^ (weight >= 140) is _, because (age > 34) is false but (weight >= 140) is true
true
double radius = 1; System.out.println(radius > 0); this displays:
true
if (x > 0) y = 1; else y = -1; Alternatively, as in the following example, you can use a conditional expression to achieve the same result.
y = (x > 0) ? 1 : -1;
(age > 34) _ (weight >= 150) is false, because (age > 34) and (weight >= 150) are both false
||
(age > 18) _ (weight < 140) is true, because (age > 18) is true.
|| (or operator)
// If number1 < number2, swap number1 with number2 line 5: the closing line
}
_ returns a random single-digit integer (i.e., a number between 0 and 9).
(int)(Math.random() * 10)
You can use _ to obtain a random double value between 0.0 and 1.0, excluding 1.0.
Math.random()
assign true to lightsOn
boolean lightsOn = true;
Common Error 3: Redundant Testing of
Boolean Values
Normally, you set e to _for comparing two values of the double type and to _ for float
1E-14, 1E-7
A variable that holds a Boolean value is known as a
Boolean variable
Common Error 4: __ Ambiguity
Dangling else
_'s law, named after Indian-born British mathematician and logician can be used to simplify Boolean expressions. The law states: !(condition1 && condition2) is the same as !condition1 || !condition2 !(condition1 || condition2) is the same as !condition1 && !condition2
De Morgan
Common Error 5: _of Two Floating-Point Values
Equality Test
_ operators, also known as Boolean operators, operate on Boolean values to create a new Boolean value.
Logical
The _ method can be used to return the absolute value of a
Math.abs(a)
_ use conditions that are Boolean expressions. A Boolean expression is an expression that evaluates to a Boolean value: true or false. We now introduce Boolean types and relational operators
Selection statements
_ (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated normally. A nonzero status code indicates abnormal termination.
System.exit(status)
The __ of two Boolean operands is true if and only if both operands are true
and (&&)
improve: Often, new programmers write the code that assigns a test condition to a boolean variable like the code in (a): if (number % 2 == 0) even = true; else even = false;
boolean even = number % 2 == 0;
In mathematics, the expression 1 <= numberOfDaysInAMonth <= 31 is correct. However, it is incorrect in Java, because 1 <= numberOfDaysInAMonth is evaluated to a __, which cannot be compared with 31.
boolean value
Conditional expressions are in a completely different style, with no explicit if in the statement. The syntax is:
boolean-expression ? expression1 : expression2;
Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; ___; ...
break
When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a _ _ or the end of the switch statement is reached
break statement
Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a _ statement or the __ statement is reached
break, end of the switch
For all programs, you should write a small amount of code and test it before moving on to add more code. This is called _ development and testing
incremental
enter a guess of lottery number
int guess = input.nextInt();
Generate a lottery number (0-100) & call the variable lottery
int lottery = (int)(Math.random() * 100);
// 1. Generate a random single-digit integer, number1 between 1-10
int number1 = (int)(Math.random() * 10);
// If number1 < number2, swap number1 with number2 line 2
int temp = number1;
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. Collect the input for year:
int year = input.nextInt();