Skip to main content

Section 5.1 Programs

View Source for section
<section xml:id="inactive-code" xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <title>Programs</title>

  <p>
    First, some samples of programs that are not interactive. They will be
    syntax highlighted if a valid <attr>language</attr> is specified.
    Optionally, lines can be numbered and selected lines highlighted.
  </p>
  <!-- This example also appears in the PreTeXt Guide (doc/guide/author/topics.xml).  Apply any edit to both copies. -->
  <listing>
    <title>A static Java program with highlighted lines</title>

    <program language="java" line-numbers="yes" highlight-lines="2,6-8">
      <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>
  </listing>

  <p>
    Instead of specifying <attr>language</attr> on each program, a default can
    be specified at <c>docinfo/defaults/programs/@language</c>. That value
    will be used for any program that lacks a <attr>language</attr> attribute.
    This sample does not specify it's own <attr>language</attr> and is relying
    on the default set in this book.
  </p>

  <listing>
    <title>Python program, relying on default programs language</title>

    <program line-numbers="yes">
      <code>
              def say_hello():
                  print("Hello, World!")

              say_hello()
      </code>
    </program>
  </listing>

  <listing xml:id="program-notactive-gdscript">
    <title>
      An non-interactive GDScript program, using
      <pubtitle>Runestone</pubtitle>
    </title>

    <program xml:id="gdscript-simple-hello-world" language="gdscript" label="gd-simple-hello-world">
      <code>
                  extends Node

                  func _ready():
                      print("Hello, World!")


      </code>
    </program>
  </listing>
</section>
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.
View Source for listing
<listing xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <title>A static Java program with highlighted lines</title>

  <program language="java" line-numbers="yes" highlight-lines="2,6-8">
    <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>
</listing>
Listing 5.1.1. A static Java program with highlighted lines
View Source for program
<program language="java" line-numbers="yes" highlight-lines="2,6-8" xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <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/defaults/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.
View Source for listing
<listing xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <title>Python program, relying on default programs language</title>

  <program line-numbers="yes">
    <code>
            def say_hello():
                print("Hello, World!")

            say_hello()
    </code>
  </program>
</listing>
Listing 5.1.2. Python program, relying on default programs language
View Source for program
<program line-numbers="yes" xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <code>
          def say_hello():
              print("Hello, World!")

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

say_hello()
View Source for listing
<listing xml:id="program-notactive-gdscript" xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <title>
    An non-interactive GDScript program, using
    <pubtitle>Runestone</pubtitle>
  </title>

  <program xml:id="gdscript-simple-hello-world" language="gdscript" label="gd-simple-hello-world">
    <code>
                extends Node

                func _ready():
                    print("Hello, World!")


    </code>
  </program>
</listing>
Listing 5.1.3. An non-interactive GDScript program, using Runestone
View Source for program
<program xml:id="gdscript-simple-hello-world" language="gdscript" label="gd-simple-hello-world" xmlns:pi="http://pretextbook.org/2020/pretext/internal">
  <code>
              extends Node

              func _ready():
                  print("Hello, World!")


  </code>
</program>
extends Node

func _ready():
    print("Hello, World!")
You have attempted of activities on this page.