Sei sulla pagina 1di 7

Assignment Report

Background:
We have to work with the Robot problems. The environment of the Robot is
intersections connected by avenues and streets on which robots travel, and where there
may be several kinds of things. The environment consists of roads. Some roads, called
streets, run east and west, while other roads, called avenues, run north and south.
Streets and avenues are both numbered starting with 0. Street 0 is located on the north
(top) side, while Avenue 0 runs along the west (left) side. The place where these two
roads meet is called the origin. Intersections may be surrounded by walls on one or
more sides. A wall stands at an edge of an intersection and blocks robots from entering
or leaving the intersection in that direction. Robots can’t push walls out of the way.
When a robot receives a turnLeftmessage, it responds by turning left 90 degrees. When
a robot facing north receives the turnLeftmessage, it turns to face west. Robots always
start out facing one of the four compass points: north, south, east, or west. When a
robot receives a moving message, it attempts to move from its current intersection to
the next intersection in the direction it is facing. It remains facing the same direction.
We were given the two tasks in the assignment to perform which are as follows;

Task1: Write a program that creates a robot. The robot, named


Navigator begins with the initial situation shown in Figure
1. Instruct the robot to go around the walls counter-clockwise and
return to its starting position.

Task2: Write a program that constructs a mountain (north is up). Then instructs a robot,
named Climber to pick up a flag (a Thing), climb the mountain, and plant the flag at the
top, descending down the other side. Figure 2 shows the initial and final states for a
robot that climbs the mountain. Climber must follow the face of the mountain as closely
as possible, as shown by the path shown in the final situation.
Literature Review:
There are many solutions present for such problems available online. After thorough
research, I came to know that there are two major solutions to these problems. One of
the solutions for this problem is to write the complete code myself then use it and the
other one is to use the already written code with customization according to our
requirements.

So, there was a library available online for learning Java Programming i.e.
Becker.Robot​, it has the Robot class implemented already with the basic structure and
basic operation [1]. Therefore, I used the basic skeleton for the code from becker.Robot
class.

Programming Approach:
I choose the simple programming approach i.e. ​Write and Use​. that means firstly, I saw
the becker.Robot class, then I customized it according to my need. As this is the best
programming practice is “Don’t reproduce the complex code yourself”. So, I use the
becker.Robot for the basic Robot class then customized it for me. The basic UML
diagram of the application is as follows;
Design of the Application:
The application has five classes named as;
● Robot.java
● Thing.java
● Climber.java
● Navigator.java
● Main.java

Robot.java: this class has the implementation of the robot moving in the city. It is
extended the becker.Robot class and get the basic functionalities from that class. But it
has some external features for this project such as turnRight(), moveForward(int steps).
- turnRight(): it will guide the robot to take a right turn in the grid. And use the
turnLeft() function from the parent class to accomplish this task.
- moveForward(int steps): it will take the no. of steps from the user and move that
number of steps in the position of robot’s head.

Thing.java: this class inherits the original becker.Robot.Thing class but has an extra
functionality of changing the color of the thing.

Navigator.java: this class has the implementation of the question no. 1. In this class,
there is a constructor that creates the initial situation of the city. Then, there is a function
that has the solution to the problem and moves the robot accordingly. This is the
implementation().

Climber.java: this class has the implementation of the question no. 2. In this class,
there is a constructor that creates the initial situation of the city. Then, there is a function
that has the solution to the problem and moves the robot accordingly. This is the
implementation().

Main.java: ​this class has the driver function that creates one object for Navigator and
the other on for Climber. It passes an instance of the City to both of them.
Question no. 1:

Question no. 2:
References:
[1]: Byron Weber Becker, ​“What is Robots?”
Appendix:

Robot.java super(ny, 3,1);


this.ny = ny;
public class Robot extends becker.robots.Robot{ // Set up the initial situation
Wall blockAve0 = new Wall(ny, 3, 2, Direction.WEST);
Robot(City ny, int i, int i0, Direction direction) { Wall blockAve1 = new Wall(ny, 3, 2, Direction.NORTH);
super(ny,i,i0,direction); Wall blockAve2 = new Wall(ny, 2, 3, Direction.WEST);
Wall blockAve3 = new Wall(ny, 1, 3, Direction.WEST);
this.setColor(Color.BLACK);
Wall blockAve4 = new Wall(ny, 1, 3, Direction.NORTH);
} Wall blockAve5 = new Wall(ny, 1, 3, Direction.EAST);
Wall blockAve6 = new Wall(ny, 2, 4, Direction.NORTH);
@Override Wall blockAve7 = new Wall(ny, 2, 4, Direction.EAST);
public void move() { Wall blockAve8 = new Wall(ny, 3, 4, Direction.EAST);
this.rb = new Robot(ny, 3, 0, Direction.EAST);
super.move();
} }

@Override public void implement(){


public void turnLeft() {
// navigator goes around the walls with the following
super.turnLeft();
lines of code
}
//move the navigator 1 step ahead per move()
public void turnRight(){ this.rb.moveForward(1);
super.turnLeft();
super.turnLeft();
if(this.rb.canPickThing()){
super.turnLeft(); this.rb.pickThing();
} }

public void moveForward(int i){ //turn the rb 90 degree to the south


this.rb.turnLeft();
for (int j = 0; j < i; j++) {
this.move(); //move the navigator 1 step ahead per move()
} this.rb.moveForward(1);
}
} //turn the navigator 90 degree to the east
this.rb.turnRight();

//move the navigator 1 step ahead per move()


this.rb.moveForward(1);

//turn the navigator 90 degree to the north


this.rb.turnLeft();

//move the navigator


this.rb.moveForward(2);

Climber.java: //turn Right the robot


public class Climber extends Thing { this.rb.turnRight();
public City ny;
public Robot rb; //stop at the starting position per move()
this.rb.moveForward(1);
Climber(City ny) {
this.rb.putThing();
this.rb.moveForward(1);
//move the navigator 1 step ahead per move()
this.rb.moveForward(1); this.rb.turnRight();

//turn the navigator 90 degree to the west this.rb.moveForward(2);


this.rb.turnRight();
this.rb.turnLeft();
this.rb.moveForward(1); }

this.rb.turnLeft(); }

Potrebbero piacerti anche