Question
Refactor the draw method as follows: Currently the draw method has several responsibilities as it must move the turtle to its starting position, draw the
Refactor the draw method as follows:
Currently the draw method has several responsibilities as it must move the turtle to its starting position, draw the first square, move to the starting position for the second square, and draw the second square. This is a violation of the SRP.
To address this issue we will refactor the draw method so that it uses private helper methods to take the necessary actions. When this is done the draw method’s single responsibility will be to orchestrate the movement and drawing, but it will not have the responsibility to do any of the movement or drawing itself.
Step 1. Refactor the code by extracting the first four lines of code in the draw method into a private helper method that moves the turtle to its starting position for the first square. Remember the name of the method should be a verb phrase and describe what the methods does.
After each refactoring, save the file and run the program to make sure it still functions as it did before. If there is an issue, please find and fix it before continuing.
Step 2. The next responsibility that can be refactored to a private helper method is the drawing of the first square. Find the code that draws the first square and extract it into a private helper method.
Note: The code that draws the second square should automatically be replaced with a call to the new helper method.
Step 3. Refactor the draw method so that the code to move the turtle to its starting position for the second square is delegated to a private helper method.
Investigate the drawSquareWithBigTurtle method and notice the six lines of code to move the turtle forward six steps are repeated four times within the method. This is a violation of the DRY principle. Refactor the code so that the six lines of code to move the turtle forward six is placed within a private helper method and the private helper method is called each time the turtle needs to move forward six.
Add functionality to add another turtle that will help with the sketching
Add a second field (data member) to the SketchController class that will store a little turtle. Do this as follows:
At the top of the SketchController class under the line that declares the bigTurtle data member. Add the following line of code below it, which will declare a second field that will be a Turtle object called littleTurtle:
private Turtle littleTurtle;
At this point, the littleTurtle has been declared as a data member, but it does not yet exist as an object to be used by the sketch controller. For it to exist, it must be instantiated. Instantiate the littleTurtle object in the constructor of the SketchController by adding the following line of code after the line of code that instantiates the bigTurtle object:
this.littleTurtle = new Turtle();
Compile and run the code. When executing the program, you should see a second turtle displayed in the upper left-hand corner of the window.
The little turtle and big turtle are both the same size (50) when they are created. Within the SketchController constructor, make the little turtle smaller (size 30) than the big turtle (size 50) by adding two method calls to make the little turtle to decrease its size. You will call the decreaseTurtleSizeBy10 method after the line of code that instantiates the little turtle.
Compile and run the code. When executing the program, verify the turtle in the upper left-hand corner is smaller.
Add code within the constructor to make the big turtle increases its size by 10, so it is size 60 – twice the size of the little turtle.
Compile and run the code.
With the big turtle larger now, the squares it draws will also be larger and now when they are drawn they should be centered in the window.
Now that a little turtle has been created and made size 30. Add functionality to the SketchController so that the little turtle will draw a square within each square that is drawn by the big turtle. The square drawn by the little turtle must be centered within the larger square. The resulting output is shown below:
In doing this, the code will need to be added to the SketchController::draw method after the code that has the big turtle draw the two larger squares.
Make sure that the draw method leverages private helper methods to move the little turtle to its starting positions and draw the square.
Make sure all the resulting code adheres to the SRP and DRY principles. Adherence to the SRP principle is checking to make sure a method doesn’t do too many things by itself, whereas the DRY principle is looking for code that is duplicated.
Step by Step Solution
3.45 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
code solution Leepy CUsersMMANOGARDesktopepy 2712 File Edi...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started