Chapter 5

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

descibe listing 5.2

An if-else statement is used to determine whether number of hours entered by user is greater than 40 -> If it is, extra hours are paid at rate one and a half times normal rate -> If there are no overtime hours, total payment based simply on number of hours worked and standard rate

what are equality operators what do they do give an code example something would get printed when total equals sum -> and one where a statement prints is they dont equal

"= =" and "!=" whether two values are equal or not equal if (total = = sum) System.out.println("total equals sum"); if (total != sum) System.out.println("total does NOT equal sum");

what's the difference between character and a character string

"char" is a primitive value that represents one character character string is represented as an object in Java, defined by "String" class

if (generator.nextInt(CHANCE) == 0) System.out.println("You are a randomly selected winner!"); describe this *CHANCE contains 20

"generator" refers to an object of the "Random" class -> so "if" statement examines value returned from a call to nextInt to determine a random winner -> odds of code picking a winner based on value of "CHANCE" constant (1 in 20 odds) -> condition looking for a return value of 0 is arbitrary -> any value between 0 and "CHANCE-1" would have worked

describe listing 5.8

"while" loop used to (validate the input) - means guarantee that the user enters a value that is considered to be valid

what are loops what does it share with conditionals

(also called repetition statements) allows us to execute a programming statement over and over again based on a boolean expression that determines how many times statement is executed

what is a conditional statement what are they types in java

(also called selection statements) allows us to choose which statement will be executed next "if" statement, "if-else" statement, and "switch" statement

what are the parts of an "if" statement what does it do

-"if" -boolean expression next -statement next If condition is true, statement executed and processing continues with next statement If condition false, statement is skipped and processing continues immediately with next statement

what are the types of loop statements

-"while" statement -"do" statement, "for" statement

what should be noted about the Unicode character set

0 - 9 are contiguous and in alphabetical order -> same for uppercase -> all lowercase alphabetic characters ('a' through 'z') then *order of what proceed first to last

if (height <= MAX) adjustment = 0; else adjustment = MAX - height; describe this

If condition is true, first assignment statement executed -> if condition is false, second assignment statement executed (only one can be executed)

if (code == 'R') if (height <= 20) System.out.println("Situation Normal"); else System.out.println("Bravo!"); what should be noted here

It may seem that an else clause after a nested if could apply to either if statement -> indentation in this example implies that it is part of the inner if statement -> else clause is always matched to the closest unmatched if that preceded it

describe listing 5.12 what happens to "ActionEvent" object

The two buttons use the same method as their event handler -> Whenever either button is pressed, "processColorButton" method is called -> It uses an if statement to check which button generated the event -> If Red button, background color of pane is set to red -> Otherwise, Blue button, so background color is set to blue variables representing the two buttons and the pane are declared as instance data at class level so that they can be accessed in both the "start" method and the event handler method

what is special about floating point data in booleans

Two floating point values are equal, according to == operator, only if all binary digits of underlying representations match If compared values are results of computation, it may be unlikely that they are exactly equal even if they are close enough for specific situation -> rarely use == with floating point data

"&&" operator performs what -> what does this mean what is it basically -> whats what

a logical AND operation -> result is true if both operands are true, but false otherwise binary -> uses two operands so 4 possible outcomes

what should be noted about equality and relational operators in mathematics

arithmetic operations are evaluated first, followed by equality and relational operations

if (total > amount) total = total + (amount + 1); describe its format

assignment statement indented under header line of "if" statement -> communicates that assignment statement is part of "if" statement -> implies that "if" statement governs whether assignment statement will be executed (the indentation is ignored by compiler)

what do we want from out programs

be (robust) - means they handle potential problems as elegantly as possible

double num = 1.0; while (num != 0.0) // infinite loop num = num− 0.1; why does this produce an infinite loop

because num will never have a value exactly equal to 0.0 -> its a floating number and never completely equals to the set floating number of 0.0

decisions in conditional statements based on what -> describe allows what give an example of that thing

boolean expression (also called condition) -> expression that evaluates to either true or false result of the expression determines which statement is executed next if (count > 20) System.out.println("Count exceeded");

how does flow of control alter how does that thing happen

by invoking a method method called -> control jumps to code defined for that method -> method completes -> control returns to place in calling method where invocation was made -> processing continues

give an example of where youd use a loop

calculate gpa of every student in a class -> calculation same for each student -> performed on different data -> loop done till no more students seen

where can block statements be used give example

can be used anywhere a single statement is called for in Java syntax if (boxes != warehouse.getCount()) { System.out.println("Inventory and warehouse do NOT match."); System.out.println("Beginning inventory process again!"); boxes = 0; } else { System.out.println("Inventory and warehouse MATCH."); warehouse.ship(); } *both if and else are block statements here

if (size >= MAX) size = 0; describe this

causes variable "size" to be set to zero if current value greater than or equal to value in constant "MAX"

what does it mean to say one character is less than another give example

characters in Java are based on Unicode character set, which defines an ordering of all possible characters that can be used ex. a comes before b in character set so a less than b

