Ch. 3
true && true
true
logical operators in order of precedence?
! && ||
general syntax for writing a format specifier is:
%[flags][width][.precision]conversion
when determining whether a number is inside a range, it's best to use this operator. a. && b. ! c. || d. ? :
a. &&
this is a boolean variable that signals when some condition exists in the program. a. flag b. signal c. sentinel d. siren
a. flag
This is an if statement that appears inside another if statement a. nested if statement b. tiered if statement c. dislodged if statement d. structured if statement
a. nested if statement
>, <, and == are _____. a. relational operators b. decision expression c. unconditional expression d. boolean expression
a. relational operators
an else clause always goes with ______. a. the closest previous if clause that doesn't already have its own else clause. b. the closest if clause c. the if clause that is randomly selected by the compiler d. none of these
a. the closest previous if clause that doesn't already have its own else clause.
conditionally executed
action performed only when a certain condition exists
System.out.printf method
allows you to format output in a variety of ways. allows you to format a string, without displaying it. The string can displayed at a later time.
How does that character 'A' compare the the character 'B'? a. 'A' is greater than 'B' b. 'A' is less than 'B' c. 'A' is equal to 'B' d. you cannot compare characters
b. 'A' is less than 'B'
The if statement is an example of a _____. a. sequence structure b. decision structure c. pathway structure d. class structure
b. decision structure
This section of a switch statement is branched to if none of of the case expressions match the switch expression. a. else b. default c. case d. otherwise
b. default
!=
not equal to
true or false. a conditionally executed statement should be indented one level from the if clause
true
true or false. all lines in a conditionally excuted block should be indented one level.
true
true or false. the scope of a variable is limited to the block in which it is defined.
true
value of: !false
true
%d
format specifer; the d conversion character stands for decimal integer, and it can be used with arguments of the int, short, and long data types. must be used with integer values.
System.out.printf method
formats a string and displays it in the console window
Write an if-else statement that assigns 1 to x when y is equal to 100. Otherwise, it should assign 0 to x.
if ( y == 100) x = 1; else x = 0;
Write an if statement that multiples payRate by 1.5 if hours is greater than 40.
if (hours > 40) payRate *= 1.5;
Write an if statement that sets the variable fees to 50 if the boolean variable max is true.
if (max==true) { fees = 50; }
Write an if statement that displays "Goodbye" if the variable myCharacter contains the character 'D'.
if (myCharacter == 'D') System.out.println("Goodbye");
Assume the variables namel and name2 reference two different string objects containing different strings. Write code that displays the strings referenced by these variables in alphabetical order.
if (name1.compareTo(name2)<0) System.out.println(name1 + " comes before " + name2 + " alphabetically."); else System.out.println(name2 + " comes before " + name1 + " alphabetically.");
Modify the statement you wrote in response to Checkpoint 3.20 so it performs a case-insensitive comparison.
if (name1.compareToIgnoreCase(name2)<0) System.out.println(name1 + " comes before " + name2 + " alphabetically."); else System.out.println(name2 + " comes before " + name1 + " alphabetically.");
Write an if statement that assigns .2 to "commission" if "sales" is greater than or equal to 10,000.00.
if (sales >= 10000) commission = .2;
precedence of all operators
- (unary negation) ! * / % + - < > <= >= == != && || = += -= *= /= %=
What does the "if" statement do?
It is used to create a decision structure, which allows a program to have more than one path of execution. The "if" statement causes one or more statements to execute only when a "boolean" expression is "true".
How does the if-else statement execute?
It will execute one group of statements if its boolean expression is true, or another group if its boolean expression is false.
Assume the variable name references a String object. Write an if statement that displays "Do I know you?" if the String object contains "Timothy".
String name = Timothy if (name = Timothy) System.out.println("Do I know you?");
Assume the variables a = 2, b = 4, and c = 6. Indicate by circling the T or F if each of the following condition is true or false. a = = 4 || b > 2 T F 1 != b && c !=3 T F !(a > 2) T F
T T T
What will the following program display? int funny = 7, serious = 15; funny = serious * 2; switch (funny) { case 0 : System.out.println("That is funny."); break; case 30: System.out.println("That is serious."); break; case 32: System.out.println("That is seriously funny."); break; default: System.out.println(funny); }
That is serious.
What do logical operators do?
They connect tow or more relational expressions into one or reverse the logic of an expression.
If you cannot use relational operators to compare String objects, what should you use instead?
a String method
flag
a boolean variable that signals when some condition exists in the program
%f
a format specifier. the letter f indicates that floating-point value will be inserted into the string when it is displayed. must be used with floating-point values.
switch statement
a multiple alternative decision structure; lets the value of a variable or expression determine where the program will branch to; allows you t o test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute.
format specifier
a placeholder for a value that will be inserted into the string when it is displayed
&&, ||, and ! are _______. a. relational operators b. logical operators c. conditional operators d. ternary operators
b. logical operators
Explain why you cannot convert the following if-else statement into a switch statement. if (temp == 100) x = 0; else if (population > 1000) x = 1; else if (rate < .1) x = -1;
because the if-else-if statement tests several different conditions, consistent of different variables.
value of: false || false
false
you can use this method to display formatted output in a console window. a. Format.out.println b. Console.fomat c. System.out.printf d. System.out.formatted
c. System.out.printf
This is an empty statement that does nothing. a. missing statement b. virtual statement c. null statement d. conditional statement
c. null statement
this determines wheter two different string objects contain the same string. a. the == operator b. the = operator c. the equals method d. the stringCompare method
c. the equals method
the conditional operator takes this many operands. a. one b. two c. three d. four
c. three
%.2f
causes the value of the variable to be rounded to two decimal places
This type of expression has a value of either true or false a. binary expression b. decision expression c. unconditional expression d. boolean expression
d. boolean expression
To create a block of statements, you enclose the statements in these. a. parentheses() b. square brackets [] c. angled brackets <> d. braces {}
d. braces {}
relational operator
determines whether a specific relationship exists between two values
null statement
empty statement that does nothing becomes the conditionally executed statement
==
equal to
Value of: true && false
false
false && false
false
true or false. the = operator and the == operator perform the same operation
false
true or false. when an if statement is nested in the else clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true.
false
value of: !true
false
value of: false && true
false
Write an if/else statement that assigns 0.10 to commission unless sales is greater than or equal to 50000.0, in which case it assigns 0.2 to commission.
if (sales >= 50000.00) commisssionRate = 0.20; else commissionRate = 0.10;
Write an if statement that displays the message " The number is valid" if the variable speed is within the range 0 through 200.
if (speed > 0 && x < 200) System.out.println("The number is valid");
Write an if-else statement that assigns 20 to the variable y if the variable x is greater than 100. Otherwise, it should assign 0 to the variable y.
if (x > 100) y = 20; else y = 0;
Write an if statement that assigns 0 to x if y is equal to 20.
if (y == 20) x = 0;
short-circuit evaluation
if the expression on the left side of the && operator is false, the expression on the right side will not be checked. Because the entire expression is flase if only one of the subexpressions is false, it would waste CPU time to check the remaining expression. So, when the && operator finds that the expression on its left is false, it short-circuits and does not evaluate the expression on its right.
Write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.
if x > 100 then set y=20 set z=40 End if
Write nested if statements that perform the following test: If amount1 is greater than 10 and amount2 is less than 100, display the greater of the two.
if(amount1 > 10 && amount2 < 100){ if(amount1 > amount2){ System.out.println(amount1); } else{ System.out.println(amount2); } }
Write an if statement that displays the message "The number is invalid" if the variable speed is outside the range 0 through 200.
if(speed < 0 || speed > 200) System.out.println("The number is invalid");
Write code that tests the variable x to determine whether it is greater than 0. If x is greater than 0, the code should test the variable y to determine whether it is less than 20. If y is less than 20, the code should assign 1 to the variable z. If y is not less than 20, the code should assign 0 to the variable z.
import javax.swing.JOptionPane; public class jjjd { public static void main(String[] args) { int x=0; int y=0; int z=0; String input; input=JOptionPane.showInputDialog("Enter a number for x"); x=Integer.parseInt(input); if (x>0) { if (y<20) { z=1; } } else { z=0; } } }
conversion character
indicates the data type of the argument that is being formatted
&&
logical AND operator; connects two boolean expressions into one. Both expressions must be true for the overall expressions to be true.
!
logical NOT operator; The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.
||
logical OR operator; connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one.
decision structure
program executed only under certain circumstances
What is wrong with the following switch statement? //This code has errors! switch (temp) { case temp < 0 : System.out.println("Temp is negative."); break; case temp = 0 : System.out.println("Temp is zero."); break; case temp > 0 : System.out.println("Temp is positive."); break; }
the case statements must be followed by an integer constant, not a relational expression.
minimum field width
the minimum number of spaces that should be used to display the value
Purposes of flags
to display numbers with comma separators. to pad numbers with leading zeros. to left-justify numbers.
true or false. when an if statement is nested in the if clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true.
true
value of: false || true
true
value of: true || false
true
value of: true || true
true
sequence structure
type of code in which the statements are executed in a sequence, without branching off in another direction.
conditional operator
used to create short expressions that work like if-else statements
who likes to poop?
wes
lexicographical comparison
when you use the compareTo method to compare two strings, the strings are compared character by character.
String.format method
works like the System.out.printf method, except that it does not display the formatted string on the screen. Instead, it returns a reference to the formatted screen. You can assign the reference to a variable, and then use it later.