Lesson 3

Introduction to OOP

Objectives

  • understanding the concepts of class and object
  • using contructors to initialize objects
  • overloading methods
  • static keyword
  • this keyword

Documentation

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

Exercise 5

Exercise 6

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

  • Two instance variables x (int) and y (int).
  • A “no-argument” (or “no-arg”) constructor that construct a point at (0, 0).
  • A constructor that constructs a point with the given x and y coordinates.
  • Getter and setter for the instance variables x and y.
  • A method setXY() to set both x and y.
  • A toString() method that returns a string description of the instance in the format “(x, y)”.
  • A method called distance(int x, int y) that returns the distance from this point to another point at the given (x, y) coordinates.
  • An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint instance another.

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