2D Arrays in Java - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

2D Arrays in Java

Description:

An interesting assignment idea - manipulating greyscale images ... Greyscale images and 2D arrays ... of integers that represent values in a greyscale image ... – PowerPoint PPT presentation

Number of Views:468
Avg rating:3.0/5.0
Slides: 26
Provided by: sand235
Category:
Tags: arrays | greyscale | java

less

Transcript and Presenter's Notes

Title: 2D Arrays in Java


1
2D Arrays in Java
  • CSIT Symposium - Atlanta - June 2007
  • Sandy Graham - University of Waterloo

2
Overview
  • How 2D arrays work in Java
  • A natural representation example - the Board
    class
  • An interesting assignment idea - manipulating
    greyscale images

3
Features of 2D arrays in Java
  • The variable values for all arrays are references
  • must allocate space first, and then fill in the
    data
  • allows for resizing later
  • A 2D array is a 1D array of arrays
  • one row of the array is a 1D array
  • allows for non-rectangular arrays
  • Can query the array for its dimensions
  • .length

4
Creating and referencing a 2D Array
  • int sample new int4 2
  • sample1 0 17
  • Note integer values in Java are automatically
    initialized to 0
  • in general, values within the array must be
    initialized with an assignment statement

5
Traversing a 2D array
  • Suppose you had an array of grades for various
    students
  • not all students have the same number of courses
  • double transcript new double
  • Recall that transcripti is a reference to a 1D
    array of double values
  • could be passed as a parameter

6
The Board class
  • 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
  • Used for introducing a variety of Java
    programming topics
  • A natural relationship with arrays

7
What can you do with a Board?
  • Simple games
  • checkers, chess, go, tic-tac-toe, minesweeper,
    ...
  • Patterns and simplegraphics
  • Elementary animation

8
Possible Programming Topics
  • Object construction
  • Method calls
  • Variables
  • Selection and repetition structures
  • Overloading
  • 1D arrays
  • 2D arrays
  • more

9
A first picture
10
Creating a Board
  • Just like any other real object in Java
  • Board b1D new Board(10)
  • or
  • Board b2D new Board(3,5)

11
Pegs
  • We can place pegs on the Board by using the
    putPeg method
  • b1D.putPeg("black", 3)
  • or
  • b2D.putPeg("blue", 2,5)

12
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

13
Simple Exercise
  • Read in the number of rows and columns and
    produce the following picture (the example uses 5
    rows and 6 columns)

14
Solution
  • import java.util.Scanner
  • public class Corners
  • public static void main(String args)
  • Scanner myScanner new Scanner(System.in)
  • int rowmyScanner.nextInt()
  • int colmyScanner.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)

15
Removing Pegs
  • Pegs can be removed from the board using
  • b1D.removePeg(2)
  • or
  • b2D.removePeg(4,2)

16
Lines
  • Lines can be drawn on the board using
  • b2D.drawLine(0,1,3,2)
  • and removed using
  • b2D.removeLine(0,1,3,2)

17
Input and Output
  • The user can interact with the Board using mouse
    clicks
  • int getPosition() or
  • Coordinate getClick()
  • The user can receive text messages on the Board
  • void displayMessage(String theMessage)

18
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()
  • It is not possible to query the Board to find out
    information about the pegs (sort of an MVC
    paradigm)
  • provide the .class file of the Board class only

19
Remembering state with arrays
  • One dimensional arrays correspond exactly to one
    dimensional Board.
  • Two dimensional arrays correspond to a two
    dimensional Board
  • Peg information can be stored
  • using a String value (representing a colour)
  • using an integer value (representing the number
    of times a particular square has been clicked
    upon)
  • using a boolean value (representing the presence
    or absence of a peg)
  • using a char value (representing an abstract
    value that the peg is modeling)

20
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.

21
Solution
  • public class OneD
  • public static void main(String args)
  • Board b1D new Board(3)
  • String colours new String3
  • colours0 "white"
  • colours1 "red"
  • colours2 "black"
  • int markers new int3
  • markers0 0
  • markers1 0
  • markers2 0
  • b1D.putPeg("white", 0)
  • b1D.putPeg("white", 1)
  • b1D.putPeg("white", 2)
  • while (markers0 3 ! 2 markers1
    3! 2 markers2 3 ! 2)
  • int position b1D.getClick().getCol()
  • markersposition

22
More advanced projects
  • Peg game - one and two dimensions

23
Greyscale images and 2D arrays
  • Practice manipulating 2D arrays of integers that
    represent values in a greyscale image
  • Sample transformations
  • darken
  • flip
  • rotate
  • mirror
  • blur
  • negative

24
A good project
  • Advantages
  • many possible array manipulations available
  • works with a real life object
  • Disadvantages
  • lots of hidden code to make it work
  • overhead in understanding how the transformations
    code fits with the rest of the project

25
Summary
  • Two dimensional arrays in Java are represented
    differently than many other languages
  • There are natural two dimensional structures that
    exist that can be modeled
  • Visual and interactive projects tend to be
    self-motivating
Write a Comment
User Comments (0)
About PowerShow.com