Board: Objects, Arrays and Pedagogy - University of Waterloo

Board: Objects, Arrays and Pedagogy - University of Waterloo

Board: Objects, Arrays and Pedagogy Troy Vasiga Lecturer, University of Waterloo Associate Director (Computing), CEMC October 18, 2006 Introduction Outline of Board Using the Board Using a Coordinate

The Board and arrays The Board pedagogy October 18, 2006 IBM CASCON 2006 Talk 2 Outline of the Board The Board is an object that displays a graphical, rectangular game board which can be interacted with and manipulated.

put pegs and lines on it remove pegs and lines from it display text messages interact by way of clicking October 18, 2006 IBM CASCON 2006 Talk 3 What can you do with a Board? Simple games checkers, chess, go, tic-tac-toe, minesweeper, ... Patterns and simple graphics

Elementary animation October 18, 2006 IBM CASCON 2006 Talk 4 Why Board and not something else? Strings are terrible objects immutable not much fun Robots are good, but not very general (hard to play games or make pretty pictures) Board provide a very general template to

implement graphical interactive objects October 18, 2006 IBM CASCON 2006 Talk 5 A first picture October 18, 2006 IBM CASCON 2006 Talk 6 Creating a Board Just like any other real object in Java

Board b1D = new Board(10); or Board b2D = new Board(3,5); October 18, 2006 IBM CASCON 2006 Talk 7 Constructor Benefits You may notice that the constructor is: simple (Have you ever tried to make a GUI from scratch in Java? Don't.) overloaded (allows discussion of what overloaded means) standard (all other real Java objects are created this way)

October 18, 2006 IBM CASCON 2006 Talk 8 Pegs We can place pegs on the Board by using the putPeg method: b1D.putPeg("black", 3); or b2D.putPeg("blue", 2,5); October 18, 2006 IBM CASCON 2006 Talk

9 Pegs (continued) Pegs are specified by a colour and a row and column Colours are one of: black, white, red, orange, yellow, green, cyan, pink, blue Row and column must be valid for the particular board October 18, 2006 IBM CASCON 2006 Talk 10

Simple Exercise Read in the number of rows and columns and produce the following picture (the example uses 5 rows and 6 columns): October 18, 2006 IBM CASCON 2006 Talk 11 Solution import java.util.Scanner; public class Corners { public static void main(String[] args) {

Scanner myScanner = new Scanner(System.in); int row=myScanner.nextInt(); int col=myScanner.nextInt(); Board b = new Board(row, col); b.putPeg("white", 0, 0); b.putPeg("red", 0, col-1); b.putPeg("orange", row-1, 0); b.putPeg("green", row-1,col-1); } } October 18, 2006 IBM CASCON 2006 Talk 12 Methods

Notice that calling methods and passing parameters is covered by the putPeg method October 18, 2006 IBM CASCON 2006 Talk 13 Removing Pegs Pegs can be removed from the board using b1D.removePeg(2); or b2D.removePeg(4,2); October 18, 2006 IBM CASCON 2006 Talk

14 Lines Lines can be drawn on the board using b2D.drawLine(0,1,3,2); and removed using b2D.removeLine(0,1,3,2); October 18, 2006 IBM CASCON 2006 Talk 15 Other Board benefits Easy to make assignments! Example: Read in the size of a square board

and make the following picture: October 18, 2006 IBM CASCON 2006 Talk 16 Loops and if statements import java.util.Scanner; public class SecondBoard { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); int size = myScanner.nextInt(); Board b = new Board(size, size);

for (int i = 0; i < size; i++) { for (int j = 0; j <= i; j++) { if (i % 2 == 0) { b.putPeg("yellow", i, j); } else { b.putPeg("black", i, j); } } } } } October 18, 2006

IBM CASCON 2006 Talk 17 Coordinates and Clicks So far, we have just manipulated a Board by directly putting/removing pegs and lines We can use the following method: public Coordinate getClick() A Coordinate is an object which has methods: getRows() getColumns() October 18, 2006 IBM CASCON 2006 Talk

18 Exercise Starting with a white peg in the top left corner of a 4x4 board, move the peg back and forth down each row, reversing direction when the end of the row is reached, moving one position each time the mouse is clicked. October 18, 2006 IBM CASCON 2006 Talk 19

Solution public class PegMover { public static void main(String[] args) { final int size = 4; Board b2D = new Board(size,size); int row=0, col=0, direction = 1; // will have value +1 if right, -1 if left Coordinate c; while (row < size && col < size && col >= 0) { b2D.putPeg("white", row, col); c = b2D.getClick(); col = col + direction; b2D.removePeg(row, col-direction); if (col == size) {

direction = -1; row++; col--; } if (col == -1) { direction = 1; row++; col++; } } } } October 18, 2006 IBM CASCON 2006 Talk 20

Arrays Arrays are difficult to visualize Numerical data is not interesting to students The Board is an array!!! In particular, the state of the Board is known only internally to the Board. Moreover, the only accessor methods are: getRows(), getColumns() October 18, 2006

