Skip to main content

Section 5.3 Code Lens

View Source for section
<section xml:id="code-lens">
  <title>Code Lens</title>

  <p>
    CodeLens is an interactive tool for following program execution, much like
    a debugger, without the ability to influence flow control or variable
    values. For use without a server, traces must be computed beforehand.
    First, we have some trivial programs, to provide minimal testing.
  </p>

  <listing xml:id="program-codelens-python">
    <title>A Python program, stepable with CodeLens</title>

    <program label="python-hello-world-code-lens" interactive="codelens" language="python">
      <code>
              print('Hello, World!')
      </code>
    </program>
  </listing>

  <listing xml:id="program-codelens-c">
    <title>An C program, stepable with CodeLens</title>

    <program label="c-hello-world-code-lens" interactive="codelens" language="c">
      <code>
              #include &lt;stdio.h&gt;

              int main(void)
              {
                  puts("Hello, World!");
              }
      </code>
    </program>
  </listing>

  <listing xml:id="program-codelens-java">
    <title>A Java program, stepable with CodeLens</title>

    <program label="java-hello-world-code-lens" interactive="codelens" language="java">
      <code>
              public class HelloWorld {
                  public static void main(String[] args) {
                      System.out.println("Hello, World!");
                  }
              }
      </code>
    </program>
  </listing>

  <p>
    Codelens interactives can be given one or more checkpoints where the user
    is asked a question as the code is executed:
  </p>

  <listing xml:id="program-codelens-python-questions">
    <title>A Python program, stepable with CodeLens, with questions</title>

    <program label="python-code-lens-questions" interactive="codelens" language="python">
      <code>
              def foo(n):
                  n = n + 1
                  return n
              x = 7
              y = x // 3
              z = foo(y)
      </code>
      <checkpoint line="4" answer="x">
        <prompt>What variable is being assigned to?</prompt>
      </checkpoint>
      <checkpoint line="5" answer-variable="globals.y">
        <prompt>
          What value will be assigned to
          <c>y</c>
          ?
        </prompt>

        <feedback>
          <c>//</c>
          does integer division
        </feedback>
      </checkpoint>
      <checkpoint line="3" answer-variable="current_frame.n">
        <prompt>What value will be returned?</prompt>
        <feedback>
          What is
          <c>n</c>
          right now?
        </feedback>
      </checkpoint>
    </program>
  </listing>

  <p>
    If a Codelens contains checkpoints, it is also possible to make it into an
    exercise. For use on Runestone, there should be an <attr>label</attr> on
    the exercise:
  </p>

  <exercise label="program-codelens-cpp-questions">
    <title>A C++ program as CodeLens exercise</title>

    <statement>
      Run the codelens and answer the questions it asks.
    </statement>

    <program interactive="codelens" language="cpp">
      <code>
                int foo() {
                    int x = 2;
                    int y = ++x;
                    return y;
                }
                int main() {
                    int x = foo();
                    while (x &lt; 20) {
                      x *= 2;
                    }
                }
                
      </code>
      <checkpoint line="3" answer="3">
        <prompt>
          What value will be assigned to
          <c>y</c>
          ?
        </prompt>

        <feedback>
          <c>++</c>
          has precedence over
          <c>=</c>
        </feedback>
      </checkpoint>
      <checkpoint line="9" answer-variable="current_frame.x">
        <prompt>
          What value will be assigned to
          <c>x</c>
          ?
        </prompt>

        <feedback>
          We are doubling it
        </feedback>
      </checkpoint>
    </program>
  </exercise>

  <p>
    Now some moderately more complicated programs to find the prime numbers
    less than <m>20</m>. We do not vouch for the quality of these, or even
    their correctness!
  </p>

  <listing xml:id="sieve-python">
    <title>
      <url href="https://www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes" visual="www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes">Sieve of Eratosthenes</url>,
      Java
    </title>

    <program label="sieve-codelens-python" interactive="codelens" language="python" starting-step="73">
      <code>
              def SieveOfEratosthenes(n):
                 # array of type boolean with True values in it
                 prime = [True for i in range(n + 1)]
                 p = 2
                 while (p * p &lt;= n):
                    # If it remain unchanged it is prime
                    if (prime[p] == True):
                       # updating all the multiples
                       for i in range(p * 2, n + 1, p):
                          prime[i] = False
                    p += 1
                 prime[0]= False
                 prime[1]= False
                 # Print
                 for p in range(n + 1):
                    if prime[p]:
                       print (p,end=" ")
              # main
              if __name__=='__main__':
                 n = 20
                 print ("The prime numbers smaller than or equal to", n,"is")
                 SieveOfEratosthenes(n)
              
      </code>
    </program>
  </listing>

  <listing xml:id="sieve-cpp">
    <title>
      <url href="https://www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range" visual="www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range">Sieve of Eratosthenes</url>,
      C++
    </title>

    <program label="sieve-codelens-cpp" interactive="codelens" language="cpp">
      <code> 
              #include &lt;stdio.h&gt;
              const int len = 20;
              int main() {
                 int arr[20] = {0};
                 for (int i = 2; i &lt; len; i++) {
                    for (int j = i * i; j &lt; len; j+=i) {
                       arr[j - 1] = 1;
                    }
                 }
                 for (int i = 1; i &lt; len; i++) {
                    if (arr[i - 1] == 0)
                      printf(" %d", i);
                 }
              }
              
      </code>
    </program>
  </listing>

  <listing xml:id="sieve-java">
    <title>
      <url href="https://www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java" visual="www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java">Sieve of Eratosthenes</url>,
      Java
    </title>

    <program label="sieve-codelens-java" interactive="codelens" language="java">
      <code>
              public class SievePrimeFactors  {
                 public static void main(String args[]) {
                    int num = 20;
                    boolean[] bool = new boolean[num];

                    for (int i = 0; i&lt; bool.length; i++) {
                       bool[i] = true;
                    }
                    for (int i = 2; i &lt; Math.sqrt(num); i++) {
                       if(bool[i] == true) {
                          for(int j = (i*i); j &lt; num; j = j+i) {
                             bool[j] = false;
                          }
                       }
                    }
                    System.out.println("List of prime numbers: ");
                    for (int i = 2; i&lt; bool.length; i++) {
                       if(bool[i]==true) {
                          System.out.println(i);
                       }
                    }
                 }
              }
              
      </code>
    </program>
  </listing>
