Lesson 1.1
Print one line
Part 1: Programs that talkReading, about 3 min2 exercises in this lesson
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
Your first program prints one line
By the end of this you will be able to write a program that prints one line of text, exactly as asked.
A program is a list of instructions that the computer carries out, one after another. The first instruction to learn is the one that shows text on the screen. Printing is how a program tells the person using it what it has done.
Printing a line
Here is a whole program. When it runs, it shows one line.
print("Doors opening.")public class Main {
public static void main(String[] args) {
System.out.println("Doors opening.");
}
}When it runs, the screen shows:
Doors opening.
The text to print goes inside double quotes. The quotes are not printed: they only mark where the text starts and ends.
In Python, one line is a whole program. Java needs more around it: every Java program lives in a class, and it starts running at the method called main. For now, write your instructions between the braces of main and leave the rest as it is. Each Java instruction ends with a semicolon.
Exactly means exactly
A computer compares what your program prints with what the question asks for, character by character. Doors opening. and doors opening. are different lines, and so are Doors opening. and Doors opening. Check three things before you run your program: the capital letters, the spaces between words, and the punctuation.
A common mistake: leaving out a quote mark. Then the computer cannot tell where your text ends, and the program stops with an error before it prints anything. If that happens, look for the quote that has no partner.
Now try it yourself on the questions below.
Exercise 1.1.11.1.1 · Write itFirst line
Write a program that prints exactly this one line:
Ready to code.
- Everything is checked exactly: the capital R, the spaces between the words and the full stop at the end. Spaces after the full stop are ignored.
- Print nothing else: one line, and no other text before or after it.
The program reads no input. In Python, the whole program can be one line. In Java, the page starts you with a class and its main method: write your line between the braces of main, and print with System.out.println.
Running Java here comes later. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run once, as in the example below. Only what it prints is checked.
the one line, exactly (rules 1 and 2)
You type
Nothing
It prints
Ready to code.
Exercise 1.1.21.1.2 · Write itFloor sign
A lift shows a sign when its doors open. Write a program that prints exactly this sign, on one line:
Floor 3: Library
- The words start with capital letters, as shown, and everything else is lower case.
- The colon comes straight after the 3, with no space before it, and one space after it.
- Print nothing else. Spaces at the end of the line are ignored.
The program reads no input. In Python, the whole program can be one line. In Java, the page starts you with a class and its main method: write your line between the braces of main, and print with System.out.println.
Running Java here comes later. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run once, as in the example below. Only what it prints is checked.
the sign, exactly (rules 1 to 3)
You type
Nothing
It prints
Floor 3: Library