Example. Methods

[comp1110-84]

Building on our example from Example [comp1110-82], imagine we want to just have a way to print a nice description for any of our basic Person objects.

To do this, we'll add a printDescription method to the class:

class Person { int age; String name; void printDescription() { IO.println(name + " (Age " + age + ")"); } }

We can try this with the following main function:

void main() { Person p = new Person(); p.name = "Timmy"; p.age = 5; p.printDescription(); }

This will print:

Timmy (Age 5)
bars magnifying-glass caret-down plus minus arrow-right xmark