← Back to course
Python 11-12 / Week 11 / Monday
1/6
Week 11 Β· Reading Java I

Monday

Same idea, new language
// Same FieldSim ideas, new syntax: types, braces, classes and arrays
⏱ about 30 min

Monday: Same Idea, New Language

The crew gathers around the bench to celebrate. FieldSim has samples, a rover, a grid, searches, sorts and recursion.

Comet spins her pencil. "What can we make next? Could FieldSim be written in another language?"

Wren crosses his arms. "What does the evidence say? We should check that a new language really says the same thing before we trust it."

Nova glides between them and splits her projection in two. Python glows on the left, and a new language glows on the right. "How can I help?" she asks. "This is Java. I compiled and ran it already. Read it beside the Python you know."

Comet squints at all the braces. "Lead programmer, can you spot what matches?"

Why read Java?

AP Computer Science A is a course that uses Java and centers on object-oriented programming. This course prepares for the ideas in it.

You already know those ideas in Python: classes, objects, lists, grids, searching, sorting and recursion. These two weeks show the same ideas in Java.

You will read and trace Java only. You do not need to install anything new.

Two lines, two languages

# ready.py
rover = "Pip"
print(rover + " is ready")
// Ready.java
// The same two lines as ready.py, written in Java.
public class Ready {
    public static void main(String[] args) {
        String rover = "Pip";
        System.out.println(rover + " is ready");
    }
}

Both programs print the same line when run:

Pip is ready
  • Java source code goes in a file ending in .java. The javac compiler turns it into a .class file, and the java launcher runs it.
  • Every Java application has a main method, public static void main(String[] args). The program starts there.
  • Lines that start with // are comments. The compiler ignores them; they are for other programmers, like # comments in Python.
READ THE JAVA
  • Read the question.
  • Tap your answer.
Where does Ready.java start running?
Which Java line does the same job as print in Python?
What does the javac compiler make from Ready.java?
FROM JAVA SOURCE TO OUTPUT
  • ?Write the source code in Ready.java
  • ?The javac compiler turns it into Ready.class
  • ?The java launcher runs the program
  • ?main starts and the line is printed
WHY THIS EXERCISESource code is compiled first. Then the launcher runs it, starting at main.

A loop in both languages

# steps.py
for i in range(3):
    print("step " + str(i))
// Steps.java
public class Steps {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("step " + i);
        }
    }
}
step 0
step 1
step 2
MATCH THE LOOP
  • Read the question.
  • Tap your answer.
Which part of the Java for line matches range(3)?
In Python, indentation groups the loop body. What does Java use?
At your computer
1. In IDLE, make ready.py and steps.py in your fieldsim folder. Run each one with F5.
2. Compare your output with the Java output on this page. They match.
3. Change range(3) to range(5) in steps.py. Which part of the Java for line would change the same way?
4. Now remove the four spaces before print in steps.py. Run it and read the error calmly.
5. Put the spaces back and run it again.
# steps.py (indent removed)
for i in range(3):
print("step " + str(i))
  File "steps.py", line 3
    print("step " + str(i))
    ^^^^^
IndentationError: expected an indented block after 'for' statement on line 2
What is the name of this error?
StatementTrue or false?
Java source code goes in files ending in .java.?
The Java compiler reads comments and runs them.?
Every Java application has a main method.?
Python groups a loop body with indentation.?
WHY THIS EXERCISEThe compiler ignores comments, every application starts in main, and Python groups by indentation.
For a grown-up
This week and next, your student reads Java beside the Python they wrote. Nothing new is installed: no Java, no new editor.
This course prepares for the ideas in AP Computer Science A. It is not affiliated with College Board.

You just read your first Java program, lead programmer. Tomorrow you will meet Java's types and statements.