← Back to course
Python 11-12 / Week 11 / Wednesday
3/6
Week 11 · Reading Java I

Wednesday

Read Lab: Sample.java
// Same FieldSim ideas, new syntax: types, braces, classes and arrays
⏱ about 30 min

Wednesday: Read Lab: Sample.java

Comet opens sample.py on one side of the bench. "This was our very first class," she says proudly. "What would it look like in Java?"

Nova projects Sample.java right beside it. "How can I help?" she asks. "This version keeps only the label, the kind and the grams."

Wren reads both, line by line, with his pencil. "What do you notice? There is a word that looks a lot like self."

Comet points at private. "And this word hides the fields, like our underscore did for the rover."

Nova adds one more file, SampleDemo.java, and dims the output. "Predict first," she says. "Then I will show you what my run printed."

Sample.java

// 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";
    }
}
  • A class is the blueprint objects are made from. Fields hold an object's state, and methods define what it does.
  • private means only the class itself can use a member. public means every class can.
  • A constructor has the class's name and no return type. It is called with new, which makes space for the object and sets up its fields.
  • Inside a constructor or method, this means the current object, like self. It is often needed when a parameter has the same name as a field, as in this constructor.
sample.py (Python)Sample.java (Java)
class Sample:public class Sample {
def __init__(self, label, kind, grams, row=0, col=0):public Sample(String label, String kind, int grams) {
self.label = labelthis.label = label;
def is_heavy(self):public boolean isHeavy() {
return self.grams >= 50return grams >= 50;
READ THE CLASS
  • Read the question.
  • Tap your answer.
Which Java word plays the part of self?
Which line is the constructor?
Code outside the class wants the grams. Which method can it call?

Predict SampleDemo

// SampleDemo.java
public class SampleDemo {
    public static void main(String[] args) {
        Sample a = new Sample("S01", "basalt", 42);
        Sample b = new Sample("S03", "clay", 65);
        System.out.println(a.describe());
        System.out.println(b.isHeavy());
        System.out.println(a.getGrams() + b.getGrams());
    }
}
PREDICT EACH LINE
  • Read the question.
  • Tap your answer.
Line 1: what does a.describe() give?
Line 2: what does b.isHeavy() print?
Line 3: what does a.getGrams() + b.getGrams() print?

Here is what SampleDemo printed when Nova ran it:

S01 basalt 42 g
true
107

Build the Python twin

  1. In your fieldsim folder, open a new file and save it as sample_demo.py, next to sample.py.
  2. Type the program below. It makes the same two samples with your Python Sample class.
  3. Predict the three lines. Then run it with F5.
  4. Compare each line with the Java output. Which lines match exactly, and which do not?
  5. Change a so it has a position: Sample("S01", "basalt", 42, 0, 2). Predict line 1, then run it.
  6. Last, change a.grams to a.get_grams() on the last line. Run it, read the error calmly, then change it back.
# sample_demo.py
# The Python twin of SampleDemo.java.
from sample import Sample

a = Sample("S01", "basalt", 42)
b = Sample("S03", "clay", 65)
print(a.describe())
print(b.is_heavy())
print(a.grams + b.grams)
S01 basalt 42 g at (0, 0)
True
107
# sample_demo.py
# The Python twin of SampleDemo.java.
from sample import Sample

a = Sample("S01", "basalt", 42, 0, 2)
b = Sample("S03", "clay", 65)
print(a.describe())
print(b.is_heavy())
print(a.get_grams() + b.grams)
S01 basalt 42 g at (0, 2)
True
Traceback (most recent call last):
  File "sample_demo.py", line 9, in <module>
    print(a.get_grams() + b.grams)
          ^^^^^^^^^^^
AttributeError: 'Sample' object has no attribute 'get_grams'
What is the name of this error?
COMPARE THE TWO RUNS
  • Read the question.
  • Tap your answer.
Java printed true for b.isHeavy(). What did Python print for b.is_heavy()?
Which line matched the Java output exactly?

You read a whole Java class and predicted its output, lead programmer. Tomorrow Nova shows the field grid in Java.

← Tuesday