How To Add Method To Turtle Class To Draw House
Overview
In the tardily 1960s, Prof. Seymour Papert (who is David'south academic grandfather through his M.S. advisor) developed a robot turtle to help teach immature children to program. This robot has a pocket-sized pen attached to the bottom of it, which would draw a trail on a slice of newspaper every bit the robot moved and turned. Children could so program the robot's movement in social club to have information technology depict a particular moving-picture show. This method of producing drawings is known as turtle graphics, versions of which can be found in pretty much every programming linguistic communication.
In this lab, we have provided you with a Coffee-based simulation of this turtle, with basic functionalities. Your task is to boss this turtle effectually to draw some simple images. This will give you lot a chance to continue practise writing code and calling methods on Java objects.
This assignment should be completed in pairs.
Objectives
- To practice writing Java code.
- To practice calling methods on objects, and passing parameters to those methods.
- To practice working with numbers and variables.
- To exercise writing methods
Required Files
The following file(due south) have been provided for this lab.- Lab2_Turtle.null
Instructions
Below are detailed instructions for completing this lab. For this early assignment, we'll walk you through most of the steps. At that place are a lot of details; be sure and read the instructions advisedly, and permit the states know if anything is unclear!
Function 1: Cartoon a Uncomplicated Bowtie
-
Download
Lab2_Turtle.zipand, like the previous lab, extract it into your network drive. If you still take not configured your network drive, then delight to refer to Lab 1. -
After you open this project, instantiate a new
Turtleobject. Recall to practise this, you right-click on theTurtleclass and cullnew Turtle(). In one case information technology is created, you should go a new window with a turtle actualization near the canvas' bottom right-hand corner, shown below.
-
Play with the
Turtleobject'due south methods from the workbench to go familiar with all its methods. For case, you tin tell the turtle to motion frontward with theforwards()method, and you lot can tell information technology to turn with theright()andleft()methods. If you don't desire the turtle to draw a line when it moves, employ thepenUp()method to raise the pen (and remember to usepenDown()to put it down again). -
Once you lot've become familiarized with the method menus, shut the cartoon window. This should likewise remove the
Turtleobject from the BlueJ workbench. -
At present let's draw something by following the instructions below.
Create a Turtle object named myTurtle Set myTurtle'due south pen colour to "blue" Turn myTurtle left by ninety degrees Move myTurtle forward by 300 Turn myTurtle right by 135 degrees Motility myTurtle forrard past 425 Plough myTurtle left by 135 Move myTurtle forward by 300 Turn myTurtle left past 135 Move myTurtle frontward by 425
-
Wow, that was boring to do by hand, and I can't imagine how long information technology would take u.s. to draw something more sophisticated than a bowtie! The good news is that we can program the figurer to do all that piece of work for us. In this section, we'll run you lot through how it's done.
Become dorsum to the BlueJ project window, and create a
Bowtieobject in the workbench. When you bring upward its methods-carte, y'all'll observe that it doesn't have any! Removing it seems to be your merely option, so exercise so.At present double-click on the
Bowtieclass. This is going to open up upwardly the source code for that class. As yous tin can see, besides the class header and a few comments, not much else has been provided to you (sugariness, thanks David). -
Outset, fill in your names next to
@writerand type in today's date adjacent to@versionin the cake comment at the acme of the file.Now notice how the class header
public class Bowtieis followed by an open-curly brace{and a close-curly brace}. These curly braces group together related Java statements. It'south maxim that annihilation that goes inside these curly braces belong to theBowtieclass. -
We want to create a method chosen
describe()for thisBowtieclass. Have a look at the snippet of code I've provided for you below. Copy and paste all 9 lines (but delete the line numbers to the left) in betwixtBowtie's curly braces. Once y'all've done that, permit'southward re-organize the code by clicking on"Edit" > "Motorcar-Layout". Permit's now learn a little scrap more about this code snippet./** * (Fill in a cursory bescription of what method does) */ public void draw () { //this is the *body* of the describe() method //implement the algorithm to draw a bowtie below }
- Lines one-3 is a block comment that describes the method. Recall that comments are completely ignored by the Java compiler, so they merely serve the purpose of informing people reading the lawmaking. Go ahead and fill in a brief sentence about what this method will perform when information technology is called.
- Line iv is the all-important method signature. Hither's what it says:
- The
publickeyword makes the method visible to users. - The
voidkeyword means this method will non return any values. -
describe()is the method'south name, followed by a listing of parameters it inputs inside the parentheses. In this case, there are no parameters required.
- The
- Lines five-eight: Notice that nosotros've begun some other ready of open- and close-curly braces. Now retrieve what they do: They group together related Java statements, but this time, in the context of the
draw()method. The code that goes between the curlies is known as the method's body. This is where thedraw()method's algorithm will go, instructing a turtle to draw a bowtie.
-
We want the body of the
void draw()method to run the algorithm we merely executed. This ways nosotros need to translate the tongue algorithm nosotros've been following to the Java programming linguistic communication. To get a feel for the translation process, we've gotten things started for you lot by providing the code to create the head and getting information technology into position.
Your chore is to complete the remaining translation, and place the code you write directly within theAlgorithm in Natural Linguistic communication Coffee Algorithm (Unfinished) Create a Turtle object named myTurtle Set myTurtle'due south pen colour to "blue" Plow myTurtle left past 90 degrees Movement myTurtle forward by 300 Turn myTurtle right past 135 degrees Move myTurtle forward past 425 Turn myTurtle left by 135 Motion myTurtle forward by 300 Plow myTurtle left by 135 Move myTurtle forwards by 425
Turtle myTurtle = new Turtle (); myTurtle. setPenColor ( "blue" ); myTurtle. left ( 90 );
void depict()method'southward body. - To help with the rest of the translation, think the following Coffee syntax:
- To call an instance method,
method(), on the object referred to asobjectName, we write:
whereobjectName. method (val1, val2, ...);
val1, val2, ...are the values for the method'southward parameters. Note that the parameter listing may exist empty. - Finally, notice that at that place is a semi-colon at the end of each statement you write.
So that you lot know how to phone call the balance of the methods, beneath is a list of methods' signatures that the turtle supports.
/** * move the turtle forward the specified distance (in pixels) */ public void forwards ( double distance) /** * lowers the pen, so that the turtle will draw when information technology moves. * Annotation that the pen is downwardly by default. */ public void penDown () /** * raises the pen, so that the turtle won't draw when it moves */ public void penUp () /** * sets the colour of the turtle's pen. Possible values are: * "bluish", "cyan", "gray", "magenta", "orange", "pinkish", "ruby-red", * "yellow", and "blackness" */ public void setPenColor (Cord color) /** * rotates the turtle to the left the specified number of degrees */ public void left ( int angle) /** * rotates the turtle to the right the specified number of degrees */ public void right ( int angle)
- To call an instance method,
-
Helpful hint! Endeavor writing simply a few method calls at a time, and then compile and exam your method. Get used to this "write a trivial; test information technology; write a piffling more" workflow---it will make your life much easier in the future.
Subsequently
void depict()has been implemented, close the source code editor, and compile the project once more from the Project window. If yous cannot go it to compile, then it's probably something small-scale you mis-typed. Let usa know if this happens, and we'll help you out. After compiling, instantiate aBowtieobject, and phone callvoid draw(). This time, the drawing should automatically appear, and you've simply completed your outset method in Java!
Part ii: Your Initials
- You should program the turtle to draw out the first initials of you and your partner. So if Joel and David were working together, we'd program the turtle to describe
J + D(Yes, y'all must draw the plus sign). - You need to create a new class, chosen
InitialDrawing. Delete everything but the grade header, and create a newdraw()method. Yous can refer back to theBowtieform for assistance with syntax. - Within this method, yous should programme your turtle using the aforementioned tricks and techniques you worked on to draw that bowtie. Be sure to write a flake of your code, and then compile and run your plan. Using the write/compile/debug cycle!
- You are welcome to go artistic with this--using different colors, decorations, etc.
Function three: House Drawing
-
Your objective is to create a new class, called
HouseDrawing. This class should contain a singledraw()method to create and boss effectually a turtle to describe a house. Upon successful implementation, your cartoon volition look like to the following:
-
Drawing the roof: The pitch (or angle) of the roof should be 45 degrees, with the tallest betoken being lined up to the centre of the business firm. Turning the turtle in the right directions should non be a trouble, but calculating the distance that needs to be drawn can present some challenges. If you become as well far or stop too short (see below), the pitch will be skewed to one side:
The trick to solving this trouble is to observe that the side of the roof is the hypoteneuse, or the longest side (C), of a correct triangle, where the other two sides (A) and (B) have equal length :
-
Luckily, nosotros know the Pythagorean Theorem. Can y'all use it to solve for the hypotenuse? You will demand to become the square root of a number, and Java provides this useful method:
whereMath. sqrt (???)
???is some mathematical expression. Finally, you can plug the equation to discover the hypotenuse right into theforwards()method'sdistanceparameter as follows:
Alternatively, you lot might prefer to store the roof'south distance in a local variable for better lawmaking readability.myTurtle. forrard (Math. sqrt (???));
double roofDistance = Math. sqrt (???); //why double? myTurtle. forward (roofDistance);
Grading
This assignment will be graded out of 10 points.
- [2pt] Your Bowtie course has a
describe()method that draws a Bowtie - [1pt] You have created a new
InitialDrawingclass with a draw() method - [2pt] Your InitialDrawing class can draw your initials wth a plus sign
- [1pt] Y'all accept created a new
HouseDrawingclass with a depict() method - [1pt] Your HouseDrawing class draws a moving picture of a firm
- [1pt] Your house drawing has a roof sloped at 45 degrees
- [1pt] All classes have class comments (and writer names!) filled in
- [1pt] Expert programming mode -- including proper indentation is expected
Grading
Credits
Adapted for use from a previous consignment past Joel Ross.Lab Omnipresence Policies
Attendance is required for lab. Unexcused absenteeism = no credit even if you turned in the lab. Unexcused tardiness = one-half credit.Source: http://mathcs.pugetsound.edu/~dchiu/teaching/archive/CS161sp15/lab2/
Posted by: wrightititan79.blogspot.com

0 Response to "How To Add Method To Turtle Class To Draw House"
Post a Comment