← Back to course
Python 11-12 / Week 11 / Friday
5/6
Week 11 · Reading Java I

Friday

Trace Friday: Python or Java?
// Same FieldSim ideas, new syntax: types, braces, classes and arrays
⏱ about 30 min

Friday: Trace Friday: Python or Java?

Wren pins two sheets of graph paper to the wall, one titled Python and one titled Java. "Trace Friday," he says. "What does the evidence say about each language?"

Comet already has a list going. "Java has braces and semicolons. Python has indentation and colons."

Nova projects a short Java loop and a Java rover counter. "How can I help?" she asks. "Trace them on paper first. Then compare them with rover.py."

Comet tucks her pencil behind her ear. "I will trace before I run this time," she says with a grin.

Wren hands you the first trace table.

Trace a Java loop

// LoopTrace.java
public class LoopTrace {
    public static void main(String[] args) {
        int battery = 100;
        int steps = 0;
        while (battery >= 30) {
            battery -= 25;
            steps++;
        }
        System.out.println(steps + " steps, battery " + battery);
        String route = "";
        for (int i = 1; i <= 3; i++) {
            route = route + "F";
        }
        System.out.println(route);
    }
}
Check battery >= 30battery after the bodysteps after the body
100 >= 30 is true751
75 >= 30 is true502
50 >= 30 is true253
25 >= 30 is falseloop endsloop ends
PICK THE OUTPUT
  • Read the question.
  • Tap your answer.
What is the first line LoopTrace prints?
What is the second line?
3 steps, battery 25
FFF

A static field and a class variable

A static field in Java is a class variable: one copy shared by every object. Rover.rovers_built is a class variable in your rover.py.

MiniRover is a small Java class written for this comparison. It counts rovers the same way.

// MiniRover.java
// A small rover class that counts rovers, like Rover.rovers_built.
public class MiniRover {
    public static int roversBuilt = 0;
    private String name;

    public MiniRover(String name) {
        this.name = name;
        roversBuilt++;
    }

    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        MiniRover a = new MiniRover("Pip");
        MiniRover b = new MiniRover("Dot");
        MiniRover c = new MiniRover("Moss");
        System.out.println(a.getName() + " " + b.getName() + " " + c.getName());
        System.out.println(MiniRover.roversBuilt);
    }
}
# count_rovers.py
# The Python twin of MiniRover.java, with one test.
from rover import Rover

a = Rover("Pip")
b = Rover("Dot")
c = Rover("Moss")
print(a.name, b.name, c.name)
print(Rover.rovers_built)
assert Rover.rovers_built == 3
print("Count test passed")
COMPARE THE COUNTERS
  • Read the question.
  • Tap your answer.
Which Java word makes roversBuilt a class variable?
Which Python line matches roversBuilt++?
Both programs make three rovers. What count do both print?
At your computer
1. Type count_rovers.py in your fieldsim folder and run it. The assert is a one-line test.
2. Add a fourth rover, d = Rover("Fern"), below c. Run it and read what the assert says.
3. Change the test to match the new count, and run it again.
4. Last, misspell the class variable as Rover.rovers_buit in the print line, not the assert. Read the error calmly, then fix it.
# count_rovers.py
# The Python twin of MiniRover.java, with one test.
from rover import Rover

a = Rover("Pip")
b = Rover("Dot")
c = Rover("Moss")
d = Rover("Fern")
print(a.name, b.name, c.name)
print(Rover.rovers_buit)
assert Rover.rovers_built == 4
print("Count test passed")
Pip Dot Moss
Traceback (most recent call last):
  File "count_rovers.py", line 10, in <module>
    print(Rover.rovers_buit)
          ^^^^^^^^^^^^^^^^^
AttributeError: type object 'Rover' has no attribute 'rovers_buit'. Did you mean: 'rovers_built'?
FeaturePythonJava
Group a blockindentationcurly braces
The current objectselfthis
Make a variablegrams = 7int grams = 7;
Where a program startsthe first line of the filethe main method
Before it runsIDLE runs the filejavac compiles it first
  1. Java source goes in .java files. javac compiles them, and the java launcher runs them, starting in main.
  2. Java is statically typed: every variable is declared with its type. Statements end with semicolons.
  3. Python groups blocks with indentation. Java groups them with curly braces.
  4. In Java, private hides a field, this means the current object, and a constructor is called with new.
  5. A Java array has a fixed length. A 2D array is an array of arrays, indexed row first.
  6. A static field is a class variable, like Rover.rovers_built.

Excellent tracing, lead programmer. You can now read a Java class, a Java array and a Java loop.

← Thursday