Ch. 3 Decision Structures

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

true && true

true

conditionally executed

action performed only when a certain condition exists

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;

general syntax for writing a format specifier is:

%[flags][width][.precision]conversion

value of: false || false

false

==

equal to

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.

!=

not equal to

value of: false || true

true

logical operators in order of precedence?

! && ||

precedence of all operators

- (unary negation) ! * / % + - < > <= >= == != && || = += -= *= /= %=

What do logical operators do?

They connect tow or more relational expressions into one or reverse the logic of an expression.

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.

false && false

false

value of: !true

false

value of: false && true

false

null statement

empty statement that does nothing becomes the conditionally executed statement

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

Value of: true && false

false

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.

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.

%.2f

causes the value of the variable to be rounded to two decimal places

relational operator

determines whether a specific relationship exists between two values

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;

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; } } }

!

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.

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

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.


Set pelajaran terkait

AP Chemistry Chapter 6 + 16 Test

View Set

Chapter 57 practice questions- PrepU Drugs Affecting GI Secretions

View Set

Chapter 70: Management of Patients With Neurologic Trauma NCLEX

View Set

Chapter 7: 2 Body Planes, Directions, and Cavities

View Set

Macroeconomics Chapter 16 (Final Exam) HSU

View Set

NUR 303 - Chapter 44: Assessment of Digestive and Gastrointestinal Function

View Set