Java Chapter 3

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

This type of expression has a value of either true or false.

boolean expression

Write an if statement that multiplies payRate by 1.5 if hours is greater than 40.

if (hours > 40) payRate *= 1.5;

This determines whether two different String objects contain the same string

The stringCompare method

What risk does a programmer take when not placing a trailing else at the end of an if-else-if statement?

There is a chance that the program will not compile because Java is looking for other factors that may be possible. No code executed.

true || false

true

1 − (unary negation) !

Unary negation, logical NOT

When determining whether a number is inside a range, it's best to use this operator.

&&

if (temp > 45) population = base * 10; else population = base * 2;

population = temp > 45 ? base * 10 : base * 2;

True or False: A conditionally executed statement should be indented one level from the if clause.

true

false || true

true

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.

1 1

!true

false

if (x > y) z = 1; else z = 20;

z = x > y ? 1 : 20;

How does the character 'A' compare to the character 'B'?

'A' is less than 'B'

Write an if statement that assigns 0 to x when y is equal to 20.

if (y == 20) x = 0;

6 &&

Logical AND

8 = += −= *= /= %=

Assignment and combined assignment

7 ||

Logical OR

Explain the purpose of a flag variable. Of what data type should a flag variable be?

A flag is a boolean variable that signals when some condition exists in the program. When the flag variable is set to false, it indicates the condition does not yet exist. When the flag variable is set to true, it means the condition does exist.

3 + −

Addition, subtraction

Why is it good advice to indent all the statements inside a set of braces?

By indenting the conditionally executed statements, you are causing them to stand out visually. This is so you can tell at a glance what part of the program the if statement executes.

Explain what is meant by the phrase "conditionally executed."

Conditionally executed code is executed only under a condition, such as an expression being true.

5 == !=

Equal to, not equal to

2 * / %

Multiplication, division, modulus

You can use this method to display formatted output in a console window.

System.out.printf

if (result >= 0) System.out.println("The result is positive."); else System.out.println("The result is negative.");

System.out.println(result >=0 ? "The result is positive" : "The result is negative");

When does a constructor execute? What is its purpose?

The object memory is allocated, the field variables with initial values are initialized, and then the constructor is called, but its code is executed after the constructor code of the object super class.

An else clause always goes with __________.

The previous if clause that doesn't already have its own else clause.

Briefly describe how the || operator works.

The program must fit either case in the || operator in order for the code to complete.

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.0) commission = 0.2; else commission = 0.1;

This is an empty statement that does nothing.

null statement

>, <, and == are __________.

relational operators

True or False: The scope of a variable is limited to the block in which it is defined.

true

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

true && true

true

true || true

true

if (hours > 40) wages *= 1.5; else wages *= 1;

wages = hours > 40 ? wages * 1.5 : wages * 1;

Briefly describe how the && operator works.

It takes two boolean expressions as operands and creates a boolean expression that is true only when both subexpressions are true

What happens when you compare two String objects with the == operator?

It will only evaluate it as truer vecause == is used for primitive operators such as boolean, int, and double.

4 < > <= >=

Less than, greater than, less than or equal to, greater than or equal to

Explain why a misplaced semicolon can cause an if statement to operate incorrectly.

The semicolon indicates the end of a statement. if you end a statement early, or end something that is not a statement, java will not understand what you are trying to do.

Why are the relational operators called "relational"?

They determine whether a specific relationship exists between two values. The relationships are greater-than, less-than, equal-to, not equal-to, greater-than or equal-to, and less-than or equal-to.

To create a block of statements, you enclose the statements in these.

curly braces {}

The if statement is an example of a __________.

decision structure

This section of a switch statement is branched to if none of the case expressions match the switch expression.

default

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

false && false

false

false && true

false

false || false

false

true && false

false

This is a boolean variable that signals when some condition exists in the program.

flag

Write an if statement that assigns 0.2 to commission if sales is greater than or equal to 10000.

if (sales >= 10000) commission = 0.2;

Write an if statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10.

if (a < 10) { b = 0; c = 1; }

Write an if-else statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10. Otherwise, it should assign -99 to the variable b and assign 0 to the variable c.

if (a < 10) { b = 0; c = 1; } else { b = -99; c = 0; }

Write an if statement that sets the variable fees to 50 if the boolean variable max is true.

if (max) 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 variable name references a String object. Write an if statement that displays "Do I know you?" if the String object contains "Timothy".

if (name.equals("Timothy")) System.out.println("Do I know you?");

Modify the statement you wrote in response to Checkpoint 3.20 so it performs a case-insensitive comparison.

if (name.equalsIgnoreCase("Timothy")) System.out.println("Do I know you?");

Assume the variables name1 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 + " " + name2); else System.out.println(name2 + " " + name1);

Write an if statement that displays the message "The number is not valid" if the variable speed is outside the range 0 through 200.

if (speed < 0 || speed > 200) System.out.println("The number is not valid");

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 && speed <= 200) System.out.println("The number is valid");

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.

if (x > 0) { if (y < 20) { z = 1; } else { z = 0; } }

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 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.

if (x > 100) { y = 20; z = 40; }

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;

!false

true

&&, ||, and ! are __________.

logical operators

This is an if statement that appears inside another if statement.

nested if statement

True or False: All lines in a conditionally executed block should be indented one level.

true

The conditional operator takes this many operands.

three


Ensembles d'études connexes

Chapter 13: Psychosocial problems

View Set

Institutional structure of the EU

View Set

National Topic Tester - Property Ownership

View Set

Use of English - CAE part 1 . - How to concentrate - 4. 12. 2016 - Hanka

View Set

Surface Area/ Volume Ratio Limits Cell Size

View Set

Experiment 12: Dehydration of Cyclohexanol

View Set