what is a block statement

collection of statements enclosed in braces -> replaces a single statement

what is lexicographic ordering

comparing characters and strings based on Unicode character set

whats a better way to check for floating point equality

compute absolute value of difference between the two values and compare result to some tolerance level (must be less than tolerance level) ex: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println("Essentially equal.");

if (count > 20) System.out.println("Count exceeded"); describe this code

condition is "count > 20" -> expression evaluates to a boolean (true or false) result -> Either value stored in count is greater than 20 or not if it is then println statement executed if not then "println" skipped and processing continues to next statement for execution

describe listing 5.9

contains two loops, one inside the other -> outer loop controls how many strings are tested, and inner loop scans through each string, character by character, until it determines whether string is a palindrome variables "left" and "right" store indexes of two characters -> initially indicate characters on either end of the string -> Each iteration of inner loop compares the two characters indicated by left and right fall out of inner loop when either characters don't match, meaning string is not a palindrome, or when the value of left becomes equal to or greater than the value of right, which means the entire string has been tested and it is a palindrome

describe listing 5.7

designate zero to be a (sentinel value) that indicates the end of the input variable called "sum" is used to maintain a running sum, which means it is sum of the values entered thus far -> "sum" is initialized to zero, and each value read is added to and stored back into "sum" sentinel value is not counted when counting the numbers to make average -> if statement at the end of the program avoids a divide-by-zero error

what should be noted about "String" objects show an example

dont use equality or relational operators -> "String" class has method called "equals" that returns a "boolean" value thats true if the two strings have same characters (false otherwise) -> called "compareTo" method if (name1.equals(name2)) System.out.println("The names are the same."); else System.out.println("The names are not the same."); *could switch name1 and name2 -> same result

if (numBooks < stackCount + inventoryCount + duplicateCount) reorder = true;

first adds three values together, then compares result to value stored in numBooks -> numBooks less than other three values combined, then boolean variable reorder is set to true -> addition operations performed before less than operator, because arithmetic operators have higher precedence than relational operators

what is the "!" operator also called give an example of what it would produce what is "!" basically

logical component boolean variable called found has the value false, then "!found" is true unary since one true or false is produced

what another way to form a else statement match to nested "if"s -> use previous example of: if (code == 'R') if (height <= 20) System.out.println("Situation Normal"); else System.out.println("Bravo!"); make bravo react to first if

if (code == 'R') { if (height <= 20) System.out.println("Situation Normal"); } else System.out.println("Bravo!");

give an example of short circuiting

if (count != 0 && total/count > MAX) System.out.println("Testing."); "if" statement will not attempt to divide by zero if left operand false -> If count has value zero, left side of "&&" operation is false -> therefore, whole expression is false and right side not evaluated

what happens when "else" is involved with "if" statement allows what

if-else statement is made allows one this to happen if "if" statement is true and another thing to happen if "if" statement is false

program execution goes how how to java applications begin executions

in a linear fashion -> running program starts at first programming statement and moves down one statement at a time until program is complete with first line of main method and proceeds step by step until it gets to end of main method

what wrong with this code if (depth >= UPPER_LIMIT) delta = 100; else System.out.println("WARNING: Delta is being reset to ZERO"); delta = 0; // not part of the else clause!

indentation implies that variable delta is reset to zero only when depth is less than UPPER_LIMIT -> However, without using a block, assignment statement that resets delta to zero is not governed by if-else statement at all -> It is executed in either case, which is clearly not what is intended

describe listing 5.3

instantiates a "Coin" object, flips coin by calling "flip" method, then uses an "if-else" statement to determine which of two sentences gets printed based on the result

why is the "compareTo" method better than the "equals" method what should be noted describe an example

instead of returning a "boolean" value, "compareTo" method returns an integer -return value is negative if "String" object through which the method is invoked precedes (is less than) string that is passed in as a parameter -return value is zero if the two strings contain the same characters -return value positive if "String" object through which the method is invoked follows (is greater than) the string that is passed in as a parameter int result = name1.compareTo(name2); if (result < 0) System.out.println(name1 + " comes before " + name2); else if (result == 0) System.out.println("The names are equal."); else System.out.println(name1 + " follows " + name2);

write a code or a while loop where loop prints values 1-5

int count = 1; while (count < = 5) { System.out.println(count); count++; } Each iteration through loop prints one value, then increments counter

what would name1 == name2 cause

its valid but actually tests to see whether both reference variables refer to the same "String" object because == operator tests whether both reference variables are aliases of each other (whether they contain the same address)

what are relational operators what are the types in java

let us decide relative ordering between values -greater than (>) -less than (<) -greater than or equal to (>=) -less than or equal to (<=)

what does a "continue" statement do what should be noted

like "break" statement but loop condition is evaluated again, and the loop body is executed again if it is still true like "break" statement, it can be avoided in a loop and should be

for logical operators, what has the highest precedence and followed by what

logical NOT > logical AND > and then logical OR

"||" operator performs what -> means what is it basically -> whats what

