Presentation is loading. Please wait.

Presentation is loading. Please wait.

Teaching Control Structures, using objectdraw Chris Nevison Barbara Wells.

Similar presentations


Presentation on theme: "Teaching Control Structures, using objectdraw Chris Nevison Barbara Wells."— Presentation transcript:

1 Teaching Control Structures, using objectdraw Chris Nevison Barbara Wells

2 Problem: Controlling the Elevator Buttons for UP and DOWN Elevator moves in direction according to Button "pushed" (clicked with mouse)

3 Java Constructs boolean values, expression –true or false –some methods return booleans –expressions include comparison of numbers if statement –takes boolean expression –true, execute body –false, execute else part, if any Syntax exactly like C++

4 objectdraw constructs Geometric objects –FilledRect, FilledOval,Text Methods for geometric objects –move, moveTo void, change position parameters determine movement –contains returns boolean –true if Location parameter is inside oval –false otherwise

5 public class ElevatorSystem2 extends WindowController{ private FilledRect elevator; private FilledOval upButton; private FilledOval downButton; private Text upText; private Text downText; public void begin() { elevator = new FilledRect(100, 200, 20, 30, canvas); upButton = new FilledOval(160, 30, 20, 20, canvas); downButton = new FilledOval(160, 60, 20, 20, canvas); elevator.setColor(Color.blue); upButton.setColor(Color.yellow); downButton.setColor(Color.yellow); upText = new Text("UP", 182, 33, canvas); downText = new Text("DOWN", 182, 63, canvas); }

6 public void onMouseClick(Location point) { if(upButton.contains(point)){ elevator.move(0.0, -60.0); } else if(downButton.contains(point)){ elevator.move(0.0, 60.0); } returns boolean moves elevator up moves elevator down

7 Teaching Points Teach if structure with interesting context Immediate visual feedback

8 Exercise: three levels 1. Indicate three floors by drawing three lines see API for Line class 2. Place one button at each floor 3. When button is pushed, elevator should move to that floor

9 Repetition loops repeat action as long as a certain condition is true –condition usually depends on a variable that changes within the loop basic loop is while while( > ){ > }

10 Example: drawing parallel lines public class ParallelLines1 extends WindowController{ public void begin() { Location startLine = new Location(0, 0); Location endLine = new Location(300, 0); int k = 0; while(k < 200){ startLine.translate(0, 10); endLine.translate(0, 10); new Line(startLine, endLine, canvas); k++; }

11 Exercise: Draw something using repetition Scene with grass or fence or other repetitive element polka dot pattern (Dalmation?) –look up java.util.Random


Download ppt "Teaching Control Structures, using objectdraw Chris Nevison Barbara Wells."

Similar presentations


Ads by Google