ch2
If the dividend and divisor in floating-point division are both 0, the division results in what?
"not a number" (NaN) indicates an unrepresentable or undefined value. Printing a floating-point variable that is not a number outputs NaN.
Scanner
A Scanner is a text parser that can get numbers, words, or phrases from an input source such as the keyboard. Getting input is achieved by first creating a Scanner object via the statement: Scanner scnr = new Scanner(System.in);. System.in corresponds to keyboard input. Then, given Scanner object scnr, the following statement gets an input value and assigns x with that value: x = scnr.nextInt() ;
object
A String variable is a reference type (discussed in depth elsewhere) variable that refers to a String object. An object consists of some internal data items plus operations that can be performed on that data. Ex: String movieTitle = "Frozen"; declares a String reference variable named movieTitle that refers to a String object. That String object stores the string "Frozen".
character literal
A character literal is surrounded with single quotes, as in myChar = 'm';
compiler warning
A compiler will sometimes report a warning, which doesn't stop the compiler from creating an executable program but indicates a possible logic error. Ex: Some compilers will report a warning like "Warning, dividing by 0 is not defined" if encountering code like: totalItems = numItems / 0 (running that program does result in a runtime error). Even though the compiler may create an executable program, good practice is to write programs that compile without warnings. In fact, many programmers recommend the good practice of configuring compilers to print even more warnings. For example, javac can be run as javac -Xlint yourfile.java.
divide-by-zero error
A divide-by-zero error occurs at runtime if a divisor is 0, causing a program to terminate. A divide-by-zero error is an example of a runtime error.
double
A double variable stores a floating-point number. Ex: double milesTravel; declares a double variable.
floating-point literal
A floating-point literal is a number with a fractional part, even if the fraction is 0, as in 1.0, 0.0, or 99.573. Good practice is to always have a digit before the decimal point, as in 0.5, since .5 might mistakenly be viewed as 5.
logic error
A logic error, also called a bug, is an error that occurs while a program runs. For example, a programmer might mean to type numBeans * numJars but accidentally types numBeans + numJars (+ instead of *). The program would compile but would not run as intended.
method
A method is a list of statements executed by invoking the method's name, such invoking known as a method call. Any method input values, or arguments, appear within ( ), separated by commas if more than one.
identifier
A name created by a programmer for an item like a variable or method is called an identifier. An identifier must: -be a sequence of letters (a-z, A-Z), underscore (_), dollar signs ($), and digits (0-9) -start with a letter, underscore, or dollar sign Identifiers are case sensitive
\n
A new output line can also be produced by inserting \n, known as a newline character, within a string literal. Ex: Outputting "1\n2\n3" outputs each number on its own output line
A character is internally stored as what?
A number. When outputting a char variable, the compiler converts the number to the appropriate letter.
How can a programmer compare if two strings are equal?
A programmer can compare two strings using the notation str1.equals(str2). The equals method returns true if the two strings are equal.
How do you output multiple character variables with one output statement?
A programmer can output multiple character variables with one statement as follows: System.out.print("" + c1 + c2);. The initial "" tells the compiler to output a string of characters, and the +'s combine the subsequent characters into such a string. Without the "", the compiler will simply add the numerical values of c1 and c2, and output the resulting sum.
How does a programmer compare strings relationally?
A programmer compares strings relationally using the notation str1.compareTo(str2). compareTo() returns values as follows. str1 less than str2 returns a negative number str1.compareTo(str2) < 0. str1 equal to str2 returns zero str1.compareTo(str2) == 0 str1 greater than str2 returns positive number str1.compareTo(str2) > 0 A common error is to forget that case matters in a string comparison. A programmer can compare strings while ignoring case using str1.equalsIgnoreCase(str2) and str1.compareToIgnoreCase(str2).
reserved word
A reserved word is a word that is part of the language, like int, short, or double. A reserved word is also known as a keyword. A programmer cannot use a reserved word as an identifier. Many language editors will automatically color a program's reserved words.
runtime error
A severe error that occurs at runtime and causes a program to terminate early. A divide-by-zero error is an example of a runtime error.
Switch statement
A switch statement can more clearly represent multi-branch behavior involving a variable being compared to constant values. The program executes the first case whose constant expression matches the value of the switch expression, executes that case's statements, and then jumps to the end. If no case matches, then the default case statements are executed.
escape sequence
A two-character sequence starting with \ that represents a special character. Ex: '\n' represents a newline character. \n newline \t tab \' single quote \" double quote \\ back slash
type cast
A type cast explicitly converts a value of one type to another type. A programmer can precede an expression with (type) to convert the expression's value to the indicated type. Ex: If myIntVar is 7, then (double)myIntVar converts int 7 to double 7.0. A common type cast converts a double to an int. Ex: myInt = (int)myDouble. The fractional part is truncated. Ex: 9.5 becomes 9.
type conversion
A type conversion is a conversion of one data type to another, such as an int to a double. The compiler automatically performs several common conversions between int and double types, such automatic conversion known as implicit conversion. For an arithmetic operator like + or *, if either operand is a double, the other is automatically converted to double, and then a floating-point operation is performed. For assignments, the right side type is converted to the left side type if the conversion is possible without loss of precision.
char
A variable of char type, as in char myChar;, can store a single character like the letter m.
Constant variable
An initialized variable whose value cannot change is called a constant variable. A constant variable is also known as a final variable. A common convention, or good practice, is to name constant variables using upper case letters with words separated by underscores, to make constant variables clearly visible in code. Ex: final double SPEED_OF_SOUND = 761.207;
overflow
An overflow occurs when the value being assigned to a variable is greater than the maximum value the variable can store.
compile-time error
Because a syntax error is detected by the compiler, a syntax error is known as a type of compile-time error.
data type: boolean
Boolean refers to a quantity that has only two possible values, true or false. Java has the built-in data type boolean for representing Boolean quantities.
What are the qualities of an equal string?
Equal strings have the same number of characters, and each corresponding character is identical.
Example structure of a switch statement
Example of a switch structure: switch (expression) { case constant Expr1: // Statements break; case constant Expr2: // Statements break; ... default: // If no other case matches // Statements break; }
T or F: You can use equality operators to compare strings.
False. You compare strings with a different method, discussed elsewhere. See another section discussing string comparison methods equals() and compareTo().
bitwise operators
For integral operand types such as int, & and | represent bitwise operators, which perform AND or OR on corresponding individual bits of the operands. Bitwise operators have highly-specialized usage, not common in beginning programs and not discussed here.
Precedence rules (order of evaluation rules)
Items within parantheses first. Logical not is next. Arithmetic operators (using their own precedence rules too) Relational operators Equality and inequality operators Logical AND Logical OR
Difference between next() and nextLine()
Mixing next() and nextLine() can be tricky, because next() leaves the newline (newline being leading whitespace) in the input, while nextLine() does not skip leading whitespace.
Does java have a method of getting one character from input?
No. Java does not have a method for getting one character from input. Instead, the following sequence can be used: myChar = scnr.next().charAt(0); The charAt(0) is explained in another section. Briefly, next() gets the next sequence of non-whitespace characters (as a string), and charAt(0) gets the first character in that string.
Omitting break statement causes what
Omitting the break statement for a case will cause the statements within the next case to be executed. Such "falling through" to the next case can be useful when multiple cases, such as cases 0, 1, and 2, should execute the same statements.
Outputting a variable's value is achieved via:
System.out.print(x);
System.out.println
System.out.println (note the ln at the end, short for "line"), starts a new output line after the outputted values, called a newline.
string literal
Text in double quotes " " is known as a string literal.
How to generate a random number
The Random class provides methods that return a random integer in the range (−2^31) to (2^31) − 1 or a programmer-defined range. The statement import java.util.Random; enables use of the Random class. The statement Random randGen = new Random(); creates a new random number generator object named randGen. The method call randGen.nextInt() can then be used to get a random integer ranging from (−2^31) to (2^31) − 1.
System.out.print
The System.out.print construct supports output. Outputting text is achieved via: System.out.print("desired text");
Explain why the integers generated by a Random object are known as pseudo-random
The integers generated by a Random object are known as pseudo-random. "Pseudo" means "not actually, but having the appearance of". Internally, the nextInt() method has an equation to compute the next "random" integer from the previous one, (invisibly) keeping track of the previous one. For the first call to nextInt(), no previous random integer exists, so the function uses an integer known as the seed. Random() seeds the pseudo-random number generator with a number based on the current time. Since, the time is different for each program run, the program will get a unique sequence. A programmer can specify the seed when the Random object is created, as in Random randGen = new Random(5); or using the setSeed() method, as in randGen.setSeed(5); With a specific seed, each program run will yield the same sequence of pseudo-random numbers.
Modulo operator (%)
The modulo operator (%) evaluates the remainder of the division of two integer operands. Examples: 24 % 10 is 4. Reason: 24 / 10 is 2 with remainder 4. 50 % 50 is 0. Reason: 50 / 50 is 1 with remainder 0. 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1.
How to output floating-point number with a set amount of digits?
The syntax for outputting the double myFloat with two digits after the decimal point isSystem.out.printf("%.2f", myFloat); When outputting a certain number of digits after the decimal using printf(), Java rounds the last output digit, but the floating-point value remains the same.
T or F: The / operator performs floating-point division if at least one operand is a floating-point type.
True. The / operator will perform floating-point division if at least one operand is a floating-point type.
T or F: The Math class is part of Java's standard language package?
True. The Math class is part of Java's standard language package, so no import is required. A programmer must precede the method name with Math. to call a Math class method. Below, the method Math.sqrt() is called with one argument, areaSquare. The method call evaluates to a value, as in Math.sqrt(areaSquare) below evaluating to 7.0, with which sideSquare is assigned. Ex: sideSquare = Math.sqrt(areaSquare);
Integer Divison
When the operands of / are integers, the operator performs integer division, which does not generate any fraction. The / operator performs floating-point division if at least one operand is a floating-point type.
floating-point number
floating-point number is a real number containing a decimal point that can appear anywhere (or "float") in the number. Ex: 98.6, 0.0001, or -55.667.
The following code at the top of a file enables the program to get input:
import java.util.Scanner;
Dividing by a nonzero floating-point number by zero results in what?
infinity or -infinity, depending on the signs of the operands. Printing a floating-point variable that holds infinity or -infinity outputs Infinity or -Infinity.
syntax error
is to violate a programming language's rules on how symbols can be combined to create a program. An example is forgetting to end a statement with a semicolon. A compiler generates a message when encountering a syntax error.
data type: long
long is used for integers expected to exceed about 2 billion.