Runfor5 then rotate back the other way

Info icon This preview shows pages 213–216. Sign up to view the full content.

View Full Document Right Arrow Icon
arthurTheTurtle.RunFor(5); ShowPosition(arthurTheTurtle); // Then rotate back the other way arthurTheTurtle.RightMotorState = MotorState.Running; // And run for PI/4 seconds to give us 45 degrees arthurTheTurtle.RunFor(Math.PI / 4.0); ShowPosition(arthurTheTurtle); Dealing with Errors | 189
Image of page 213

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
// And finally drive backwards for a bit arthurTheTurtle.RightMotorState = MotorState.Reversed; arthurTheTurtle.LeftMotorState = MotorState.Reversed; arthurTheTurtle.RunFor(Math.Cos(Math.PI / 4.0)); ShowPosition(arthurTheTurtle); Console.ReadKey(); } private static void ShowPosition(Turtle arthurTheTurtle) { Console.WriteLine( "Arthur is at ({0}) and is pointing at angle {1:0.00} radians.", arthurTheTurtle.CurrentPosition, arthurTheTurtle.CurrentOrientation); } We chose the times for which to run quite carefully so that we end up going through relatively readable distances and angles. (Hey, someone could design a more usable facade over this API!) If we compile and run, we see the following output: Arthur is at (0,0) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle 1.57 radians. Arthur is at (-25,10) and is pointing at angle 1.57 radians. Arthur is at (-25,10) and is pointing at angle 0.79 radians. Arthur is at (-27.5,7.5) and is pointing at angle 0.79 radians. OK, that seems fine for basic operation. But what happens if we change the width of the platform to zero? Turtle arthurTheTurtle = new Turtle { PlatformWidth = 0.0 , PlatformHeight = 10.0, MotorSpeed = 5.0 }; Not only does that not make much sense, but the output is not very useful either; clearly we have divide-by-zero problems: Arthur is at (0,0) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle Infinity radians. Arthur is at ( NaN , NaN ) and is pointing at angle Infinity radians. Arthur is at ( NaN , NaN ) and is pointing at angle NaN radians. Arthur is at ( NaN , NaN ) and is pointing at angle NaN radians. Clearly, our real-world turtle could go badly wrong if we told it to rotate through an infinite angle. At the very least, we’d get bored waiting for it to finish. We should prevent the user from running it if the PlatformWidth is less than or equal to zero. Previously, we used the following code: // Run the turtle for the specified duration public void RunFor(double duration) { 190 | Chapter 6: Dealing with Errors
Image of page 214
if (PlatformWidth <= 0.0) { // What to do here? } // ... } That detects the problem, but what should we do if our particular turtle is not set up correctly? Previously, we silently ignored the problem, and returned as though every- thing was just fine. Is that really what we want to do? For this application it might be perfectly safe, but what if another developer uses our turtle with a paintbrush strapped to its back, to paint the lines on a tennis court? The developer added a few extra moves at the beginning of his sequence, and he didn’t notice that he had inadvertently done so before he initialized the PlatformWidth . We could have a squiggly paint disaster on our hands!
Image of page 215

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 216
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern