Programming Project 4 You have learned that organizing your programs in terms of
ID: 3839085 • Letter: P
Question
Programming Project 4
You have learned that organizing your programs in terms of objects is a very powerful idea and
would like to start giving Object Oriented Programming (OOP) a try. You plan to write a simpleprogram, RobotDriver.cpp, which relies on a Robot class for its functionality.
The robots operate in a square grid of size 100 by 100 squares and should never go outside evenif asked to do so. The x and y coordinate for a grid square is a number between 0 and 99. The programcreates a few robot objects and gives them tasks to execute at different locations in the grid. Thedescription of the capabilities of a robot is given to you in the Robot class in the RobotDriver.cpp file.
All you have to do is implement the functions of the robot class and create a few robots in themain function and instruct them to execute tasks.
A sample run for the given main function is pasted below:
**************************************************************Robot Zero located at (0, 0)Task: noneDestination: (0, 0)Energy level: 100****************************************************************************************************************************Robot Rob located at (10, 10)Task: noneDestination: (0, 0)Energy level: 100****************************************************************************************************************************Robot Walle located at (50, 10)Task: noneDestination: (0, 0)Energy level: 100**************************************************************Robot Rob unable to complete task.Destination unreachable with current energy level.18 more energy units are needed to carry out this task.**************************************************************Robot Rob located at (10, 10)Task: Pick up bookDestination: (88, 50)Energy level: 100****************************************************************************************************************************Robot Walle located at (88, 50)Task: Completed: Pick up bookDestination: (88, 50)Energy level: 122****************************************************************************************************************************Robot Rob located at (10, 10)Task: Transfered to WalleDestination: (10, 10)Energy level: 0**************************************************************
/*
* RobotDriver.cpp
*
* Your name here
*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/*
* Point in a 2D grid
*/
class Point {
public:
// Make a point at (0, 0)
Point();
// Make a point at (x, y). If a coord is out of the range [0, 99], that coord is set to 0
Point(int x, int y);
// Get the x coordinate
int getX();
// Get the y coordinate
int getY();
private:
// x coordinate
int xPos;
// y coordinate
int yPos;
};
class Robot {
public:
// Make a robot with the given name located at 0, 0 with 100 energy units
Robot(string name);
// Make a robot with the given name located at x, y with 100 energy units
Robot(string name, int x, int y);
// Get the robot name
string getName();
// Get the x position
int getPosX();
// Get the y position
int getPosY();
// Get the current energy level
int getEnergyLevel();
// Increment the energy level by the given energy amount
void charge(int energyUnits);
// Set the destination where the task needs to be executed
void setDestination(int x, int y);
// Get the x coord of the destination
int getDestX();
// Get the y coord of the destination
int getDestY();
// Set the task description
void setTask(string task);
// Get the task description
string getTask();
/*
* Execute the task only if the robot has enough energy to carry it out. Return true if successful.
* Energy is used up at 1 unit per grid square movement. The robot can only move vertically or horizontally
* in the grid (not diagonally) and 1 square move costs 1 energy unit.
* If the robot does not have enough energy, it does not move and no energy is used up. It prints out
* how many units it is short of energy to carry out the task and returns false.
*
* This function must use the distanceToDestination helper function to calculate how much energy is needed/used up.
*/
bool executeTask();
// Print the current status of the robot
void status();
// Transfer the task and all the energy to the other robot
void transferTaskToFriend(Robot& otherRobot);
private:
// robot name
string name;
// the current position in the grid
Point currentPosition;
// the destination in the grid where the task needs to be executed
Point destinationPosition;
// the energy level
int energyLevel;
// the task description
string task;
/*
* helper function to calculate the distance from the current position to the destination.
* Remember that the robot only moves vertically or horizontally (never diagonal)
*/
int distanceToDestination();
};
int main() {
Robot zero("Zero");
zero.status();
Robot rob("Rob", 10, 10);
Robot walle("Walle", 50, 10);
rob.status();
walle.status();
rob.setDestination(88, 50);
rob.setTask("Pick up book");
rob.executeTask();
rob.status();
rob.transferTaskToFriend(walle);
walle.executeTask();
walle.status();
rob.status();
// make 3 more robots here
// give them good tasks and exercise their functionality
return 0;
}
/*
* Point class implementation
*/
Point::Point() {
xPos = yPos = 0;
}
Point::Point(int x, int y) {
if (x < 0 || x > 99) {
x = 0;
}
if (y < 0 || y > 99) {
y = 0;
}
xPos = x;
yPos = y;
}
int Point::getX() {
return xPos;
}
int Point::getY() {
return yPos;
}
/*
* Robot class implementation
*/
Robot::Robot(string name) {
this->name = name;
currentPosition = Point(0, 0);
destinationPosition = Point(0, 0);
energyLevel = 100;
task = "none";
}
// implement other Robot functions here ...
Explanation / Answer
Robot::Robot(string name) {
this->name = name;
currentPosition = Point(0, 0);
destinationPosition = Point(0, 0);
energyLevel = 100;
task = "none";
}
Robot::Robot(string name, int x, int y) {
this->name = name;
currentPosition = Point(x, y);
destinationPosition = Point(0, 0);
energyLevel = 100;
task = "none";
}
Robot::string getName() {
return name;
}
Robot::int getPosX() {
return currentPosition.getX();
}
Robot::int getPosY() {
return currentPosition.getY();
}
Robot::int getEnergyLevel() {
return energyLevel;
}
Robot::void charge(int energyUnits) {
energyLevel += energyUnit;
}
Robot::void setDestination(int x, int y) {
destinationPosition = Point(x, y);
}
Robot::int getDestX() {
return destinationPosition.getX();
}
Robot::int getDestY() {
return destinationPosition.getY();
}
Robot::void setTask(string task) {
this->task = task;
}
Robot::string getTask() {
return task;
}
Robot::bool executeTask() {
int dist = destanceToDistance();
if (dist <= energyLevel) {
energyLevel -= dist;
return true;
}
cout<< "Cannot execute task. Requires " << (dist - energyLevel) << " energy more." << endl;
return false;
}
Robot::void status() {
cout<< "Robot name: " << name << endl;
cout<< "Robot position: (" << getPosX() << ", " << getPosY() <<")" << endl;
cout<< "Robot destination: (" << getDestX() << ", " << getDestY() <<")" << endl;
cout<< "Robot energy: " << energyLevel << endl;
cout<< "Robot task: " << task << endl;
}
Robot::void transferTaskToFriend(Robot& otherRobot) {
otherRobot.task = task;
otherRobot.charge(energyLevel);
}
Robot::int distanceToDistanc() {
return abs(currentPositon.getX() - destinationPosition.getX()) + abs(currentPositon.getY() - destinationPosition.getY());
}
these are the function you would need to complete your assignment.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.