Skip to main content
Logo image

PreTeXt Sample Book: Abstract Algebra (SAMPLE ONLY)

Section 5.1 ActiveCode

Programs in supported languages are made interactive in HTML, on request.
Listing 5.1.1. An interactive Python program, using Runestone
Otherwise they are rendered as text with syntax coloring. Either way, if a @language is not specified, docinfo/programs/@language will be checked to determine what language to assume the code is written in.
print("Hello, World!")
Listing 5.1.2. An interactive Python program, using Runestone
A C program will only be interactive if hosted on a Runestone server.
Listing 5.1.3. An C program, interactive on a Runestone server
A Java program will only be interactive if hosted on a Runestone server.
Listing 5.1.4. A Java program, interactive on a Runestone server
Javascript programs are made interactive in HTML, on request.
Listing 5.1.5. An interactive JavaScript program, using Runestone
A <program> is not interactive, by default, no matter where it is hosted.
import javax.swing.JFrame;  //Importing class JFrame
import javax.swing.JLabel;  //Importing class JLabel
public class HelloWorld {
    public static void main(String[] args) {
        JFrame frame = new JFrame();           //Creating frame
        frame.setTitle("Hi!");                 //Setting title frame
        frame.add(new JLabel("Hello, world!"));//Adding text to frame
        frame.pack();                          //Setting size to smallest
        frame.setLocationRelativeTo(null);     //Centering frame
        frame.setVisible(true);                //Showing frame
    }
}
Listing 5.1.6. A static Java program
An Octave program will only be interactive if hosted on a Runestone server. Octave is meant to be a drop-in replacement for Matlab.
Listing 5.1.7. A simple Octave program
A language not supported by Runestone Services will be rendered static.
program HelloWorld;
begin
  WriteLn('Hello, world!');
end.
Listing 5.1.8. A Pascal program that cannot be interactive on Runestone
The following Python program is in a <listing> since we will want to reference it shortly. The program does not do very much, it just defines four variables whose values are lists of statistics. It should run, and there will be no syntax errors, but it is a bit boring since there is no output.
If you examine the source, you will also notice the <program> lacks a @language attribute. It is relying on the docinfo/programs/@language value that is in bookinfo.xml. If present, that attribute will be used for any programs or Runestone interactives that lack a @language.
Listing 5.1.9. A Python program that defines some statistics
An ActiveCode running on the Runestone server (rather than in a browser) can be provided with attributes that are flags to influence the compiler and linker, as in this <program> recycled from before.
Listing 5.1.10. A Java program, interactive on a Runestone server, with compiler and linker flags
Now a programming exercise. The program upcoming is going to include all the code of the program preceding. This is accomplished with an @include attribute on the including program whose value is the @xml:id of the included program. So by running the next program, it should pass all of its three tests (for example another example using unit tests, see Checkpoint 5.3.4). Now reload the page, do not run the program in the listing, and then see that the program in the exercise still runs correctly.
You’ll see nothing that tells the reader that the one chunk of code is prefacing the other. And in static formats it might be even less obvious. So you will want to say something to alert the reader. Here it is easy: Checkpoint 5.1.11 includes all the code from Listing 5.1.9.

Checkpoint 5.1.11. A Python program, including another.

Compute the total amount of money loaned and store it in the variable loan_total.
Exact same exercise again, but now we include two programs. We first get the simple “Hello, world!” program at Listing 5.1.1 and then the same program defining the variables with lists of statistics at Listing 5.1.9. So the output just includes the extra result from the print() statement.

Checkpoint 5.1.12. A Python program, including two others.

Compute the total amount of money loaned and store it in the variable loan_total.
Here is an activecode with @language set to sql uses the @database to load a SQLite database file.

Checkpoint 5.1.13. An SQL program that uses an SQLite database file.

Select all the columns of all the rows in the test database table.
A nonsense paragraph just to check on spacing. A nonsense paragraph just to check on spacing. A nonsense paragraph just to check on spacing. A nonsense paragraph just to check on spacing.
You have attempted of activities on this page.