Robots and code

Learn how Java programs are organized and write some code to drive a robot.

The code below creates a robot in a maze. Run the code by pressing the ‘Run’ button after the code.

import com.educative.robot.*;
// A program that creates a robot in a maze
class RobotDemo {
public static void main(String[] args) {
// set up variables to store the robot and the maze
Robot robbie;
Maze maze;
// create the robot and maze, and add
// the robot to the maze
robbie = new Robot(0.0, 0.0, "red");
maze = new Maze(8, 8, 50);
maze.add(robbie);
// set the robot movement direction:
robbie.setDirection("east");
// move things in the maze for one turn:
maze.turn();
}
}

Soon, you’ll add walls to the maze and make the robot run around doing cool things. As a first step, change one line of code so that the robot drives down towards the bottom of the maze. Run your code after you have made the change.

Hint: There are several lines of code that you do not need to understand yet. robbie.setDirection("east"), instructs the robot to move towards the right. Make sure the direction you choose is also all lowercase, and don’t leave out any of the punctuation.

Write your own code: square dance

Lines of code are executed in the order that they appear.

Change the code so that the robot drives in a small square by moving south (downwards), east, north, and then west. You’ll need to use the maze.turn() method after each robbie.setDirection() command. This tells the maze to simulate the motion of any robots for one turns worth of time (about one second).

Hint: You will write eight lines of code. Feel free to copy and paste robbie.setDirection("east"); and maze.turn(); as many times as you need, and then edit the directions.

Code organization

There are two types of code: lines of code that organize the program (in the same way that a recipe book might be organized into chapters and recipes), and lines of code that tell the computer to actually do something.

Comments

Comments help inform a human reader. The computer ignores any text on a line that starts with the characters //. We call this text a comment, and it’s just to help the human reader understand the intent of the code. The computer also ignores blank lines; blank lines just separate the code into chunks that humans can read more easily. Read the comments in the code above now.

Code is organized into classes and methods

Let’s look at the organizational part of our first program first. Here is the organizational part, with the lines of code that do things removed:

// A program 'RobotDemo' that creates a robot in a maze
class RobotDemo {
public static void main(String[] args) {
// the main program code goes here
}
}

The line class RobotDemo { tells the computer to start a new section of code, called a class, with the name RobotDemo. The curly brace { starts this section of code, and the closing curly brace } on line 8 ends this section of code.

Within a class, code is organized into sections called methods. The complicated-looking code public static void main(String[] args) just defines a method with the name main. The method starts with the curly brace on line 4 and ends with the curly brace on line 7.

Lines of code in the main method

To make the organization clear, the code within the class RobotDemo (between its curly braces) is indented one level, and the code within the main method is indented even further. Let’s look at the code in main:

import com.educative.robot.*;
// A program that creates a robot in a maze
class RobotDemo {
public static void main(String[] args) {
// set up variables to store the robot and the maze
Robot robbie;
Maze maze;
// create the robot and maze, and add
// the robot to the maze
robbie = new Robot(0.0, 0.0, "red");
maze = new Maze(8, 8, 50);
maze.add(robbie);
// set the robot movement direction:
robbie.setDirection("east");
// move things in the maze for one turn:
maze.turn();
}
}

The lines 6 through 13 set up a Robot object and a Maze object and stores references to them in variables robbie and maze.

The code: robbie.setDirection("east"); instructs the robot robbie to set its horizontal x-velocity to 1.0, and its vertical y-velocity to 0.0, so that the robot moves eastwards in the maze. Punctuation matters: the dot after robbie is needed, and so are the parentheses and quotes. Case matters: setDirection is the name of the command; setdirection is not. Legal directions are “north”, “east”, “south”, and “west”.

We say that the line robbie.setDirection("east"); calls the method setDirection with the parameter "east" on the object robbie. When a method is called, Java goes and runs some code somewhere else, using the parameters to pass along any information that code needs.

If we set the robot velocity, and then the program ends, the robot doesn’t have time to move. The turn method call tells the maze to animate the motions of everything in the maze for one second; you can think of it as a turn in a game.