Checkpoint 5.2.15. A Python program, including another.
Compute the total amount of money loaned and store it in the variable
loan_total.
@interactive is set to activecode. Some languages can be made interactive on any server, while others require being served from Runestone servers. See Interactive Programs Capabilities in the PreTeXt Guide for a list of what languages are supported in which environs.
@language is not specified, docinfo/programs/@language will be checked to determine what language to assume the code is written in.
print("Hello, World!")
#include <stdio.h>
int main(void)
{
puts("Hello, world!");
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hi world!");
}
}
@compiler-args and @linker-args or @interpreter-args as appropriate to the language. Default values for those options can be set in <docinfo/programs> - any defaults set there will be used for any program that lacks the corresponding attribute.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cin >> name;
cout << "Hello, " << name << endl;
return 0;
}
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
}
}
x = 2 + 2
printf("%d\n", x)
fun main() {
println("Hello, world!!")
}
program HelloWorld;
begin
WriteLn('Hello, world!');
end.
@highlight-lines works on ActiveCode programs, but the highlighted lines are only shown when viewing the initial code. Any version of the code that the reader has edited may have shifted lines of code and thus highlighting might affect the wrong lines, causing confusion.
<preamble> and/or <postamble> which are added to the code that the user writes before it is run. They are visible by default, but can be made invisible with @visible set to "no". When visible, the code editor will prevent those regions from being modified. The indentation for lines in the preamble/code/postamble elements will be calculated relative to each other - make sure to indent them all to a similar extent. (In the source for the sample below, the # TODO... is intentially indented one stop extra so that the userβs code is part of the add function.
<tests> are similar to <postamble> in that it represents code that is added to the users submission. However, <tests> is intended specifically for unit testing code (see examples below for unit testing in Python, Java, C++, SQL). Tests are invisble by default and can be made visible with @visible set to "yes". For historical reasons, the indentation of the <tests> is treated separately from the rest of the program.
<preamble> and <postamble> that are invisible. The user will not see the code that is added to their submission. Not actually useful in this case, but it might be if you wanted to hide boilerplate setup from the reader.
<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. Note that it does not have an @language and is relying on the default one specified in <docinfo/programs>
<program> recycled from before.
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
}
}
@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.4.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.
@autorun to execute on page load and the @codelensto disable the codelens feature.
loan_total.
@hidecode to initially keep the code hidden and @download to enable a file download of the program (that includes all the included code).
loan_total.
@language set to sql uses the @database to load a SQLite database file.
test database table.
.h file that defines an @filename. It has an @xml:id so other elements can reference it. It also has an @label which is itβs unique identifier in the Runestone database. (The label is intentionally different than the xml:id for demonstration purposes, but they can be the same.) The @filename is used to indicate what to name to use when the contents of the element are written to a file on the server - other program code and/or tools on the server can look for it using this name. The @filename need not be uniqueβwe could have multiple programs with @filename="add.h". As we will see below, the author specifies which βversionβ of a file should be used in any given location with its unique xml:id.
<program> is inside of a <listing> so that we have a place to add a title and so that we can reference the code sample, including that title, with an <xref> from elsewhere. The <title> of the <listing> is rendered with the program. It is also used if an <xref> links to the <listing> using @text="title".
int add(int a, int b);
.cpp file. Note that because it is part of an exercise, the @label is applied to the exercise that contains it. The @xml:id and @filename still belong on the program itself.
@extra-compiler-args. These will be added to any default compiler args for the book. In this case, the -c indicates that we only want to compile this file when it is βRunβ and not try to link it or actually run it.
int add(int a, int b) {
return a + b;
}
@add-files is used to specify (by xml:id) the files that need to be added to the program directory. @compile-also specifies files that must be compiled with this source file (they will be assumed to be also part of @add-files). Notice that when using @add-files to reference code we want to include, we use the xml:id of the target program - it has the contents we want to include. When making a textual reference with an <xref> we canβt link to the program, as it lacks the contextual information required to create a valid reference. Instead, we should link to a listing (or some other container) that surrounds the code, like this: ListingΒ 5.2.18 or add.h (version 1) - A very simple header file that lacks header guards.
#include "add.h"
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = 2;
cout << "The sum of " << a << " and " << b << " is " << add(a, b) << endl;
}