Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C + + PLEASE, READ CAREFULLY ( USE ATTACHED TEMPLATES ) For bug.h use this template PLEASE - #ifndef BUG _ H #define BUG _

C++ PLEASE, READ CAREFULLY (USE ATTACHED TEMPLATES) For bug.h use this template PLEASE- #ifndef BUG_H #define BUG_H class Bug public: private: ; #endif for bug.cpp use this template PLEASE #include "bug.h" To check code use this template-// Do not edit this file. #include "bug.h" #include #include using namespace std; class Tester public: // Checks answer and prints success or failed bool test(double got, double expected); private: size_t test_num =1; double thresh =1.0/1024.0; ; bool Tester::test(double got, double expected) cout <<"==== Test case "<< test_num <<"===="<< endl; bool success = true; if (abs(got - expected)>= thresh) cout << "Not passed yet. Keep trying!" << endl; cout <<" Got: "<< got << endl; cout <<" Expected: "<< expected << endl; success = false; cout << endl; if (success) cout << "Passed." << endl << endl; test_num++; return success; int main() Tester tester; bool all_passed = true; // Test case 1 Bug bugsy1= Bug(10); bugsy1.move(); bugsy1.move(); bugsy1.turn(); bugsy1.move(); bugsy1.move(); bugsy1.move(); bool test_case_passed = tester.test(bugsy1.get_position(),9); all_passed = test_case_passed all_passed; // Test case 2 Bug bugsy2= Bug(0); bugsy2.move(); bugsy2.move(); bugsy2.move(); bugsy2.move(); bugsy2.move(); test_case_passed = tester.test(bugsy2.get_position(),5); all_passed = test_case_passed all_passed; // Test case 3 Bug bugsy3= Bug(-100); bugsy3.turn(); bugsy3.move(); bugsy3.move(); bugsy3.move(); bugsy3.move(); bugsy3.turn(); bugsy3.move(); test_case_passed = tester.test(bugsy3.get_position(),-103); all_passed = test_case_passed all_passed; // Test case 4 Bug bugsy4= Bug(-3); tester.test(bugsy4.get_position(),-3); all_passed = test_case_passed all_passed; // Test case 5 Bug bugsy5= Bug(-7); bugsy5.turn(); bugsy5.turn(); bugsy5.turn(); test_case_passed = tester.test(bugsy5.get_position(),-7); all_passed = test_case_passed all_passed; if (all_passed) cout << "Hooray, you passed all tests!" << endl; return 0; Problem 1: Bug Please turn in bug.h and bug.cpp that contains your implementation of the Bug class. I provided starter code bug.h and bug.cpp. I also provided bug_main.cpp, which contains the main function and testers. You should not modify bug_main.cpp.- In bug.h, you need to provide the class definition - In bug. cpp you need to provide implementations of the member functions. - For bug_main.cpp, you will not edit this, but you will run this and it will test your implementation. Please pass all tests before submitting your bug. h and bug. cpp. Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to flip its direction (if the current direction is right, turn to left; if the current direction is left, turn to right). In each move, its position changes by one unit in the current direction. Provide a constructor Bug(int initial_position) and member functions void turn() void move() int get_position() const Sample usage: Bug bugsy(10); bugsy.move(); // Now the position is 11 bugsy.turn(); bugsy.move(); // Now the position is 10- Note: There will be no user input. Just run the bug_main. cpp and check your answers.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

Students also viewed these Databases questions