gatech-csl / jes

The Jython Environment for Students allows students to write Jython programs that can manipulate pictures, sounds, and videos.
http://mediacomputation.org/
60 stars 38 forks source link

Multiple function definitions permitted in code area... #41

Closed leafstorm closed 10 years ago

leafstorm commented 10 years ago

From peter.de...@gmail.com on April 14, 2010 14:39:10

Multiple functions definitions (with same signature) can be declared in the program area. Since no error seems to be generated, I'm assuming that only the last one is loaded/used. However, from the student's perspective, it compiles fine and promotes ambiguity.

Student code sample: (note the multiple drawBorder definitions used for each picture. Sure, the student doesn't really understand functional abstraction, but the compiler permitted this code.

def main(): loadPictures() saveComic()

setMediaPath() titlePic=makePicture(getMediaPath("pictureTitle.jpg")) firstPic=makePicture(getMediaPath("pictureOne.jpg")) secondPic=makePicture(getMediaPath("pictureTwo.jpg")) thirdPic=makePicture(getMediaPath("pictureThree.jpg")) fourthPic=makePicture(getMediaPath("pictureFour.jpg")) bg=makePicture(getMediaPath("background.jpg"))

def loadPictures(): posterize(thirdPic) chromakey(titlePic,bg) drawBorder(titlePic) drawBorder(firstPic) drawBorder(secondPic) drawBorder(thirdPic) drawBorder(fourthPic) addText(titlePic,234,411,"Bench Hunting: A Comic Strip by XXXX") addText(firstPic,21,445,"This bench is great! Come try it!") addText(firstPic,435,131,"Okay!") addText(secondPic,94,86,"Let's try to find a more comfortable one!") addText(secondPic,486,177,"As long as the bench is fierce") addText(thirdPic,42,139,"This bench is comfortable!") addText(thirdPic,260,68,"This isn't a bench") addText(thirdPic,525,147,"Not fierce enough") addText(fourthPic,162,38,"You know what the most comfortable bench is? A couch.") addText(fourthPic,502,89,"I hope it's animal print") layoutComic()

def posterize(thirdPic): for p in getPixels(thirdPic): red=getRed(p) green=getGreen(p) blue=getBlue(p)

if(red<64):
  setRed(p,31)
if(red>63 and red<128):
  setRed(p,95)
if(red>127 and red<192):
  setRed(p,192)
if(red>191 and red<256):
  setRed(p,223)

if(green<64):
  setGreen(p,31)
if(green>63 and green<128):
  setGreen(p,95)
if(green>127 and green<192):
  setGreen(p,192)
if(green>191 and green<256):
  setGreen(p,223)

if(blue<64):
  setBlue(p,31)
if(blue>63 and blue<128):
  setBlue(p,95)
if(blue>127 and blue<192):
  setBlue(p,192)
if(blue>191 and blue<256):
  setBlue(p,223)

def chromakey(titlePic,bg): for x in range(0,getWidth(titlePic)): for y in range(0,getHeight(titlePic)): p=getPixel(titlePic,x,y) if (getRed(p)+getBlue(p)<getGreen(p)): setColor(p,getColor(getPixel(bg,x,y))) return titlePic show(titlePic)

def drawBorder(titlePic): w = getWidth(titlePic) h = getHeight(titlePic) for x in range(0,w): pix = getPixel(titlePic,x,0) setColor(pix,black) pix = getPixel(titlePic,x,1) setColor(pix,black) pix = getPixel(titlePic,x,h-1) setColor(pix,black) pix = getPixel(titlePic,x,h-2) setColor(pix,black) for y in range(0,h): pix = getPixel(titlePic,0,y) setColor(pix,black) pix = getPixel(titlePic,1,y) setColor(pix,black) pix = getPixel(titlePic,w-2,y) setColor(pix,black) pix = getPixel(titlePic,w-1,y) setColor(pix,black)

def drawBorder(firstPic): w = getWidth(firstPic) h = getHeight(firstPic) for x in range(0,w): pix = getPixel(firstPic,x,0) setColor(pix,black) pix = getPixel(firstPic,x,1) setColor(pix,black) pix = getPixel(firstPic,x,h-1) setColor(pix,black) pix = getPixel(firstPic,x,h-2) setColor(pix,black) for y in range(0,h): pix = getPixel(firstPic,0,y) setColor(pix,black) pix = getPixel(firstPic,1,y) setColor(pix,black) pix = getPixel(firstPic,w-2,y) setColor(pix,black) pix = getPixel(firstPic,w-1,y) setColor(pix,black)

def drawBorder(secondPic): w = getWidth(secondPic) h = getHeight(secondPic) for x in range(0,w): pix = getPixel(secondPic,x,0) setColor(pix,black) pix = getPixel(secondPic,x,1) setColor(pix,black) pix = getPixel(secondPic,x,h-1) setColor(pix,black) pix = getPixel(secondPic,x,h-2) setColor(pix,black) for y in range(0,h): pix = getPixel(secondPic,0,y) setColor(pix,black) pix = getPixel(secondPic,1,y) setColor(pix,black) pix = getPixel(secondPic,w-2,y) setColor(pix,black) pix = getPixel(secondPic,w-1,y) setColor(pix,black)

def drawBorder(thirdPic): w = getWidth(thirdPic) h = getHeight(thirdPic) for x in range(0,w): pix = getPixel(thirdPic,x,0) setColor(pix,black) pix = getPixel(thirdPic,x,1) setColor(pix,black) pix = getPixel(thirdPic,x,h-1) setColor(pix,black) pix = getPixel(thirdPic,x,h-2) setColor(pix,black) for y in range(0,h): pix = getPixel(thirdPic,0,y) setColor(pix,black) pix = getPixel(thirdPic,1,y) setColor(pix,black) pix = getPixel(thirdPic,w-2,y) setColor(pix,black) pix = getPixel(thirdPic,w-1,y) setColor(pix,black)

def drawBorder(fourthPic): w = getWidth(fourthPic) h = getHeight(fourthPic) for x in range(0,w): pix = getPixel(fourthPic,x,0) setColor(pix,black) pix = getPixel(fourthPic,x,1) setColor(pix,black) pix = getPixel(fourthPic,x,h-1) setColor(pix,black) pix = getPixel(fourthPic,x,h-2) setColor(pix,black) for y in range(0,h): pix = getPixel(fourthPic,0,y) setColor(pix,black) pix = getPixel(fourthPic,1,y) setColor(pix,black) pix = getPixel(fourthPic,w-2,y) setColor(pix,black) pix = getPixel(fourthPic,w-1,y) setColor(pix,black)

canvas=makeEmptyPicture((getWidth(titlePic)+getWidth(firstPic)+getWidth(secondPic)+getWidth(t hirdPic)+getWidth(fourthPic)),getHeight(firstPic),lightGray)

def layoutComic(): targetX=0 for sourceX in range(0,getWidth(titlePic)): targetY=getHeight(canvas)-getHeight(titlePic)-30 for sourceY in range(0,getHeight(titlePic)): px=getPixel(titlePic,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY+1 targetX=targetX+1 targetX=getWidth(titlePic) for sourceX in range(0,getWidth(firstPic)): targetY=getHeight(canvas)-getHeight(firstPic) for sourceY in range(0,getHeight(firstPic)): px=getPixel(firstPic,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY+1 targetX=targetX+1 targetX=(getWidth(titlePic)+getWidth(firstPic)) for sourceX in range(0,getWidth(secondPic)): targetY=getHeight(canvas)-getHeight(secondPic) for sourceY in range(0,getHeight(secondPic)): px=getPixel(secondPic,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY...

Original issue: http://code.google.com/p/mediacomp-jes/issues/detail?id=41

leafstorm commented 10 years ago

From bjdorn@gmail.com on April 14, 2010 12:18:38

Hi Peter,

This too is the intended behavior of JES and therefore not a bug. Redefinition of existing identifiers is perfectly legal in Python (you can do this in the interaction pane too). When the program is loaded it is interpreted sequentially, just as if it were entered manually in the interaction pane. Thus, the final definition of the function is the one that is executed when called in the interaction pane. This behavior is the same as other educational IDEs (e.g., IDLE and DrScheme).

Ultimately this is a difference between the dynamic type system and interpreted nature of Python and a statically typed, compiled language like Java. Student errors like this come with the territory.

Cheers, Brian

Status: WontFix