• Understand the CPSC 220 Programming Patterns.
  • Programming Patterns

    Wirth Pattern

    The Wirth Pattern was developed in the Intro & 1st Programs module and shown in Primitive Types. We reference the Wirth Pattern in other modules. For example, when studying Simple Objects focus on the Data Structures component and studying Control Flow focuses on the Algorithms component.

    Main Program Pattern

    The Main Program Pattern was developed in the Intro & 1st Programs module and shown in Problem Solving. We use the Main Program Pattern in every Java program we create.

    Output Program Pattern

    We know that all programs accept input and produce output. The Output Program Pattern shows how to produce output in the terminal. We developed output in the Intro & 1st Program module and showed the Output Program Pattern in Our First Java Programs.

    • The parameter data is a String. The attributes of Strings as defined above apply when passing a string to print and println.
    • The rationale for prefixing print and println with System.out will be explained in Simple Objects.
    • If you have several values to print you simply concatenate them. The following is an example.
    int num1 = 4;
    double num2 = 3.14;
    System.out.println("Num 1 is " + num1 + " and num 2 is " + num2 + ".");
    

    Input Program Pattern

    We know that all programs accept input and produce output. The Input Program Pattern shows how to accept input from the terminal. We developed output in the Intro & 1st Program module and showed the Input Program Pattern in Our First Java Programs. The Input Program Pattern uses a Scanner object.

    JVM, JDK, and JRE Pattern

    We developed JVM, JDK, and JRE in the Intro & 1st Program module and showed the pattern in IDEs, Compiling, Interpreting. The JVM, JDK, and JRE pattern figure allows you to easily visualize compiling and execution of Java programs.

    Software Development Process Pattern

    We developed JVM, JDK, and JRE in the Intro & 1st Program module and showed the pattern in Software Development Process. The software development process pattern figure allows you to easily visualize the software development process.

    Swapping Variables Pattern

    We developed Swapping Variables Pattern in the Expressions module and showed the pattern in Assignment Expressions.

    Random Number Pattern

    We developed Random Number Pattern in the Simple Objects module and showed the pattern in Java Random. The Random Number Pattern uses a Random object and Math.random to generate a random int and double.

    Defining Type Pattern

    We developed Defining Type Pattern in the Simple Objects module and showed the pattern in Our First Classes.

    Defining Tester Pattern

    We developed Defining Tester Pattern in the Simple Objects module and showed the pattern in Our First Classes.

    Counting Loop Pattern

    We developed Counting Loop Pattern in the Control Flow module and showed the pattern in Loops, while, for, do-while.

    Fence Post Pattern

    We developed Fence Post Pattern in the Control Flow module and showed the pattern in Loop Patterns.

    Sentinel Loop Pattern

    We developed Sentinel Loop Pattern in the Control Flow module and showed the pattern in Loop Patterns.

    Accumulator Pattern

    We developed Accumulator Pattern in the Control Flow module and showed the pattern in Loop Patterns.

    Compute Sum/Average Pattern

    We developed Compute Sum/Average Pattern in the Control Flow module and showed the pattern in Loop Patterns.

    Find Largest (or Smallest) Pattern

    We developed Find Largest (or Smallest) Pattern in the Control Flow module and showed the pattern in Loop Patterns. You can change the comparison from > to < to find the smallest.

    Find Largest Pattern for Numbers in a File

    We developed Find Largest Pattern in a File in the Control Flow module and showed the pattern in Loop Patterns.
    We connect a Java Scanner to an input file, and read numbers from a file to find the largest number.

    Count Matches Pattern

    We developed Count Matches Pattern in the Control Flow module and showed the pattern in Loop Patterns.

    Find Position of First Match Pattern

    We developed Find Position of First Match Pattern in the Control Flow module and showed the pattern in Loop Patterns.

    A String is a sequence of characters. A String can also be a sentence with words. The position of a match in a String is the index where the String begins. For examples,

    • The first 'a' in "I am a teacher." is at position 2.
    • The first "brick" in "Lego bricks are not masonry bricks" is at position 5.

    The Find Poistion of First Match Pattern finds the index where the match begins. The example in ths section uses String, but this pattern works with any sequential collection such as arrays and ArrayLists.

    The following example finds the position of a space in a String. If the String does not contain a space, the position is determined to be -1. You should notice this mimicks the semantics of the String method indexOf.

    Tags: