Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the public functions turnLeft , turnRight , and moveForward . The functions

ID: 3731725 • Letter: A

Question

Add the public functions turnLeft, turnRight, and moveForward. The functions should update the robot's x, y, and heading attributes and should not return anything.

turnLeft and turnRight should update the robot's heading. If the robot's heading is 'N', turnLeftshould result in the new heading being 'W'

moveForward should update the robot's x or y position by 1. If the robot's heading is 'N', moveForward should result in the y position increasing by one. If the robot's heading is 'S', moveForward should result in the y position decreasing by one. (in c++)

Code from previous questions:

class Robot

{

int x;

int y;

char heading;

public:

// constructor

Robot( int x, int y, char heading )

{

this->x = x;

this->y = y;

this->heading = heading;

}

  

// getter methods

int getX()

{

return this->x;

}

int getY()

{

return this->y;

}

char getHeading()

{

return this->heading;

}

// setter method

void setX(int x)

{

this->x = x;

}

void setY(int y)

{

this->y = y;

}

void setHeading(char heading)

{

this->heading = heading;

}

};

Explanation / Answer

class Robot

{

    int x;

    int y;

    char heading;

  

    public:

      

        // constructor

        Robot( int x, int y, char heading )

        {

            this->x = x;

            this->y = y;

            this->heading = heading;

        }

     

        // getter methods

      

        int getX()

        {

            return this->x;

        }

      

        int getY()

        {

            return this->y;

        }

      

        char getHeading()

        {

            return this->heading;

        }

      

        // setter method

      

        void setX(int x)

        {

            this->x = x;

        }

      

        void setY(int y)

        {

            this->y = y;

        }

      

        void setHeading(char heading)

        {

            this->heading = heading;

        }

void turnLeft()

        {

      

            // if robot is currently moving north

      

            if( heading == 'N' )

      

                heading = 'W';

      

            // if robot is currently moving south

      

            else if( heading == 'S' )

      

                heading = 'E';

      

            // if robot is currently moving east

      

            else if( heading == 'E' )

      

                heading = 'N';

      

            // if robot is currently moving west

      

            else if( heading == 'W' )

      

                heading = 'S';

      

        }

      

        void turnRight()

      

        {

      

            // if robot is currently moving north

      

            if( heading == 'N' )

      

                heading = 'E';

      

            // if robot is currently moving south

      

            else if( heading == 'S' )

      

                heading = 'W';

      

            // if robot is currently moving east

      

            else if( heading == 'E' )

      

                heading = 'S';

      

            // if robot is currently moving west

      

            else if( heading == 'W' )

      

                heading = 'N';

      

        }

      

        void moveForward()

      

        {

      

            // if robot is currently moving north

      

            if( heading == 'N' )

       

                y++;

      

            // if robot is currently moving south

      

            else if( heading == 'S' )

      

                y--;

       

            // if robot is currently moving east

      

           if( heading == 'E' )

      

                x++;

      

            // if robot is currently moving west

      

            if( heading == 'W' )

      

                x--;

      

        }

      

};

int main()

{

    Robot robot1 = Robot(9, 1, 'N');

  

    robot1.moveForward();

    robot1.turnLeft();

    robot1.turnRight();

  

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote