Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Classes and Objects  ›  Lesson

Attributes and Methods

Attributes and Methods 13 minFields store each object's state and methods act on that state, with the header stating the parameters a method takes and the type of value it returns.

Attributes and Methods

A drink that only stores data is half an object. Brew Bar's orders need to do things: add a cup, work out their total, print a receipt. Putting that behaviour inside the class, next to the data it uses, is what makes objects worth having.

The rule

Fields are what an object knows; methods are what it does.

Attributes, usually called fields, hold each object's state. Methods are the actions, and they can read and change that state directly:

public class Order {
    String customer;
    int cups;
    int unitCents;

    Order(String customer, int unitCents) {
        this.customer = customer;
        this.unitCents = unitCents;
        this.cups = 1;
    }

    void addCup() {
        cups++;
    }

    int totalCents() {
        return cups * unitCents;
    }

    String receipt() {
        return customer + ": " + cups + " x " + unitCents + " = " + totalCents();
    }
}

Read a method header from left to right:

  • Return type: what the method hands back. int totalCents() promises an int, and every path through it must return one. void means it hands back nothing.
  • Name: a verb or question, in camelCase, such as addCup or isLarge.
  • Parameters: the inputs in brackets, each with a type. addCups(int n) would take one; () means none.

Inside a method, a field name such as cups means this object's cups. Methods can also call each other: receipt() uses totalCents() rather than repeating the multiplication.

Calling a method uses a dot on a particular object:

Order o = new Order("Asha", 300);
o.addCup();
System.out.println(o.receipt());   // Asha: 2 x 300 = 600

The shortcut

Before writing a method, say its header as a sentence: "Given ___, it gives back ___." The blanks are the parameters and the return type. If the second blank is "nothing, it just changes the order", the return type is void.

Designing a "remove cup" methodStep 1 of 4
  1. Say the sentence

    "Given nothing, it gives back nothing; it just lowers the cup count."

Drills

Answer fast — trust the rule.

quick drill

Order o = new Order("Asha", 300); o.addCup(); o.addCup(); What is o.totalCents()?

quick drill

What happens with int totalCents() { System.out.println(cups * unitCents); }?

quick drill

What happens with int n = o.addCup();?

quick drill

Which header fits "given a number of extra cups, it gives back the new total in cents"?

Remember

  • Fields are what an object knows; methods are what it does.
  • Say "given ___, it gives back ___" to write the header.
  • Traps: forgetting return in a non-void method, and using a void result as a value.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDesign two Order methods

Design two methods for the Order class: one that answers whether the order is large (5 cups or more), and one that applies a discount of a given number of cents to each cup. Give each header, explain your choice of return type and parameters, and describe what each body does.