Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
111 Cards in this Set
- Front
- Back
algorithm
|
describes how a problem is solved in terms of the actions to be executed and the order of their execution
help the programmer plan a program before writing it in a programming language described in natural languages or in pseudocode |
|
pseudocode
|
natural languages mixed with programming code
|
|
variable
|
designates a location in memory for storing data and computational results in the program
has a name that can be used to access the memory location every variable has a name, a type, a size, and a value |
|
descriptive names
|
rather than 'x' and 'y' -->
use 'radius' for radius and 'area' for area to let the compiler know what 'radius' and 'area' are, specify their data types |
|
floating-point number
|
numbers with a decimal point
|
|
primitive data types
(fundamental types) |
simple data types provided by Java for representing integers, floating-point numbers, characters, and Boolean types
|
|
double
|
indicates that the variable is a double-precision floating-point value stored in the computer
|
|
System.out.println
|
the method to display the final product in the console
|
|
variable declarations
declaring variables |
tells the compiler to allocate appropriate memory space for the variable based on its data type
the syntax for declaring a variable is --> datatype variableName; a variable MUSt be declared before can be assigned a value. A variable declared in a method MUST be assigned a value before it can be used. examples: double radius; // Declare radius double area; // Declare area Whenever possible, declare a variable and assign its initial value in one step. This will make the program easy to read and avoid programming errors. |
|
variable declaration:
int count; |
// Declare count to be an integer variable;
|
|
variable declaration:
double radius; |
// Declare radius to be a double variable;
|
|
variable declaration:
double interestRate; |
// Declare interestRate to be a double variable;
|
|
declaring variables of the same type
|
if variables are of the same type, they can be declared together, as follows:
datatype variable1, variable2, ..., variablen; the variables are separated by commas. for example, int i, j, k; // Declare i, j, and k as int varaibles |
|
assign value
|
the value of a variable is not defined until you assign a value
example: radius = 20; // New value is radius area = radius * radius * 3.141592; |
|
concatenating strings
|
Done so with signs (+)
|
|
string concatenation operator
|
combines two strings if two operands are strings
if one of the operands is a nonstring (e.d., a number), the nonstring value is converted into a string and concatenated with the other string |
|
breaking a long string
|
A string constants cannot cross lines in the source code. Thus the following statement would result in a compile error.
System.out.println ("Introduction to Java Programming, by Y. Daniel Liang"); To fix the error, break the string into separate substrings, and use the concatenation operator (=) to combine them: System.out.println("Introduciton to Java Programming, " + "by Y Daniel Liang"); |
|
incremental development and testing
|
It is a good approach to develop and test steps incrementally by adding them one at a time
|
|
System.out
|
default - the output device is the display monitor
|
|
System.in
|
default - the input device is the keyboard
|
|
println
|
the method to display a primitive value or a string to the console
|
|
new Scanner(System.in)
|
creates an object of the Scanner type
console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in with --> Scanner input = new Scanner(System.in); |
|
Scanner input = newScanner(System.in)
|
creates a 'Scanner' object and assigns its reference to the variable 'input'
|
|
invoke method on an object
|
to perform a task
|
|
Method:
nextByte() |
Description:
reads an integer of the 'byte' type |
|
Method:
nextShort() |
Description:
reads an integer of the 'short' type |
|
Method:
nextInt=() |
Description:
reads an integer of the 'int' type |
|
Method:
nextLong() |
Description:
reads an integer of the 'long' type |
|
Method:
nextFloat() |
Description:
reads a number of the 'float' type |
|
Method:
nextDouble() |
Description:
reads a number of the 'double' type |
|
Method:
next() |
Description:
reads a string that ends before a whitespace charachter |
|
Method:
nextLine() |
Description:
reads a line of text (i.e., a string ending with the 'Enter' key pressed) |
|
nextDouble()
|
for now, we'll see how to read a number that includes a decimal point by invoking the nextDouble() method
|
|
import class
|
import java.util.Scanner; // Scanner is in the java.util package
|
|
create a Scanner
|
Scanner input = new Scanner(System.in);
|
|
read a double
|
double radius = input.nextDouble()
|
|
print vs. println
|
the 'print' method is identical to the 'println' method except that 'println' moves the cursor to the next line after displaying the string, but 'print' does not advance the cursor to the next line when completed
|
|
identifiers
|
the names of things that appear in the program
identifiers are for naming variables, constants, methods, classes, and packages descriptive identifiers make program easy to read ex: 'ComputeAverage', 'main', 'input', 'number1', 'number2', 'number3' |
|
identifier naming rules
|
An identifier is a sequence of characters that consists of letters, digits, underscores, and dollar signs
An identifier must start with a letter, an underscore, or a dollar sign. It cannot start with a digit. An identifier cannot be a reserved word. An identifier cannot be 'true', 'false', or 'null.' An identifier can be of any length. |
|
illegal identifiers
|
the Java compiler detects illegal identifiers (those that brea the identifier naming rules) and reports syntax errors
|
|
naming variables
|
By convention, variables are in lowercase. If a name consists of several words, concatenate all of them and capitalize the first letter of each word except the first. Examples of variables are 'radius' and 'interestRate.'
|
|
initializing variables
|
Variables often have initial values. You can declare a variable AND initialize it in one step.
int count = 1; this is equivalent to the next two statements: int count; x = 1; You can also use a shorthand form to declare AND initialize variables of the same type together. For example, int i = 1, j = 2 |
|
assignment statement
|
after a variable is declared, you can assign a value to it by using an assignment statement
The syntax for assignment statements is as follows: variable = expression; |
|
assignment operator
|
In Java, the equal sign (=) is used as the assignment operator.
|
|
expression
|
represents a computation involving values, variables, and operators that, taking them together, evaluates to a value
|
|
variables in expressions
|
A variable can be used in an expression. For example,
x = x + 1; In this assignment statement, the result of x + 1 is assigned to x. If x is 1 before the statement is executed, then it becomes 2 after the statement is executed. To assign a value to a variable, the variable name must be on the left of the assignment operator. Thus, 1 = x would be wrong. |
|
assignment expression
|
an assignment statement is known as an assignment expression
in Java, an assignment statement is essentially an expression that evaluates to the value to be assigned to the variable on the left-hand side of the assignment operator System.out.println(x=1); is equivalent to x = 1; System.out.println(x); |
|
named contant
constant |
represents permanent data that never changes
|
|
syntax for declaring a constant
|
final datatype CONSTANTNAME = VALUE;
a constant must be declared AND initialized in the same statement |
|
final
|
a Java keyword for declaring a constant
final double PI = 3.141592; // Declare a constant constants are named in uppercase: PI, not pi or Pi |
|
benefits of constants
|
1) you don't have to repeatedly type the same value
2.) if you have to change the constant value (e.g.k from 3.14 to 3.141592 for PI), you need to change it only in a single location in the source code 3.) a descriptive name for constants makes the program easy to read |
|
the six numeric data types
|
byte
short int long float double |
|
the four integer types
|
byte
short int long |
|
the two types for floating-point numbers
|
float
double the 'double' type is twice as big as 'float.' so the 'double' is known as double precision, 'float' as single precision. |
|
numeric operators
|
the operators for numeric data types include the standard arithmetic operators:
addition (+) subtraction (-) multiplication (*) division (/) remainder (%) |
|
integer devision
|
When both operands of a division are integers, the result of the division is an integer.
For example, 5/2 yields1, not 2.5 To perform regular mathematical division, one of the operands must be a floating-point number. For example, 5.0/2 yields 2.5 |
|
unary operator
|
a unary operator has only one operand
( the + and - operators can be both unary and binary) |
|
binary operator
|
a binary operator has two operands
( the + and - operators can be both unary and binary) |
|
floating-point approximation
|
calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy
integers are stored precisely, therefore calculations with integers yield a precise integer result |
|
literal
|
a constant value that appears directly in a program
|
|
long literal
|
to denote an integer literal of the 'long' type, append the letter L to it
2147483648L because 2147483648 exceeds the range for the 'int' value |
|
floating-point literals
|
written with a decimal point
treated as a 'double' type value for example, 5.0 is considered a 'double' value, not a 'float' value suffix f or F: you can make a number a 'float' by appending the letter F suffix d or D: you can make a number a 'double' by appending the letter D |
|
double vs. float
|
the 'double' values are more accurate than 'float' values
|
|
scientific notation
|
floating-point literals can also be specified in scientific notation; for example,
1.23456e+2 is equivalent to 1.23456 X 10(2) [squared] 1.23456e-2 is equivalent to 1.23456 X 10(-2) |
|
'E' or 'e'
|
represents an exponent and can be in either lowercase or uppercase
|
|
why called floating-point?
|
The 'float' and 'double' types are used to represent numbers with a decimal point. Why are they called 'floating-point numbers'? These numbers are stored in scientific notation. When a number such as 50.534 is converted into scientific notation, such as 5.0534e+1, its decimal point is moved (i.e., floated) to a new position.
|
|
evaluating an expression
|
Java follows Please Excuse My Dear Aunt Sally.
|
|
integer vs. decimal division
|
Be careful when applying division. Division of two integers yields an integer in Java. 5/9 is translated to 5.0/9instead of 5/9, because 5/9 yields 0 in Java.
|
|
currentTimeMillis
|
The currentTimeMillis method in the System class returns the current time in milliseconds elapsed since the time 00:00:00 on January 1, 1970 GMT (the Unix epoch).
|
|
Unix epoch
|
1970 was the year when the Unix operating system was formally introduced.
00:00:00 on January 1, 1970 |
|
System.currentTimeMillis()
|
returns the number of milliseconds since the Unix epoch
you can use this method to obtain the current time, and then compute the current second, minute, and hour |
|
shorthand operators
|
Java allows you to combine assignment and numeric operators using a shorthand operator
there are no spaces in the shorthand operators. for example, + = should be += |
|
Operator:
+= |
Name:
addition assignment operator Example: i += 8 Equivalent: i = i + 8 |
|
Operator:
-= |
Name:
subtraction assignment operator Example: i -= 8 Equivalent: i = i - 8 |
|
Operator:
*= |
Name:
multiplication assignment operator Example: i *= 8 Equivalent: i = i * 8 |
|
Operator:
/= |
Name:
division assignment operator Example: i /= 8 Equivalent: i = i / 8 |
|
Operator:
%= |
Name:
remainder assignment operator Example: i %= 8 Equivalent: i = i % 8 |
|
assignment operator --> assignment statement
|
Like the assignment operator (=), the operators (+=, -=, /=, %=) can be used to form an assignment statement as well as an expression. For example, i the following code, x += 2 is a statement in the first line and an expression in the second line.
x += 2; // Statement System.out.println(x += 2); // Expression |
|
incrementing and decrementing a variable by 1
|
two shorthand operators:
++ // Incrementing a variable by 1 -- // Decrementing a variable by 1 prefix or suffix |
|
Operator:
++var preincrement |
Description:
Increment var by 1 and use the new var value Example (assume i = 1): int j = ++i; // j is 2, // i is 2 |
|
Operator:
var++ postincrement |
Description:
Increment the var by 1 and use the original var value Example (assume i=1): int j = i++; // j is 1, // i is 2 |
|
Operator:
--var predecrement |
Description:
Decrement the var by 1 and use the new var value Example (assume i = 1): int j = --i; // j is 0, // i is 0 |
|
Operator:
var-- postdecrement |
Description:
Decrement the var by 1 and use the original var value Example (assume i = 1): int j = i--; // j is 0, // i is 0 |
|
if the operator is before (prefixed to) the variable
preincrement operator |
the variable is incremented or decremented by 1, then the new value of the variable is returned
|
|
if the operator is after (suffixed to) the variable
postincrement operator |
the variable is incremented or decremented by 1, but the original old value of the variable is returned
|
|
loop statement
|
a construct that controls how many times an operation or sequence of operations is performed in succession
the increment operator ++ and the decrement operator -- can be applied to all integer floating-point types More on this in Chapter 4 |
|
Can you perform binary operations with two operands of different types?
|
Yes. If an integer and a floating-point number are involved in a binary operation, Java automatically converts the integer to a floating-point value.
So, 3 * 4.5 is the same as 3.0 * 4.5 |
|
type casting
|
an operation that converts a value of one data type into a value of another data type
casting does not change the variable being cast |
|
widening a type
|
casting a variable of a type with a small range to a variable of a type with a larger range
like ordering a tall coffe in a grande cup at Starbucks |
|
narrowing a type
|
casting a variable of a type with a large range to a variable of a type with a smaller range
moving from the country to the city - in regards to physical space moving from the city to the country - in regards to diversity |
|
type casting syntax
|
The syntax is the target type in parentheses, followed by the variable's name or the value to be case. For example, the following statement:
System.out.println((int) 1.7); displays 1. When a 'double' value is cast into an 'int' value, the fractional part is truncated. The following statement System.out.println((double) 1/2); displays 0.5, because 1 is cast to 1.0 first, then 1.0 is divided by 2. However, the statement System.out.println(1 / 2); displays 0, because 1 and 2 are both integers and the resulting value should also be an integer. |
|
pow(a, b) method
|
The pow(a, b) method in the Math class can be used to compute a(b) [a to the b power]
Example: System.out.println(Math.pow(2, 3)); // Display 8 System.out.println(Math.pow(4, 0.5)); // Display 4 |
|
char type
|
The character data type, 'char', is used to represent a single character.
|
|
character literal
|
A character literal is enclosed in single quotation marks:
char letter - 'A'; char numChar - '4'; The first statement assigns character A to the char variable letter. The second statement assigns digit character 4 to the char variable numChar. "A" is a string, and 'A' is a character. Also, letters are equiv. to Unicode here |
|
character encoding
|
Mapping a character to its binary representation
|
|
encoding scheme
|
the different ways in which characters are encoded
|
|
Unicode
|
Java supports Unicode, an encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world's diverse languages.
|
|
original Unicode
|
Unicode was originally designed as a 16-bit character encoding
The primitive data type 'char' was intended to take advantage of this design by providing simple data type that culd hold any character |
|
supplementary characters
|
original Unicode only had 65,536 possible characters in 16-bit encoding.
This was not sufficient for the world's characters, so they expanded to allow 1,112,064 characters |
|
prefix \u
|
a 16-bit Unicode takes two bytes, preceded by '\u' expressed in four hexadecimal digits that run from '\u0000' to '\uFFFF'
|
|
ASCII
|
American Standard Code for Information Interchange
a 7-bit encoding scheme for representing all uppercase and lowercase letters, digits, punctuation marks, and control characters |
|
escape sequence for special characters
|
Java defines escape sequences to represent special characters
begins with the backslash character (\) followed by a character that has a special meaning to the compiler |
|
Character Escape Sequence:
\b |
Name:
backspace Unicode Code: \u0008 |
|
Character Escape Sequence:
\t |
Name:
tab Unicode Code: \u0009 |
|
Character Escape Sequence:
\n |
Name:
linefeed Unicode Code: \u000A |
|
Character Escape Sequence:
\f |
Name:
formfeed Unicode Code: /u000C |
|
Character Escape Sequence:
\r |
Name:
Carriage Return Unicode Code: \u000D |
|
Character Escape Sequence:
\\ |
Name:
backslash Unicode Code: \u005C |
|
Character Escape Sequence:
\' |
Name:
Single Quote Unicode Code: \u0027 |
|
Character Escape Sequence:
\" |
Name:
Double Quote Unicode Code: \u0022 |
|
implicit and explicit casting
|
Implicit casting
double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) |