CS 111 - 1/25/18 Data Types Continued Some operators do differeent things based on the Data Type x = y / z f = 5 / 9 * ( f - 32 ) would be wrong if integer division occurs (always gives zero) f = (f - 32) * 5 / 9 When working with strings both + and * are defined word1 = "HellO" word2 = "There" word3 = word1 + word2 word4 = word1 * 3 This idea is called operator "overloading" Turtle Drawings Develop some basic programming skills We have a turtle which has a pen on its back. The give the turtle commands such as: - move forward - turn - set pen down - lift pen up We will need to set up the proper environment for the drawings 1. Create the World earth = makeWorld() 2. Create the turtle(s) that live on the world t1 = makeTurtle ( earth ) Then we want to write code to perform the desired algorithm To draw a square, the turtle will need to - draw 4 sides of same length - turn 90 degrees between each side To move forward, use the forward() JES library function syntax: forward ( , ) code: forward ( t1, 175 ) To turn X degrees in a clockwise direction, use turn() JES library function syntax: turn ( , ) code: turn ( t1, 90 ) If wanting to draw a second square (or other shape), we may want to reposition the turtle to another location without drawing a line: use the penUp() and penDown() JES functions syntax: penUp ( ) penDown( ) code: # lift the pen up penUp ( t1 ) # reposition the turtle so no line is drawn turn ( t1, 270 ) forward ( t1, 50 ) turn ( t1, 90 ) # put pen back down on the paper penUDown ( t1 ) To change the color of the pen we will need to use the JES Library METHOD of setPenColor() But First: FUNCTIONS vs METHODS syntax is different Unfortunately, somethings are written as FUNCTIONS, some as METHODS, some as BOTH. forward exists in the JES library as both a FUNCTION and a METHOD syntax as a FUNCTION: forward ( , ) syntax as a METHOD: .forward ( ) code as a FUNCTION: forward ( t1, 175 ) code as a METHOD: t1.forward ( 175 ) Now: the senPenColor() METHOD from the JES library syntax: .setPenColor ( ) JES has some pre-defined colors including: black, white, blue, red, green code to change the turtle's pen to blue: t1.setPenColor ( blue )