IBM CASCON 2006 Talk 21 Remembering state is the job of the user One dimensional arrays correspond exactly to one dimensional board. The colour of a peg can be stored using an integer value (or an integer value in a String array of colours) October 18, 2006 IBM CASCON 2006 Talk 22

Exercise Create a one-dimensional board of size 3, and each click on a cell changes the colour of the cell from white to red to black to white again. The program stops when all pegs are black. October 18, 2006 IBM CASCON 2006 Talk 23 Solution public class OneD {

public static void main(String[] args) { Board b1D = new Board(3); String[] colours = new String[3]; colours[0] = "white"; colours[1] = "red"; colours[2] = "black"; int[] markers = new int[3]; markers[0] = 0; markers[1] = 0; markers[2] = 0; b1D.putPeg("white", 0); b1D.putPeg("white", 1); b1D.putPeg("white", 2); while (markers[0] % 3 != 2 || markers[1] % 3!= 2 || markers[2] % 3 != 2) { int position = b1D.getClick().getCol(); markers[position]++;

b1D.putPeg(colours[markers[position]%3], position); } } } October 18, 2006 IBM CASCON 2006 Talk 24 Pedagogical Decisions

FUN! General Interactive Expandable Capture key object-oriented ideas Allow use of non object-oriented ideas Method choice October 18, 2006 IBM CASCON 2006 Talk 25

Capture key object-oriented ideas instance variables accessor methods mutator methods overloading passing parameters (of both primitive and class type) returning values (objects and primitive types)

overloading parameters October 18, 2006 IBM CASCON 2006 Talk 26 Allow use of non object-oriented ideas conditions repetitions data structures October 18, 2006

IBM CASCON 2006 Talk 27 Method choice Trade-off between user-side and implementation side Who does the record keeping? If all information about the Board is known, then the problem becomes algorithmic If no information about the Board is known, then the problem becomes data structural Algorithms+Data Structures = Programs (Dijkstra) October 18, 2006 IBM CASCON 2006 Talk

28 Summary Board class is easy to use graphical fun pedagogically sound free

October 18, 2006 IBM CASCON 2006 Talk 29

Recently Viewed Presentations

  • Présentation PowerPoint

    Présentation PowerPoint

    1. Définitions. Développement durable (art 8 du R(UE) 1303/2013) dont le principe de « pollueur-payeur » : EM et AG veillent à ce que les programmes financés par les FESI soit mis en œuvre en conformité avec le principe de...
  • Sédation en réanimation - Free

    Sédation en réanimation - Free

    The activated carbon layer absorbs some of the expired anaesthetic vapour and desorbs some of it in the next inspiration Sédation par halogénés(3) En anesthésie: Tempia, Anesth Alnalg, 2003 81 patients, chirurgie lourde, Randomisée sévo classique ou anaconda Utilisation équivalente...
  • Year 2 maths parents meeting - Whitley Abbey Primary

    Year 2 maths parents meeting - Whitley Abbey Primary

    In school we teach the children to use . millimetres (mm), centimetres (cm), metres (m) for length. Millilitres (ml) and litres (l) to measure capacity. Grams (g) and Kilograms (KG) for weight. Degrees Celsius for temperature. For this the children...
  • Diapositiva 1 - ComexPerú

    Diapositiva 1 - ComexPerú

    Paintgs,drawgs&pastels executd by hand exc hd 4906&h-p&h-d mfd art '902119. Orthopaedic or fracture appliances, nes '920993. Parts and accessories for the musical instruments of heading No 92.03 '911280. Clock cases&cases of a similar type,for oth goods of this Chapter,nes '910899....
  • Field Research: Conducting an Interview Purdue OWL Staff

    Field Research: Conducting an Interview Purdue OWL Staff

    Worldwide users may call the grammar hotline with short questions. Worldwide users may also visit the Purdue OWL to access a large library of writing resources. * This presentation is designed to teach students techniques and etiquette for effective interviewing...
  • Bell Work Complete the Stream Development worksheet Earths

    Bell Work Complete the Stream Development worksheet Earths

    Salinity and the Water Cycle: Liquid water: is usually a mix of salts from dissolved rocks & sediments Water vapor and ice: salt free 86% of global evaporation & 78% global precipitation is over ocean. Changes in the water cycle...
  • Central Maryland Transit Development Plan

    Central Maryland Transit Development Plan

    This Central Maryland TDP is significant in that it is the first fully regional transit plan for our unique region that includes several jurisdiction partners. The MTA requires Locally Operated Transit Systems in Maryland to conduct a TDP every 5...
  • MY VISUAL RHETORIC - barren.k12.ky.us

    MY VISUAL RHETORIC - barren.k12.ky.us

    Visual Rhetoric Project BY MICAELA PERKINS Background Information WWF: The World Wildlife Fund, created this ad, as well as many others to show the importance of keeping the Rainforest alive, as well as the animals that live there.