120 likes | 210 Views
ITK 168 Even more control structures. Dr. Doug Twitchell 10/11/05. The test. Average: 83.9 Questions?. Using loops in a GUI. StickFigure example Horizontal stick figures Vertical stick figures. switch. switch(wallDir){ case SOUTH: for(int i = 0; i < length; i++){
E N D
ITK 168 Even more control structures Dr. Doug Twitchell 10/11/05
The test • Average: 83.9 • Questions?
Using loops in a GUI • StickFigure example • Horizontal stick figures • Vertical stick figures
switch switch(wallDir){ case SOUTH: for(int i = 0; i < length; i++){ Wall wall = new Wall(this, street + i, avenue, pDir); } break; case EAST: for(int i = 0; i < length; i++){ Wall wall = new Wall(this, street, avenue + i, pDir); } break; case WEST: for(int i = 0; i < length; i++){ Wall wall = new Wall(this, street, avenue - i, pDir); } break; default: for(int i = 0; i < length; i++){ Wall wall = new Wall(this, street - i, avenue, pDir); } }
Printing to the console • System.out.println(); • Demo • Demo with switch
Variables • Declaration: type name; • int num; • Assignment: name = value; • num = 5; • Combined: type name = value; • int num = 5;
Variables • Kinds of variables • parameters • public void move(int numTimes){ … • temporary • int numMoves = numTimes; • instance variables (new!!) • private int street; • constants (new!!) • public static final in MAX_MOVES = 20;
Instance Variables • Represent an attribute of a class • color • current street • name • Are declared outside of the methods • Have a visibility modifier
Instance variables – declaring and assigning public class Robot … { private int street; private int avenue; public Robot(…, int aStreet, int anAvenue,…){ this.street = aStreet; this.avenue = anAvenue; } … }
Constants • Never change during the course of a program • Declared at class level • Are declared static and final • Examples: • π • private static final double PI = 3.14159; • number of points per three pointer • private static final int THREE_POINTER_POINTS = 3; • Show example in BasketballPlayer