logical OR operator -> true if one or the other or both operands are true, but false otherwise binary -> uses two operands so 4 possible outcomes

what is a while statement what should be noted

loop that evaluates a boolean condition just as an "if" statement does and executes a statement (called the body of the loop) if condition is true unlike "if" statement after body is executed, condition is evaluated again -> if still true body executed again -> stops once condition is false and processing continues with statement after body of the while loop

flow of control is what

order in which statements are executed in a running program

How do you interrupt an infinite loop

pressing the Control-C

what are logical operators what are they types and what do they do

produce boolean results and also take boolean operands

what does an "if" statement allow

program to choose whether to execute a particular statement

describe listing 5.1

reads age of user and then makes a decision as to whether to print a particular sentence based on age that is entered age less than value of constant MINOR, the statement about youth is printed -> If age equal to or greater than the value of MINOR, "println" statement skipped -> In either case, final sentence about age being a state of mind printed

what are the mechanics of the "while" loop give an ex of "while" statement

repeatedly executes specified Statement as long as boolean Expression is true -> Expression evaluated first -> so Statement might not be executed at all -> Expression is evaluated again after each execution of Statement until Expression becomes false while (total > max) { total = total / 2; System.out.println("Current total: " + total); }

what happens to "ActionEvent" object

represents the event and always is passed into the event handler method

describe listing 5.6

statement executed as the result of an if statement could be another if statement -> known as nested if -> allows making another decision after determining the results of a previous decision

what something special about string objects

string literal (like "Nathan") is convenient and actually a shorthand technique for creating a String object Java creates a unique object for string literals only when needed -> if string literal "Hi" is used multiple times in a method, only one String object is created to represent it that would mean something like this would be true String str = "software"; if (str == "software") System.out.println("References are the same"); if (str.equals("software")) System.out.println("Characters are the same");

what is a palindrome

string of characters that reads the same forward or backward

what is special about "&&" and "||" how does this happen with each

they are "short-circuited" -> if left operand sufficient to decide boolean result of operation -> right operand is not evaluated if "&&" false -> result of operation will be false no matter what value of right operand is if "||" true -> result of operation is true no matter what value of right operand is

int count1, count2; count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 50) { System.out.println("Here again"); count2++; } count1++; } whats noted here

this is a nested loop outer loop executes 10 times, as count1 iterates between 1 and 10 -> inner loop executes 50 times, as count2 iterates between 1 and 50 -> For each iteration of outer loop, inner loop executes completely -> println statement is executed 500 times

describe listing 5.4

this is the "Coin" class -> stores two integer constants ("HEADS" and "TAILS") that represent two possible states of the coin, and an instance variable called "face" that represents the current state of the coin -> "Coin" constructor initially flips coin by calling "flip" method, which determines new state of coin by randomly choosing a number (either 0 or 1) "isHeads" method returns "boolean" value based on current face value of coin -> "toString" method uses an "if-else" statement to determine which character string to return to describe coin -> "toString" method automatically called when "myCoin" object is passed to "println" in "main" method

Java has what that affect the processing of conditionals and loops

two statements -"break" -"continue"

alternating flow of control through code in a method can be done how

two types of statements can do this -conditionals -loops

boolean expressions on which conditionals and loops are based on what

use equality operators, relational operators, and logical operators to make decisions

what one way to find out is a character is greater or lesser than another

use the equality and relational operators on character data ex. if (ch1 > ch2) System.out.println(ch1 + " is greater than " + ch2); else System.out.println(ch1 + " is NOT greater than " + ch2);

describe listing 5.5

uses an "if-else" statement in which the statement of the "else" clause is a block statement if block braces were not used, sentence stating that the answer is incorrect would be printed if the answer was wrong, but sentence revealing correct answer would be printed in all cases -> That is, only first statement would be considered part of "else" clause

what is an infinite loop what should be note give an example of one

when a loop body executes forever, or at least until the program is interrupted this is a mistake -> want condition of the loop to be false at some point int count = 1; while (count <= 25) // Warning: this is an infinite loop! { System.out.println(count); count = count - 1; }

what does "break" statement do what would happen if done in the body of a loop what should be noted about "break" statements

when executed flow of execution transfers immediately to the statement after the one governing the current flow execution of loop is stopped and statement following the loop is executed, breaking out of the loop Because "break" statement causes program flow to jump from one place to another, using it isnt good practice

what is a nested loop what should be noted

when the body of a loop has another loop for each iteration of outer loop, the inner loop executes completely


संबंधित स्टडी सेट्स

multiple choice ch.19,20,21,22,23,

View Set

Estate Planning: Forms of Property Ownership (Module 2)

View Set

Chapter 5: The American Revolution, 1776-1783

View Set

CH. 3 and 4 jurisdiction practice questions

View Set

Biology- Gene Expression and Inheritance

View Set

Unit 8 - The Securities Exchange Act of 1934 and the Secondary Market Quiz/Test Questions

View Set

Arrhenius, Bronsted-Lowry, and Lewis Acids and Bases Assignment

View Set