Computer Programming

Cards (35)

  • Enumeration (enum)

    A data type that contains a fixed set of constants
  • Enums are good to use when you already know all possibilities of the values or instances of the class
  • Enums can be declared on their own or within another class
  • With enums, it is impossible to create an invalid enum value without introducing a compiler error
  • Examples of when enumerations are useful
    • Compass directions
    • Months of the year
    • Cards in a deck
  • Creating an enum
    1. Declare a type with the enum keyword
    2. Provide an identifier for the type
    3. List the enum constants
  • Enum constants
    • They are the only allowed values for the type
  • Enum methods
    • toString()
    • ordinal()
    • equals()
    • compareTo()
  • Static enum methods
    • valueOf()
    • values()
  • Enums can be declared in their own file or within a class, but not within a method
  • Enum usage in a switch statement
    • Displaying pizza prices based on size
  • Using enums makes the values type-safe
  • Nested classes

    Classes created within another class
  • Types of nested classes
    • Static member class
    • Non-static member class (inner class)
    • Local class
    • Anonymous class
  • Reasons for nesting classes
    • The inner class is used only by the top-level class
    • Packaging the classes together makes their connection easier to understand and their code easier to maintain
  • Example of a nested class
    • RealEstateListing class with a nested HouseData class
  • An inner class can access its top-level class's fields and methods, even if they are private, and an outer class can access its inner class's members
  • Nested classes are not frequently created, but they are sometimes seen in built-in Java classes
  • Regular expression (regex)

    A character or a sequence of characters that represent a string or multiple strings
  • Regular expressions are part of the java.util.regex package
  • Regular expressions

    • Allow for quicker, easier searching, parsing, and replacing of characters in a string
    • Are used to ensure that strings contain specific contents
    • Are often used to check correct email format (@ sign is included) on form validation
  • String.matches(String regex)
    Returns true if a string matches the given regular expression
  • Regex to check if animal is "cat" or "dog"
    • animal.matches("cat|dog")
  • Regex to check if animal is "cat", "Cat", "dog", or "Dog"
    • animal.matches("[Cc]at|[Dd]og")
  • Regex to check if word rhymes with "cat"

    • word.matches("[a-z]at")
  • Regex to check if word starts with any letter and ends in "at"

    • word.matches("[a-zA-Z](at|AT)")
  • Dot (.)

    Wildcard operator that represents any single character in regular expressions
  • Repetition operators
    • * (0 or more occurrences)
    • ? (0 or 1 occurrence)
    • + (1 or more occurrences)
    • {x} (x occurrences)
    • {x,y} (between x & y occurrences)
    • {x,} (x or more occurrences)
  • Regular expressions allow for very powerful validation of strings without having to write much code
  • Pattern class

    Stores the format of a regular expression
  • Matcher class
    Stores a possible match between a pattern and a string
  • Using Pattern and Matcher
    1. Initialize pattern with Pattern.compile(regex)
    2. Initialize matcher with matcher = pattern.matcher(string)
    3. Use matcher.matches() to check if string matches pattern
  • Matcher's find() method

    • Scans the input sequence looking for the next subsequence that matches the pattern
  • String.split(regex)
    • Splits the string around matches of the given regular expression
  • String.replaceAll(regex, replacement)

    • Replaces all the occurrences of the defined regular expression found in the string with another string