</section>
CodeLens is an interactive tool for following program execution, much like a debugger, without the ability to influence flow control or variable values. For use without a server, traces must be computed beforehand. First, we have some trivial programs, to provide minimal testing.
View Source for listing
<listing xml:id="program-codelens-python">
  <title>A Python program, stepable with CodeLens</title>

  <program label="python-hello-world-code-lens" interactive="codelens" language="python">
    <code>
            print('Hello, World!')
    </code>
  </program>
</listing>
Listing 5.3.1. A Python program, stepable with CodeLens
View Source for program
<program label="python-hello-world-code-lens" interactive="codelens" language="python">
  <code>
          print('Hello, World!')
  </code>
</program>
View Source for listing
<listing xml:id="program-codelens-c">
  <title>An C program, stepable with CodeLens</title>

  <program label="c-hello-world-code-lens" interactive="codelens" language="c">
    <code>
            #include &lt;stdio.h&gt;

            int main(void)
            {
                puts("Hello, World!");
            }
    </code>
  </program>
</listing>
Listing 5.3.2. An C program, stepable with CodeLens
View Source for program
<program label="c-hello-world-code-lens" interactive="codelens" language="c">
  <code>
          #include &lt;stdio.h&gt;

          int main(void)
          {
              puts("Hello, World!");
          }
  </code>
</program>
View Source for listing
<listing xml:id="program-codelens-java">
  <title>A Java program, stepable with CodeLens</title>

  <program label="java-hello-world-code-lens" interactive="codelens" language="java">
    <code>
            public class HelloWorld {
                public static void main(String[] args) {
                    System.out.println("Hello, World!");
                }
            }
    </code>
  </program>
</listing>
Listing 5.3.3. A Java program, stepable with CodeLens
View Source for program
<program label="java-hello-world-code-lens" interactive="codelens" language="java">
  <code>
          public class HelloWorld {
              public static void main(String[] args) {
                  System.out.println("Hello, World!");
              }
          }
  </code>
