Section 3.4 Coding Exercises
Program listings can be more that just live demonstrations, they can be exercises. The first two also occur in the sample article where they just get a static rendering, if at all.
Checkpoint 3.4.1. Inline Coding Exercise, No Help.
View Source for exercise
<exercise label="coding-exercise-blank">
<title>Inline Coding Exercise, No Help</title>
<statement>
<p>An exercise might ask a reader to write a computer program, that would go here in the <tag>statement</tag>. But you can also add a <tag>program</tag> element after a <tag>statement</tag>. Here we place no code at all, but we do say we want it to be interactive. The purpose is to make it a live coding environment for a version of your output that allows the reader to perhaps submit a solution. The <tag>program</tag> element is necessary so you can specify a programming language.</p>
<p>In interactive formats, try creating and running a Python program below. Use CodeLens to step through the program.</p>
</statement>
<program interactive="activecode" language="python" />
<hint><p>We didn't really ask you to do anything.</p></hint>
</exercise>
An exercise might ask a reader to write a computer program, that would go here in the <statement>
. But you can also add a <program>
element after a <statement>
. Here we place no code at all, but we do say we want it to be interactive. The purpose is to make it a live coding environment for a version of your output that allows the reader to perhaps submit a solution. The <program>
element is necessary so you can specify a programming language.
In interactive formats, try creating and running a Python program below. Use CodeLens to step through the program.
Hint.
We didn’t really ask you to do anything.
Checkpoint 3.4.2. Inline Coding Exercise, Partial.
View Source for exercise
<exercise label="coding-exercise-partial-one">
<title>Inline Coding Exercise, Partial</title>
<statement>
<p>Similar to above, but we provide a starting point for the exercise.</p>
</statement>
<program interactive="activecode" language="c">
<code>
#include <stdio.h>
int main(void)
</code>
</program>
<answer>
<p>We're not really sure. But it would begin as follows:</p>
<program language="c">
<code>
#include <stdio.h>
int main(void)
</code>
</program>
</answer>
</exercise>
Similar to above, but we provide a starting point for the exercise.
#include <stdio.h>
int main(void)
Answer.
We’re not really sure. But it would begin as follows:
#include <stdio.h>
int main(void)
Activity 3.4.1. Activity Coding Exercise.
View Source for activity
<activity xml:id="coding-exercise-partial-two">
<title>Activity Coding Exercise</title>
<statement>
<p>Similar to above, but now as a complete Python program inside an <tag>activity</tag>. This demonstrates the possibility to use any <q>project-like</q> block (<tag>project</tag>, <tag>activity</tag>, <tag>exploration</tag>, <tag>investigation</tag>), but not in the case when structured with <tag>task</tag>. (There is an empty <tag>tests</tag> element here, designed to test relief for an error this will cause on a Runestone server.)</p>
</statement>
<program label="activity-program" interactive="activecode" language="python">
<code>
for i in range(10):
print(i)
</code>
<tests>
</tests>
</program>
<answer><p>We're still not really sure.</p></answer>
</activity>
Similar to above, but now as a complete Python program inside an <activity>
. This demonstrates the possibility to use any “project-like” block (<project>
, <activity>
, <exploration>
, <investigation>
), but not in the case when structured with <task>
. (There is an empty <tests>
element here, designed to test relief for an error this will cause on a Runestone server.)
Answer.
We’re still not really sure.
Checkpoint 3.4.3. An Exercise with a Static Program.
View Source for exercise
<exercise>
<title>An Exercise with a Static Program</title>
<statement>
<p>Similar to above, again, but we place the <tag>program</tag> element <em>inside</em> the <tag>statement</tag>, not after it as a peer. This signals that this is not a coding exercise and the program will render static, since it is explicitly labeled as not being interactive.</p>
<program xml:id="coding-exercise-static" language="c" interactive="no">
<code>
#include <stdio.h>
int main(void)
</code>
</program>
</statement>
<solution><p>We're not really sure. Still.</p></solution>
</exercise>
Similar to above, again, but we place the <program>
element inside the <statement>
, not after it as a peer. This signals that this is not a coding exercise and the program will render static, since it is explicitly labeled as not being interactive.
#include <stdio.h>
int main(void)
Solution.
We’re not really sure. Still.
Unit testing can be used to automatically evaluate student work. Unit testing frameworks are available for Python, Java, and C++
Checkpoint 3.4.4. Coding Exercise, with Unit Tests.
View Source for exercise
<exercise xml:id="unit-test-example" label="coding-exercise-python-unit-test" xml:base="rune-examples/activecode-unittest.xml">
<title>Coding Exercise, with Unit Tests</title>
<statement>
<p>Fix the following code so that it always correctly adds two numbers. [Ed. Unit test support is experimental.]</p>
</statement>
<program interactive="activecode" language="python">
<code>
def add(a,b):
return 4
</code>
<tests>
from unittest.gui import TestCaseGui
class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(add(2,2), 4, "Adding two identical integers")
self.assertEqual(add(7,13), 20, "Adding two different integers")
self.assertAlmostEqual(add(2.0,3.0), 5.0, 5, "Adding two decimal numbers")
myTests().main()
</tests>
</program>
<answer>
<p>We're not really sure. But it would begin as follows:</p>
<program language="c">
<code>
#include <stdio.h>
int main(void)
</code>
</program>
</answer>
</exercise>
Fix the following code so that it always correctly adds two numbers. [Ed. Unit test support is experimental.]
Answer.
We’re not really sure. But it would begin as follows:
#include <stdio.h>
int main(void)
Checkpoint 3.4.5. Java Exercise, with Unit Tests.
View Source for exercise
<exercise xml:id="unit-test-example-java" label="coding-exercise-java-unit-test">
<title>Java Exercise, with Unit Tests</title>
<statement>
<p>Unit tests for Java can be written using junit.</p>
</statement>
<program interactive="activecode" language="cpp">
<code>
public class StudentCode
{
public static void main(String[] args)
{
for(int count = 2; count $lt;= 10; count++)
{
System.out.println(count);
}
}
public int adder(int a, int b) {
return a+b;
}
}
</code>
<tests>
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ForLoopTestTester extends CodeTestHelper
{
/* Example test for main method - should pass */
@Test
public void testMain() throws IOException
{
// I wrote a method to run a method and send back the output - only works with String[] args for now
String output = getMethodOutput("main");
String expect = "2\n3\n4\n5\n6\n7\n8\n9\n10\n";
assertEquals("Output doesn't match", cleanString(expect), cleanString (output));
}
@Test
public void testAdder() throws IOException {
StudentCode s = new StudentCode();
String msg = createMessage("Adding 2+2", ""+4, ""+s.adder(2,2));
System.out.println("testing s.adder(2,2)");
assertEquals(msg, 4, s.adder(2,2));
assertEquals("adding 3+3", 6, s.adder(3,3));
}
@Test
public void testContent() throws IOException {
String content = new String ( Files.readAllBytes( Paths.get("StudentCode. java")));
}
}
</tests>
</program>
</exercise>
Unit tests for Java can be written using junit.
public class StudentCode
{
public static void main(String[] args)
{
for(int count = 2; count $lt;= 10; count++)
{
System.out.println(count);
}
}
public int adder(int a, int b) {
return a+b;
}
}
Checkpoint 3.4.6. C++ Exercise, with Unit Tests.
View Source for exercise
<exercise xml:id="unit-test-example-cpp" label="coding-exercise-cpp-unit-test">
<title>C++ Exercise, with Unit Tests</title>
<statement>
<p>Unit tests for C++ can be written using doctest or catch. Doctest based tests build substantially faster than catch based ones.</p>
<p>In an interactive environment, the tests in this exercise will be made visible, but uneditable, so that in the event of a failed test the student can see exactly what is being tested (Doctest does not report on individual passed tests and the feedback on failed tests generally won't make sense without the test itself).</p>
</statement>
<program interactive="activecode" language="cpp">
<code>
// Complete the function to return the sum of two numbers
int add(int a, int b) {
}
</code>
<tests visible="yes">
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
TEST_CASE( "Test the add function" ) {
REQUIRE( add(2, 3) == 5 );
REQUIRE( add(6, 1) == 7 );
REQUIRE( add(-5, 5) == 0 );
}
</tests>
</program>
</exercise>
Unit tests for C++ can be written using doctest or catch. Doctest based tests build substantially faster than catch based ones.
In an interactive environment, the tests in this exercise will be made visible, but uneditable, so that in the event of a failed test the student can see exactly what is being tested (Doctest does not report on individual passed tests and the feedback on failed tests generally won’t make sense without the test itself).
// Complete the function to return the sum of two numbers
int add(int a, int b) {
}
For simple programs, or languages without an available unit testing framework, input-output testing can be done instead. IO testing can only be done on languages that are run on a Runestone server (Java/C/C++/Octave/Python3).
Checkpoint 3.4.7. C++ Exercise, with IO Tests.
View Source for exercise
<exercise xml:id="io-test-example-cpp" label="coding-exercise-cpp-io-tests">
<title>C++ Exercise, with IO Tests</title>
<statement>
<p>Read in an integer <c>n</c>. Print out a n by n square of asterisks.</p>
</statement>
<program interactive="activecode" language="cpp">
<code>
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "*";
}
cout << endl;
}
}
</code>
<tests>
<iotest>
<input>1</input>
<output>
*
</output>
</iotest>
<iotest>
<input>3</input>
<output>
***
***
***
</output>
</iotest>
<iotest>
<input>5</input>
<output>
*****
*****
*****
*****
*****
</output>
</iotest>
</tests>
</program>
</exercise>
Read in an integer n
. Print out a n by n square of asterisks.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "*";
}
cout << endl;
}
}