<section xml:id="coding-exercises">
<title>Coding Exercises</title>
<p>
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.
</p>
<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>
<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>
<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>
<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>
<p>
Unit testing can be used to automatically evaluate student work.
Unit testing frameworks are available for Python, Java, and C++
</p>
<xi:include xmlns="http://www.w3.org/2001/XInclude" href="./rune-examples/activecode-unittest.xml" />
<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>
<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>
<p>
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).
</p>
<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>
</section>
Section 3.4 Coding Exercises
View Source for section
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.
View Source for hint
<hint>
<p>
We didn't really ask you to do anything.
</p>
</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.
View Source for answer
<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>
We’re not really sure. But it would begin as follows:
View Source for program
<program language="c">
<code>
#include <stdio.h>
int main(void)
</code>
</program>
#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.
View Source for answer
<answer>
<p>
We're still not really sure.
</p>
</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.View Source for program
<program xml:id="coding-exercise-static" language="c" interactive="no">
<code>
#include <stdio.h>
int main(void)
</code>
</program>
#include <stdio.h>
int main(void)
Solution.
View Source for solution
<solution>
<p>
We're not really sure.
Still.
</p>
</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. 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.5. 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.6. 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;
}
}