Table of Contents

Lesson 3

Introduction to OOP

Objectives

Documentation

Java Oracle Tutorial - OOP Concepts
Principiile programarii orientate pe obiecte
Ciclul de viaţă al obiectelor

Learn with robots:

Exercises

For resolving exercises 1 to 5 it is required to add backer.jar library in the path of your project. The library can be downloaded from here. In case you are working with Eclipse you can find here step by step instructions for adding a jar to the build path of a project. The java documentation of the classes part of the backer library can be accessed from here.

Exercise 1

Run the program from the listing bellow and analyse the execution results:

import becker.robots.*;
 
public class DeliverParcel
{
   public static void main(String[] args)
   {  
   	// Set up the initial situation
   	City prague = new City();
      Thing parcel = new Thing(prague, 1, 2);
      Robot karel = new Robot(prague, 1, 0, Direction.EAST);
 
		// Direct the robot to the final situation
      karel.move();
      karel.move();
      karel.pickThing();
      karel.move();
      karel.turnLeft();	// start turning right as three turn lefts
      karel.turnLeft();
      karel.turnLeft();	// finished turning right
      karel.move();
      karel.putThing();
      karel.move();
   }
} 

Exercise 2

Run the program from the listing bellow and analyse the execution results:

import becker.robots.*;
 
public class GoAroundRoadBlock
{
   public static void main(String[] args)
   {
      // Set up the initial situation
      City ny = new City();
      Wall blockAve0 = new Wall(ny, 0, 2, Direction.WEST);
      Wall blockAve1 = new Wall(ny, 1, 2, Direction.WEST);
      Robot mark = new Robot(ny, 0, 2, Direction.WEST);
      Robot ann = new Robot(ny, 0, 1, Direction.EAST);
 
 
      // mark goes around the roadblock
      mark.turnLeft();
      mark.move();
      mark.move();
      mark.turnLeft();     // start turning right as three turn lefts
      mark.turnLeft();
      mark.turnLeft();     // finished turning right
      mark.move();
 
      // ann goes to meet mark
      ann.turnLeft();      // start turning right as three turn lefts
      ann.turnLeft();
      ann.turnLeft();      // finished turning right
      ann.move();
      ann.move();
      ann.turnLeft();
   }
} 

Exercise 3

Write a program that creates a robot at (1, 1) that moves north five times, turns around, and returns to its starting point.

Exercise 4

Implement the program described in Chapter 1 - Learn with robots - Problem Set - 1.12 (page 49)

Exercise 5

Implement the program described in Chapter 1 - Learn with robots - Problem Set - 1.13 (page 49)

Exercise 6

A class called MyPoint, which models a 2D point with x and y coordinates contains:

Write the code for the class MyPoint. Also write a test class (called TestMyPoint) to test all the methods defined in the class.