B

FLVS APCSA 2026 MODULE 2 (Using Objects) GUIDED NOTES

Java Classes and APIs

Objectives
  • Identify the attributes and behaviors of a class found in the libraries contained in an API.

  • Develop code to write expressions that incorporate calls to built-in mathematical libraries and determine the value that is produced as a result.

Key Concepts and Terms
  • Java API (Application Interface): A source of documentation for all ready-to-use Java classes.

  • Packages: A group of related classes.

  • Three Frames of the Java API:

    • Package Frame: The top-left frame, initially listing all packages in the Java API.

    • Classes Frame: The bottom-left frame, allowing selection of classes. Selecting a package here will cause this frame to display only the classes within that specific package.

    • Documentation Frame: Initially lists brief descriptions of each package. When a particular class or package is selected, it provides detailed information about it.

  • Members (as they relate to classes): These are the parts that constitute a class.

  • Fields: An instance variable, often predefined class constants.

  • Constructors: A statement used to create an object, which can then be utilized to access the class's methods.

  • Benefit of Using the Java API: It allows programmers to access detailed information about any member of a class.

  • length() method (String): This method returns the total number of characters (length) of the string provided inside its brackets by counting the Unicode code units within it.

String Objects

Objectives
  • Develop code to create String objects and determine the result of creating and combining Strings.

  • Develop code to call methods on String objects and determine the result of calling these methods.

Math Class Methods
  • pow(x,y): Raises x to the power of y. For example, Math.pow(2, 3) would return 8.0.

  • sqrt(): Returns the POSITIVE square root of the number provided within its parentheses. For example, Math.sqrt(9) would return 3.0.

  • abs(): Returns the absolute value of the number inside its parentheses, effectively making the number positive even if it was initially negative. For instance, Math.abs(-5) would return 5.

  • random(): Generates a pseudo-random double number that is no lower than 0.0 and less than 1.0. (i.e., in the range [0.0, 1.0)).

  • Math.random() * range) + min: This formula adjusts the random number generated by Math.random() to fit within a specified range, starting from a minimum number min.

    • The range is calculated as (max number - min number + 1).

  • Math.PI: Represents the mathematical constant Pi, which is the ratio of a circle's circumference to its diameter. Its approximate value is 3.141592653589793.

Key Concepts and Terms
  • Identifier: A name used to uniquely identify various programming constructs like variables, functions, classes, and methods.

  • String Objects: These consist of sequences of text characters.

  • ASCII Art: A form of digital art that uses characters from the ASCII character set (including letters, symbols, and numbers) to create images.

String Methods
  • length(): Returns the length of the string, which is the total count of characters it contains.

  • indexOf(y, x):

    • Returns the index (position) of the first occurrence of the specified substring y within the calling string.

    • If an integer x is provided after the substring y within the brackets, the search for the substring's first occurrence will begin after the position specified by x.

    • Note: Positions (indices) in Java strings start at 0, not 1, unlike some pseudocode standards (e.g., APCSP).

  • replace(y, x): Replaces every occurrence of the literal target sequence y within the string with the specified replacement x. In simpler terms, it replaces every instance of y with x.

  • replaceAll(y, x): Similar to replace(), but it replaces every substring that matches y based on a regular expression pattern with x. This allows for more advanced, pattern-based replacements, such as replacing all numbers or whitespace characters.

  • substring(x, y):

    • Returns a new string that is a substring of the original, starting at the character position x (inclusive).

    • If a second integer y is provided, the substring will end at position y (exclusive).

    • If no y is provided, the substring will start at position x and continue until the end of the original string.

    • Note: All positions (indices) start at 0.

Working with Methods and Objects
  • Dot Notation: A notation used to call methods or access variables. It involves placing a dot (.) between an object or class (A) and its member (B) to access B. For instance, A.B();.

Scanner Class

Objectives
  • Develop code to read input from various sources.

  • Develop code to read data from a text file.

