Course of Raku / Objects, I/O, and exceptions / Classes and objects / Methods / Exercises

A walking robot

Problem

Define a class Robot with a changeable position attribute that defaults to 0, and a method move that advances the position by a given number of steps. Make the number of steps default to 1, so that calling move with no argument moves the robot one step.

Create a robot, move it 5 steps, then move it with no argument, then move it 2 more steps, and print its final position.

Example

The program prints:

8

Solution

See the solution

Course navigation

Quiz — Methods   |   Solution: A walking robot