java test 2
loop body
A block of statements Executed repeatedly
empty body
A body with no statements Caused by misplaced semicolons
string
A class for working with fixed-string data Unchanging data composed of multiple characters StringBuilder and StringBuffer Classes for storing and manipulating changeable data composed of multiple characters
infinite loop
A loop that never ends Can result from a mistake in the while loop Do not write intentionally
literal string
A sequence of characters enclosed within double quotation marks is a _______.
event-controlled loop
Altered by user input Controlled by the user Executed any number of times
the OR operator
An action to occur when at least one of two conditions is true Written as || Sometimes called pipes
switch statement
An alternative to a series of nested if statements Test a single variable against a series of exact integer, character, or string values
the logical AND operator
An alternative to some nested if statements Used between two Boolean expressions to determine whether both are true Written as two ampersands ( && ) Include a complete Boolean expression on each side Both Boolean expressions that surround the operator must be true before the action in the statement can occur
do... while loop
As a pretest loop: Checks the value of the loop control variable before loop body As a posttest loop Checks the value of the loop control variable At the bottom of the loop After one repetition has occurred Performs a task at least one time You are never required to use this type of loop Use curly braces to block the statement Even with a single statement
Boolean value
Boolean values true and false values Used in every computer decision
compareTo() method
Compares two Strings and returns: Zero: If two Strings refer to the same value Negative number: If the calling object is "less than" the argument Positive number: If the calling object is "more than" the argument if (aWord.compareTo(anotherWord) < 0)
character class
Contains standard methods for testing the values of characters Methods that begin with "is" Such as isUpperCase() Return a Boolean value that can be used in comparison statements Methods that begin with "to" Such as toUpperCase() Return a character that has been converted to the stated form
indexOf() method
Determines whether a specific character occurs within a String Returns the position of the character The first position of a String is zero The return value is -1 if the character does not exist in the String
endsWith() method and startsWith() method
Each takes a String argument Return true or false if a String object does or does not end or start with the specified argument, respectively
validating data
Ensure a value falls within a specified range Use indefinite loops to validate input data If a user enters incorrect data, the loop repeats
equals() method
Evaluates the contents of two String objects to determine if they are equivalent Returns true if objects have identical contents public boolean equals(String s)
while loop
Executes a body of statements continually As long as the Boolean expression that controls entry into the loop continues to be true Consists of: The keyword while Followed by a Boolean expression within parentheses Followed by the body of the loop; can be a single statement or a block of statements surrounded by curly braces
short circuit evaluation
Expressions on each side of the logical operator are evaluated only as far as necessary Determine whether an expression is true or false
substring()method
Extracts part of a String Takes two integer arguments Start position End position The length of the extracted substring is the difference between the second integer and the first integer
case
Followed by one of the possible values for the test expression and a colon
break statements in the switch structure
If a break statement is omitted: The program finds a match for the test variable All statements within the switch statement execute from that point forward
A Boolean expression is evaluated as true or false
If the value of testExpression is true: The entire conditional expression takes on the value of the expression following the question mark If the value is false: The entire expression takes on the value of falseResult An advantage of using the conditional operator is the conciseness of the statement
equalsIgnoreCase() Method
Ignores case when determining if two Strings are equivalent Useful when users type responses to prompts in programs
Unconventional for loops
Initialization of more than one variable Place commas between separate statements Performance of more than one test using AND or OR operators Decrementing or performance of some other task Altering more than one value You can leave one or more portions of a for loop empty
priming read
Input retrieved before the loop is entered Within a loop, the last statement retrieves the next input value and checks the value before the next entrance of the loop
character
Instances hold a single character value Defines methods that can manipulate or inspect single-character data
decision making
Involves choosing among alternative courses of action Based on some value within a program All computer decisions are yes-or-no decisions
if/else
It is most efficient to ask a question most likely to be true first Avoids asking multiple questions Makes a sequence of decisions more efficient
concatenation
Join a simple variable to a String String aString = "My age is " + myAge; Use the + operator
case statement
No need to write code for each case Evaluate char variables Ignore whether it is uppercase or lowercase
toString()method
Not part of the String class Converts any object to a String Converts primitive data types to Strings String theString; int someInt = 4; theString = Integer.toString(someInt);
sequence structure
One step follows another unconditionally Cannot branch away or skip a step
default
Optionally is used prior to any action that should occur if the test variable does not match any case
break
Optionally terminates a switch statement at the end of each case
single-alternative if
Perform an action, or not Based on one alternative
if-else statement
Performs one action when a Boolean expression evaluates as true Performs a different action when a Boolean expression evaluates as false A statement that executes when if is true or false and ends with a semicolon Vertically align the keyword if with the keyword else Illegal to code else without if Depending on the evaluation of the Boolean expression following if, only one resulting action takes place
charAt() method
Requires an integer argument Indicates the position of the character that the method returns Example: myName.charAt(4) outputs "y" if myName=Stacy
conditional operator
Requires three expressions separated with a question mark and a colon testExpression ? trueResult : falseResult; smallerNum=(a < b)? a : b;
length() method
Returns the length of a String
switch
Starts the structure Followed by a test expression enclosed in parentheses
flowchart
Steps in diagram form A series of shapes connected by arrows Programmers use a variety of shapes for different tasks Rectangle to represent any unconditional step Diamond to represent any decision
prefix++
The result is calculated and stored Then the variable is use
if statement
The simplest statement to make a decision A Boolean expression appears within parentheses No space between the keyword if and the opening parenthesis Execution always continues to the next independent statement Use a double equal sign ( == ) to determine equivalency
postfix++
The variable is used Then the result is calculated and stored
regionMatches() method
Two variants that can be used to test if two String regions are equal A substring of the specified String object is compared to a substring of the other If the substrings contain the same character sequence, then the expression is true Otherwise, the expression is false A second version uses an additional boolean argument Determines whether case is ignored when comparing characters
Pseudocode
Use paper and a pencil Plan a program's logic by writing plain English statements Accomplish important steps in a given task Use everyday language
for loop
Used when a definite number of loop iterations is required One convenient statement indicates: The starting value for the loop control variable The test condition that controls loop entry The expression that alters the loop control variable
NOT operator
Written as an exclamation point ( ! ) Negates the result of any Boolean expression When preceded by the NOT operator, any expression evaluated as: true becomes false false becomes true
string
a ___ is a string each crated ___ is a class object it is a variable name not a simple data type
for
a concise format in which to execute loops
string variable
a named object of the string class
range check
a series of if statements that determine whether a value falls within a specified range Java programmers commonly place each else of a subsequent if on the same line
loop
a structure that allows repeated execution of a block of statements
reference
a variable that holds a memory address
incrementing
adding
==
compare 2 strings using the___ operator Not comparing values Comparing computer memory locations Compare contents of memory locations more frequently than memory locations themselves
class string
defined in java.lang.string automatically imported into every program
Use the for loop that contains no body (do-nothing loop) for(x = 0; x < 100000; ++x);
how do you pause a program ?
immutable
objects that cannot be changed, such as a string
iteration
one execution of any loop
indefinite loop
programmer cannot predict number of iterations
definite loop
programmer knows exact number of iterations
accumulating
repeatedly increasing a value by some amount
replace() method
replaces all occurrences of some characters within a string
length()method
returns the numerical length of the string
null string
se the null Java keyword Strings are set to null by default Cannot be used in String methods
decrementing
subtracting
nested if statement
tatements in which an if structure is contained inside another if structure Two conditions must be met before some action is taken Pay careful attention to the placement of else clauses else statements are always associated with if on a "first in-last out" basis
while loop
the loop controlling boolean expression is the 1st statement
do.. while loop
the loop-controlling boolean expression is the last statement
dual alternative if
two possible courses of action
They are convenient when several alternative courses of action depend on a single integer, character, or string value Use only when there is a reasonable number of specific matching values to be tested
why use switch statements