Key Concepts and Terms
  • Different Forms of Input:

    • Tactile: Input from touchscreens or physical buttons.

    • Audio: Input from microphones.

    • Visual: Input from cameras.

    • Texts: Input from text boxes or command lines.

  • How the Scanner Class is Used: It is employed to enable a program to accept text input provided by the user.

Scanner Methods
  • next(): Finds and returns the next complete token from this scanner. A token is typically a sequence of non-whitespace characters.

  • nextInt(): Scans the next token of the input as an int.

  • nextDouble(): Scans the next token of the input as a double.

  • nextLine(): Advances this scanner past the current line and returns the input that was skipped. This method reads the rest of the current line, including the line separator, and returns the skipped input.

Parsing Methods
  • Parsing:

    • Breaking data or code down into smaller, manageable parts.

  • parseInt():

    • Converts a string (consisting of numbers) into an integer.

  • parseDouble():

    • Converts a string into a double.

Calling Methods
  • Method: a small segment of code that performs a specific task when called.

  • Top-Down Design: This coding style breaks programs into smaller subtasks or methods for better organization and manageability.

  • Modularity: Programs are divided into smaller, reusable parts, enhancing code maintainability and readability.

  • Procedural Abstraction: Programmers can use methods by understanding what they do without needing to know how they are implemented.

  • Static vs. Non-Static Methods and Side Effects:

    • //Static Methods:

      • Reference no objects; their behavior and any value they return are determined solely by their argument(s).

      • They behave like a "black box" where the output depends only on the input arguments.

    • Non-Static Methods (e.g., String methods):

      • Their behavior and return values depend on both their argument(s) (if any) and the specific object upon which they are invoked.

    • Side Effects:

      • Incidental occurrences during method execution that manifest as changes or actions rather than a computed return value.

      • A common side effect is printing a message to the output window.

  • Parameters in Method Signatures:

    • Formal Parameter:

      • A data type keyword / variable pair (e.g., (int x)) found within the parentheses of a method's signature.

      • It serves as a placeholder variable that, within the method's description, will be replaced by the actual argument during method invocation.

  • Void vs. Non-Void Methods and the return Keyword:

    • void Methods (Non-Returning):

      • Use the void keyword in their signature to signify that they do not explicitly return a value.

      • Their primary purpose is to produce side effects (e.g., printing output, modifying object state) rather than evaluating and returning a specific result.

    • Non-void Methods (Returning):

      • Specify a data type (e.g., int, double, String) in their signature, indicating the type of value they will compute and return.

    • return Keyword:

      • Marks an exit point from a method.

      • When executed, it evaluates the expression immediately following it.

      • Ceases all further execution within the method.

      • Returns the evaluated value to the calling context.

  • Hard Coding Values:

    • Hard coding refers to embedding fixed values directly into the source code, making it less flexible and harder to maintain.

  • Program Entry Point:

    • Java programs typically begin execution with the main() method, regardless of the order in which methods are defined in the code.

  • Instance of a Class:

    • An instance of a class is a specific object created from that class, representing a concrete manifestation of the class definition.

  • NullPointerException:

    • A NullPointerException may be thrown when an attempt is made to call an instance method on an object that has not been initialized or does not exist.

  • Passing in Arguments:

    • Passing in arguments means providing values to a method's parameters when calling it, allowing the method to operate with dynamic input rather than fixed values.

Booleans
  • Boolean Data Types:

    • Boolean data types can hold two possible values: true or false.

  • Truth Values:

    • Truth values refer to the possible outcomes of a boolean expression, indicating whether the expression evaluates to true or false.

  • Relational Operators:

    • Relational operators are used to compare two values, variables, or expressions, resulting in a boolean outcome.

  • Object Reference:

    • An object reference indicates the memory location or address where an object is stored in a computer's memory.

  • Example of Boolean Evaluation:

    • Code examples demonstrate how boolean expressions evaluate to true or false:

      • System.out.println(5 < 8); outputs TRUE.

      • System.out.println(8 < 5); outputs FALSE.