Skip to main content
Logo image

PreTeXt Sample Book Abstract Algebra (SAMPLE ONLY)

Section 3.1 Programs

First, some samples of programs that are not interactive. They will be syntax highlighted if a valid @language is specified. Optionally, lines can be numbered and selected lines highlighted.
Listing 3.1.1. A static Java program with highlighted lines
View Source for program
<program language="java" line-numbers="yes" highlight-lines="2,6-8" include-source="yes">
    <title>"hi" in Java</title>
    <code>
    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
        }
    }
    </code>
</program>
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
    }
}
Instead of specifying @language on each program, a default can be specified at docinfo/programs/@language. That value will be used for any program that lacks a @language attribute. This sample does not specify it’s own @language and is relying on the default set in this book.
Listing 3.1.2. Python program, relying on default programs language
View Source for program
<program line-numbers="yes" include-source="yes">
    <code>
    def say_hello():
        print("Hello, World!")

    say_hello()
    </code>
</program>
def say_hello():
    print("Hello, World!")

say_hello()