Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Latest commit

 

History

History
151 lines (140 loc) · 5.61 KB

Robot.md

File metadata and controls

151 lines (140 loc) · 5.61 KB

1. Exercise: Simple Robot

Robot models an n-wheeled robot with m ultrasonic sensors.

  • The robot must always have a name to identify it.
  • The number of wheels and ultrasonic sensors on the robot is fixed upon creation.
  • Has an x and y position. The initial position is always (0, 0).
  • Assume distance is in metres and angles are in radians.

Implement Robot according to the following specification:

Method Description Usage Exceptions
Robot(std::string const& name, int const& n) Constructs a robot with a name, n wheels, and no sensors.
Robot robot("robot", 3);
Prints to std::cerr if n is negative.
Robot(std::string const& name, int const& n, int const& m) Constructs a robot with a name, n wheels, and m sensors.
Robot robot("robot", 2, 4);
Prints to std::cerr if n or m is negative.
std::string getName() Gets the name of the robot.
std::string name{robot.getName()};
None
int getNumWheels() Gets the number of robot wheels.
int num_wheels{robot.getNumWheels()};
None
int getNumSensors() Gets the number of robot sensors.
int num_sensors{robot.getNumSensors()};
None
void setName(std::string const& name) Sets the name of the robot to the name provided by the argument.
robot.setName("robot2");
None
void move(double x, double y) Moves the robot to the new position.
Robot r("r", 2, 0); // (0, 0)
r.move(1.1, 2.2); // (1.1, 2.2)
None
std::pair<double, double> localise() Gets the polar coordinates of the robot with respect to (0, 0).
Robot r("r", 2, 0);
r.move(3, 4);
std::pair<double, double> p{r.localise}; // (5, 0.9273)
None

In addition to the above specification, you must:

  • Implement const-correctness for methods and member variables.
  • Mark the default constructor as default or delete.
  • Mark the destructor as default or delete.
  • Use initialiser lists where required.
  • Use delegating constructors where possible.
  • Have correct access specifiers.

2. Exercise: Simple Robot (Continued)

Take the Robot class from lab02-1 and extend it with the following specification:

Method Description Usage Exceptions
Robot(Robot const& r) Copy constructor.
Robot r1("r1", 2, 0);
Robot r2(r1);
None
Robot(Robot&& r) Move constructor.
Robot r1("r1", 2, 0);
Robot r2(std::move(r1));
None
Robot& operator=(Robot const& r) Copy assignment.
Robot r1("r1", 2, 0);
Robot r2("r2", 2, 0);
r1 = r2;
None
Robot& operator=(Robot&& r) Move assignment.
Robot r1("r1", 2, 0);
Robot r2("r2", 2, 0);
r1 = std::move(r2);
None
static std::pair<double, double> cartesian_to_polar(std::pair<double, double> const& val) Converts a cartesian coordinate to polar coordinate.
std::pair<double, double> carte{3, 4};
std::pair<double, double> polar{Robot::cartesian_to_polar(carte)}; // (5, 0.9273)
None
friend std::ostream& operator<<(std::ostream& os, Robot const& r) Writes the name, number of wheels, number of sensors, cartesian coordinates, and polar coordinates of the robot.
Robot r("r", 4, 7);
r.move(3, 4);
std::cout << r; // "r", 4, 7, (3, 4), (5, 0.9273)
None
friend std::istream& operator<<(std::istream& is, Robot& r) Inputs the x then y coordinates for the robot. It is equivalent to r.move(x, y).
Robot r("r", 4, 7);
std::cin >> r; // 3 4
None

In addition to the above specification, you must:

  • Implement const-correctness for methods and member variables.
  • Mark appropriate constructors, assignments, and destructor asdefault or delete.
  • Have correct access specifiers.
  • Write more tests for the new methods.