← Back to course
Python 11-12 / Week 11 / Mission Quest
6/6
Week 11 · Reading Java I

Mission Quest

Mission Quest: Match the lines
// Same FieldSim ideas, new syntax: types, braces, classes and arrays
⏱ about 30 min

Saturday: Mission Quest: Match the Lines

Saturday morning, Comet arrives with a stack of blank index cards. "Let us make a matching game," she says.

Wren writes the lines of describe() on the cards, Python in pencil and Java in pen. "What does the evidence say?" he asks. "Every Python line should have a Java partner, or a reason it has none."

Nova projects the two methods, then fades them out so only the cards are left. "How can I help?" she asks. "Read each card aloud. Listen for the semicolons and the braces."

Comet shuffles the cards and hands them to you. "Lead programmer, play it with your family."

Here are the two files the cards come from: your sample.py, then Nova's Sample.java.

# sample.py
# A Sample is one rock or soil token the rover picks up.


class Sample:
    """One sample token from the test field."""

    def __init__(self, label, kind, grams, row=0, col=0):
        self.label = label
        self.kind = kind
        self.grams = grams
        self.row = row
        self.col = col

    def describe(self):
        return f"{self.label} {self.kind} {self.grams} g at ({self.row}, {self.col})"

    def is_heavy(self):
        return self.grams >= 50
// Sample.java
// The same Sample class as sample.py, written in Java.
public class Sample {
    private String label;
    private String kind;
    private int grams;

    public Sample(String label, String kind, int grams) {
        this.label = label;
        this.kind = kind;
        this.grams = grams;
    }

    public String getLabel() {
        return label;
    }

    public int getGrams() {
        return grams;
    }

    public boolean isHeavy() {
        return grams >= 50;
    }

    public String describe() {
        return label + " " + kind + " " + grams + " g";
    }
}
◇ PYTHON AND JAVA LINE MATCH
You need: index cards or slips of paper, and a pencil.
Copy lines from the two files above onto cards, one line per card.
Python: the def describe(self): line and its return line. Java: the public String describe() { line, its return line and its closing brace.
Do the same for is_heavy and isHeavy: four Python lines in all, and six Java lines in all.
Shuffle the cards. Your family member reads one card aloud, and you find its partner in the other language.
For a Java card with no Python partner, such as a closing brace, explain why Python does not need it.
Swap roles and play again.
CHECK THE MATCHES
  • Read the question.
  • Tap your answer.
Which Java line matches def is_heavy(self):?
Why does the Java describe() need a closing brace card?
Which Java card ends with a semicolon?
For a grown-up
This week your student read Java beside the Python they wrote: types, semicolons, braces, a class and a grid.
Ask them to point out one Java line and say what the matching Python line is.
Talk about it: why might people write the same idea in two different languages?

Great matching, lead programmer. Next week the crew reads lists and algorithms in Java, and ships FieldSim.

← Friday