</program>
Codelens interactives can be given one or more checkpoints where the user is asked a question as the code is executed:
View Source for listing
<listing xml:id="program-codelens-python-questions">
  <title>A Python program, stepable with CodeLens, with questions</title>

  <program label="python-code-lens-questions" interactive="codelens" language="python">
    <code>
            def foo(n):
                n = n + 1
                return n
            x = 7
            y = x // 3
            z = foo(y)
    </code>
    <checkpoint line="4" answer="x">
      <prompt>What variable is being assigned to?</prompt>
    </checkpoint>
    <checkpoint line="5" answer-variable="globals.y">
      <prompt>
        What value will be assigned to
        <c>y</c>
        ?
      </prompt>

      <feedback>
        <c>//</c>
        does integer division
      </feedback>
    </checkpoint>
    <checkpoint line="3" answer-variable="current_frame.n">
      <prompt>What value will be returned?</prompt>
      <feedback>
        What is
        <c>n</c>
        right now?
      </feedback>
    </checkpoint>
  </program>
</listing>
Listing 5.3.4. A Python program, stepable with CodeLens, with questions
View Source for program
<program label="python-code-lens-questions" interactive="codelens" language="python">
  <code>
          def foo(n):
              n = n + 1
              return n
          x = 7
          y = x // 3
          z = foo(y)
  </code>
  <checkpoint line="4" answer="x">
    <prompt>What variable is being assigned to?</prompt>
  </checkpoint>
  <checkpoint line="5" answer-variable="globals.y">
    <prompt>
      What value will be assigned to
      <c>y</c>
      ?
    </prompt>

    <feedback>
      <c>//</c>
      does integer division
    </feedback>
  </checkpoint>
  <checkpoint line="3" answer-variable="current_frame.n">
    <prompt>What value will be returned?</prompt>
    <feedback>
      What is
      <c>n</c>
      right now?
    </feedback>
  </checkpoint>
</program>
If a Codelens contains checkpoints, it is also possible to make it into an exercise. For use on Runestone, there should be an @label on the exercise:

Checkpoint 5.3.5. A C++ program as CodeLens exercise.

View Source for exercise
<exercise label="program-codelens-cpp-questions">
  <title>A C++ program as CodeLens exercise</title>

  <statement>
    Run the codelens and answer the questions it asks.
  </statement>

  <program interactive="codelens" language="cpp">
    <code>
              int foo() {
                  int x = 2;
                  int y = ++x;
                  return y;
              }
              int main() {
                  int x = foo();
                  while (x &lt; 20) {
                    x *= 2;
                  }
              }
              
    </code>
    <checkpoint line="3" answer="3">
      <prompt>
        What value will be assigned to
        <c>y</c>
        ?
      </prompt>

      <feedback>
        <c>++</c>
        has precedence over
        <c>=</c>
      </feedback>
    </checkpoint>
    <checkpoint line="9" answer-variable="current_frame.x">
      <prompt>
        What value will be assigned to
        <c>x</c>
        ?
      </prompt>

      <feedback>
        We are doubling it
      </feedback>
    </checkpoint>
  </program>
</exercise>
Run the codelens and answer the questions it asks.
Now some moderately more complicated programs to find the prime numbers less than \(20\text{.}\) We do not vouch for the quality of these, or even their correctness!
View Source for listing
<listing xml:id="sieve-python">
  <title>
    <url href="https://www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes" visual="www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes">Sieve of Eratosthenes</url>,
    Java
  </title>

  <program label="sieve-codelens-python" interactive="codelens" language="python" starting-step="73">
    <code>
            def SieveOfEratosthenes(n):
               # array of type boolean with True values in it
               prime = [True for i in range(n + 1)]
               p = 2
               while (p * p &lt;= n):
                  # If it remain unchanged it is prime
                  if (prime[p] == True):
                     # updating all the multiples
                     for i in range(p * 2, n + 1, p):
                        prime[i] = False
                  p += 1
               prime[0]= False
               prime[1]= False
               # Print
               for p in range(n + 1):
                  if prime[p]:
                     print (p,end=" ")
            # main
            if __name__=='__main__':
               n = 20
               print ("The prime numbers smaller than or equal to", n,"is")
               SieveOfEratosthenes(n)
            
    </code>
  </program>
