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.
// 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 >= 30 | battery after the body | steps after the body |
|---|---|---|
| 100 >= 30 is true | 75 | 1 |
| 75 >= 30 is true | 50 | 2 |
| 50 >= 30 is true | 25 | 3 |
| 25 >= 30 is false | loop ends | loop ends |
3 steps, battery 25 FFF
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") # 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'? | Feature | Python | Java |
|---|---|---|
| Group a block | indentation | curly braces |
| The current object | self | this |
| Make a variable | grams = 7 | int grams = 7; |
| Where a program starts | the first line of the file | the main method |
| Before it runs | IDLE runs the file | javac compiles it first |
Excellent tracing, lead programmer. You can now read a Java class, a Java array and a Java loop.