Unit 1 · Using Objects and Methods
● Core concept · ○ Supporting concept
1.1 Introduction to Algorithms, Programming, and Compilers
Algorithm ● (core concept) — An algorithm defines a step-by-step process to follow when completing a task or solving a problem. Algorithms can be represented using written language or diagrams.
Sequencing ● (core concept) — Sequencing defines an order for when steps in a process are completed. Steps in a process are completed one at a time.
Integrated development environment (IDE) ● (core concept) — Code can be written in any text editor; however, an integrated development environment (IDE) is often used to write programs because it provides tools for a programmer to write, compile, and run code.
Compiler ● (core concept) — A compiler checks code for some errors. Errors detectable by the compiler need to be fixed before the program can be run.
Syntax error ● (core concept) — A syntax error is a mistake in the program where the rules of the programming language are not followed. These errors are detected by the compiler.
Logic error ● (core concept) — A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. These errors are detected by testing the program with specific data to see if it produces the expected outcome.
Run-time error ● (core concept) — A run-time error is a mistake in the program that occurs during the execution of a program. Run-time errors typically cause the program to terminate abnormally.
Exception ● (core concept) — An exception is a type of run-time error that occurs as a result of an unexpected error that was not detected by the compiler. It interrupts the normal flow of the program's execution.
1.2 Variables and Data Types
Data type ● (core concept) — A data type is a set of values and a corresponding set of operations on those values. Data types can be categorized as either primitive or reference.
Primitive data type ● (core concept) — The primitive data types used in this course define the set of values and corresponding operations on those values for numbers and Boolean values.
Reference type ● (core concept) — A reference type is used to define objects that are not primitive types.
int ● (core concept) — int is one of the three primitive data types used in this course. An int value is an integer.
double ● (core concept) — double is one of the three primitive data types used in this course. A double value is a real number.
boolean ● (core concept) — boolean is one of the three primitive data types used in this course. A boolean value is either true or false.
Variable ● (core concept) — A variable is a storage location that holds a value, which can change while the program is running. Every variable has a name and an associated data type. A variable of a primitive type holds a primitive value from that type.
1.3 Expressions and Output
System.out.print ● (core concept) — System.out.print displays information on the computer display. Unlike System.out.println, it does not move the cursor to a new line after the information has been displayed.
System.out.println ● (core concept) — System.out.println displays information on the computer display and moves the cursor to a new line after the information has been displayed.
Literal ● (core concept) — A literal is the code representation of a fixed value.
String literal ● (core concept) — A string literal is a sequence of characters enclosed in double quotes.
Escape sequence ● (core concept) — Escape sequences are special sequences of characters that can be included in a string. They start with a backslash (\) and have a special meaning in Java. Escape sequences used in this course include double quote \", backslash \\, and newline \n.
Arithmetic expression ● (core concept) — An arithmetic expression consists of numeric values, variables, and operators. Arithmetic expressions include expressions of type int and double.
Arithmetic operators ● (core concept) — The arithmetic operators consist of addition +, subtraction -, multiplication *, division /, and remainder %. An arithmetic operation that uses two int values will evaluate to an int value. An arithmetic operation that uses at least one double value will evaluate to a double value.
Integer division ● (core concept) — When dividing numeric values that are both int values, the result is only the integer portion of the quotient. When dividing numeric values that use at least one double value, the result is the quotient.
Remainder operator (%) ● (core concept) — The remainder operator % is used to compute the remainder when one number a is divided by another number b.
Operator precedence ● (core concept) — At compile time, numeric values are associated with operators according to operator precedence to determine how they are grouped. Parentheses can be used to modify operator precedence. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.
ArithmeticException ● (core concept) — An attempt to divide an integer by the integer zero will result in an ArithmeticException.
1.4 Assignment Statements and Input
Initialization ● (core concept) — A variable is initialized the first time it is assigned a value. Every variable must be assigned a value before it can be used in an expression, and that value must be from a compatible data type.
null ● (core concept) — The literal null is a special value used to indicate that a reference is not associated with any object. Reference types can be assigned a new object or null if there is no object.
Assignment operator (=) ● (core concept) — The assignment operator = allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.
Expression evaluation ● (core concept) — During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the evaluation of the expression.
Scanner class ● (core concept) — Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard.
1.5 Casting and Range of Variables
Casting ● (core concept) — The casting operators (int) and (double) can be used to convert from a double value to an int value (or vice versa) in arithmetic expressions.
Truncation ● (core concept) — Casting a double value to an int value causes the digits to the right of the decimal point to be truncated.
Widening (automatic cast) ● (core concept) — Some code causes int values to be automatically cast (widened) to double values.
Rounding via casting ● (core concept) — Values of type double can be rounded to the nearest integer by (int)(x + 0.5) for non-negative numbers or (int)(x - 0.5) for negative numbers.
Integer.MAX_VALUE and Integer.MIN_VALUE ● (core concept) — The constant Integer.MAX_VALUE holds the value of the largest possible int value. The constant Integer.MIN_VALUE holds the value of the smallest possible int value.
int range and storage ● (core concept) — Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
Integer overflow ● (core concept) — If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. The result is an int value in the allowed range but not necessarily the value expected.
Round-off error ● (core concept) — Computers allot a specified amount of memory to store data based on the data type. If an expression would evaluate to a double that is more precise than can be stored in the allotted amount of memory, a round-off error occurs and the result will be rounded to the representable value. To avoid rounding errors that naturally occur, use int values.
1.6 Compound Assignment Operators
Compound assignment operators ● (core concept) — Compound assignment operators +=, -=, *=, /=, and %= can be used in place of the assignment operator in numeric expressions. A compound assignment operator performs the indicated arithmetic operation between the value on the left and the value on the right and then assigns the result to the variable on the left.
Post-increment (++) and post-decrement (--) ● (core concept) — The post-increment operator ++ and post-decrement operator -- are used to add 1 or subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.
1.7 Application Program Interface (API) and Libraries
Library ● (core concept) — Libraries are collections of classes.
API (Application Program Interface) ● (core concept) — An application programming interface (API) specification informs the programmer how to use the classes in a library. Documentation found in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API. A class defines a specific reference type.
Package ● (core concept) — Classes in APIs and libraries are grouped into packages. Existing classes and class libraries can be utilized to create objects.
Attribute ● (core concept) — Attributes refer to the data related to the class and are stored in variables.
Behavior ● (core concept) — Behaviors refer to what instances of the class can do (or what can be done with them) and are defined by methods.
1.8 Documentation with Comments
Comments ● (core concept) — Comments are written for both the original programmer and other programmers to understand the code and its functionality, but are ignored by the compiler and are not executed when the program is run. Three types of comments in Java include /* */, which generates a block of comments; //, which generates a comment on one line; and /** */, which are Javadoc comments and are used to create API documentation.
Precondition ● (core concept) — A precondition is a condition that must be true just prior to the execution of a method in order for it to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
Postcondition ● (core concept) — A postcondition is a condition that must always be true after the execution of a method. Postconditions describe the outcome of the execution in terms of what is being returned or the current value of the attributes of an object.
1.9 Method Signatures
Method ● (core concept) — A method is a named block of code that only runs when it is called. A block of code is any section of code that is enclosed in braces.
Procedural abstraction ● (core concept) — Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.
Parameter ● (core concept) — A parameter is a variable declared in the header of a method or constructor and can be used inside the body of the method. This allows values or arguments to be passed and used by a method or constructor.
Method signature ● (core concept) — A method signature for a method with parameters consists of the method name and the ordered list of parameter types. A method signature for a method without parameters consists of the method name and an empty parameter list.
void method ● (core concept) — A void method does not have a return value and is therefore not called as part of an expression.
Non-void method ● (core concept) — A non-void method returns a value that is the same type as the return type in the header. To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression.
Argument ● (core concept) — An argument is a value that is passed into a method when the method is called. The arguments passed to a method must be compatible in number and order with the types identified in the parameter list of the method signature.
Call by value ● (core concept) — When calling methods, arguments are passed using call by value. Call by value initializes the parameters with copies of the arguments.
Overloaded methods ● (core concept) — Methods are said to be overloaded when there are multiple methods with the same name but different signatures.
Method call and flow of control ● (core concept) — A method call interrupts the sequential execution of statements, causing the program to first execute the statements in the method before continuing. Once the last statement in the method has been executed or a return statement is executed, the flow of control is returned to the point immediately following where the method was called.
1.10 Calling Class Methods
Class method (static) ● (core concept) — Class methods are associated with the class, not instances of the class. Class methods include the keyword static in the header before the method name.
Calling a class method ● (core concept) — Class methods are typically called using the class name along with the dot operator. When the method call occurs in the defining class, the use of the class name is optional in the call.
1.11 Math Class
java.lang package ● (core concept) — The Math class and the String class are part of the java.lang package. Classes in the java.lang package are available by default.
Math class contains only class methods ● (core concept) — The Math class contains only class methods.
Math.abs ● (core concept) — static int abs(int x) returns the absolute value of an int value. static double abs(double x) returns the absolute value of a double value.
Math.pow ● (core concept) — static double pow(double base, double exponent) returns the value of the first parameter raised to the power of the second parameter.
Math.sqrt ● (core concept) — static double sqrt(double x) returns the nonnegative square root of a double value.
Math.random ● (core concept) — static double random() returns a double value greater than or equal to 0.0 and less than 1.0.
Random values in a range ● (core concept) — The values returned from Math.random() can be manipulated using arithmetic and casting operators to produce a random int or double in a defined range based on specified criteria. Each endpoint of the range can be inclusive, meaning the value is included, or exclusive, meaning the value is not included.
1.12 Objects: Instances of Classes
Object vs. class ● (core concept) — An object is a specific instance of a class with defined attributes. A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
Superclass and subclass ● (core concept) — A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without rewriting them in the code. This creates an inheritance relationship from the subclasses to the superclass.
Object class ● (core concept) — All classes in Java are subclasses of the Object class.
Object reference ● (core concept) — A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
1.13 Object Creation and Storage (Instantiation)
Constructor ● (core concept) — A class contains constructors that are called to create objects. They have the same name as the class.
Constructor signature ● (core concept) — A constructor signature consists of the constructor's name, which is the same as the class name, and the ordered list of parameter types. The parameter list, in the header of a constructor, lists the types of the values that are passed and their variable names.
Overloaded constructors ● (core concept) — Constructors are said to be overloaded when there are multiple constructors with different signatures.
Creating an object with new ● (core concept) — An object is typically created using the keyword new followed by a call to one of the class's constructors.
Constructor parameters ● (core concept) — Parameters allow constructors to accept values to establish the initial values of the attributes of the object.
Constructor argument ● (core concept) — A constructor argument is a value that is passed into a constructor when the constructor is called. The arguments passed to a constructor must be compatible in order and number with the types identified in the parameter list in the constructor signature. When calling constructors, arguments are passed using call by value.
Constructor call and flow of control ● (core concept) — A constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the constructor before continuing. Once the last statement in the constructor has been executed, the flow of control is returned to the point immediately following where the constructor was called.
1.14 Calling Instance Methods
Instance method ● (core concept) — Instance methods are called on objects of the class. The dot operator is used along with the object name to call instance methods.
NullPointerException ● (core concept) — A method call on a null reference will result in a NullPointerException.
1.15 String Manipulation
String object ● (core concept) — A String object represents a sequence of characters and can be created by using a string literal or by calling the String class constructor.
String immutability ● (core concept) — A String object is immutable, meaning once a String object is created, its attributes cannot be changed. Methods called on a String object do not change the content of the String object.
String concatenation ● (core concept) — Two String objects can be concatenated together or combined using the + or += operator, resulting in a new String object. A primitive value can be concatenated with a String object. This causes the implicit conversion of the primitive value to a String object.
toString ● (core concept) — A String object can be concatenated with any object, which implicitly calls the object's toString method — a behavior guaranteed to exist by the inheritance relationship every class has with the Object class. An object's toString method returns a string value representing the object. Subclasses of Object often override the toString method with a class-specific implementation.
String indices ● (core concept) — A String object has index values from 0 to one less than the length of the string. Attempting to access indices outside this range will result in a StringIndexOutOfBoundsException.
String.length() ● (core concept) — int length() returns the number of characters in a String object.
String.substring ● (core concept) — String substring(int from, int to) returns the substring beginning at index from and ending at index to - 1. String substring(int from) returns substring(from, length()).
String.indexOf ● (core concept) — int indexOf(String str) returns the index of the first occurrence of str; returns -1 if not found.
String.equals ● (core concept) — boolean equals(Object other) returns true if this corresponds to the same sequence of characters as other; returns false otherwise.
String.compareTo ● (core concept) — int compareTo(String other) returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other. Strings are ordered based upon the alphabet.
Single-character substring ● (core concept) — A string identical to the single element substring at position index can be created by calling substring(index, index + 1).