</listing>
Listing 5.3.6. Sieve of Eratosthenes, Java
View Source for program
<program label="sieve-codelens-python" interactive="codelens" language="python" starting-step="73">
  <code>
          def SieveOfEratosthenes(n):
             # array of type boolean with True values in it
             prime = [True for i in range(n + 1)]
             p = 2
             while (p * p &lt;= n):
                # If it remain unchanged it is prime
                if (prime[p] == True):
                   # updating all the multiples
                   for i in range(p * 2, n + 1, p):
                      prime[i] = False
                p += 1
             prime[0]= False
             prime[1]= False
             # Print
             for p in range(n + 1):
                if prime[p]:
                   print (p,end=" ")
          # main
          if __name__=='__main__':
             n = 20
             print ("The prime numbers smaller than or equal to", n,"is")
             SieveOfEratosthenes(n)
          
  </code>
</program>
View Source for listing
<listing xml:id="sieve-cpp">
  <title>
    <url href="https://www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range" visual="www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range">Sieve of Eratosthenes</url>,
    C++
  </title>

  <program label="sieve-codelens-cpp" interactive="codelens" language="cpp">
    <code> 
            #include &lt;stdio.h&gt;
            const int len = 20;
            int main() {
               int arr[20] = {0};
               for (int i = 2; i &lt; len; i++) {
                  for (int j = i * i; j &lt; len; j+=i) {
                     arr[j - 1] = 1;
                  }
               }
               for (int i = 1; i &lt; len; i++) {
                  if (arr[i - 1] == 0)
                    printf(" %d", i);
               }
            }
            
    </code>
  </program>
</listing>
Listing 5.3.7. Sieve of Eratosthenes, C++
View Source for program
<program label="sieve-codelens-cpp" interactive="codelens" language="cpp">
  <code> 
          #include &lt;stdio.h&gt;
          const int len = 20;
          int main() {
             int arr[20] = {0};
             for (int i = 2; i &lt; len; i++) {
                for (int j = i * i; j &lt; len; j+=i) {
                   arr[j - 1] = 1;
                }
             }
             for (int i = 1; i &lt; len; i++) {
                if (arr[i - 1] == 0)
                  printf(" %d", i);
             }
          }
          
  </code>
</program>
View Source for listing
<listing xml:id="sieve-java">
  <title>
    <url href="https://www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java" visual="www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java">Sieve of Eratosthenes</url>,
    Java
  </title>

  <program label="sieve-codelens-java" interactive="codelens" language="java">
    <code>
            public class SievePrimeFactors  {
               public static void main(String args[]) {
                  int num = 20;
                  boolean[] bool = new boolean[num];

                  for (int i = 0; i&lt; bool.length; i++) {
                     bool[i] = true;
                  }
                  for (int i = 2; i &lt; Math.sqrt(num); i++) {
                     if(bool[i] == true) {
                        for(int j = (i*i); j &lt; num; j = j+i) {
                           bool[j] = false;
                        }
                     }
                  }
                  System.out.println("List of prime numbers: ");
                  for (int i = 2; i&lt; bool.length; i++) {
                     if(bool[i]==true) {
                        System.out.println(i);
                     }
                  }
               }
            }
            
    </code>
  </program>
</listing>
Listing 5.3.8. Sieve of Eratosthenes, Java
View Source for program
<program label="sieve-codelens-java" interactive="codelens" language="java">
  <code>
          public class SievePrimeFactors  {
             public static void main(String args[]) {
                int num = 20;
                boolean[] bool = new boolean[num];

                for (int i = 0; i&lt; bool.length; i++) {
                   bool[i] = true;
                }
                for (int i = 2; i &lt; Math.sqrt(num); i++) {
                   if(bool[i] == true) {
                      for(int j = (i*i); j &lt; num; j = j+i) {
                         bool[j] = false;
                      }
                   }
                }
                System.out.println("List of prime numbers: ");
                for (int i = 2; i&lt; bool.length; i++) {
                   if(bool[i]==true) {
                      System.out.println(i);
                   }
                }
             }
          }
          
  </code>
</program>
You have attempted of activities on this page.