Docsity

Mapping Park Coordinates to Java: Converting Park Coordinates to Java 2D Array Indexing, Slides of Object Oriented Programming

Partial preview of the text

Download Mapping Park Coordinates to Java: Converting Park Coordinates to Java 2D Array Indexing and more Slides Object Oriented Programming in PDF only on Docsity!

Lists

Time spent on A

  • max: 25.4; avg: 5.2 hours, mean: 4.5 hours, min: 0.57 hours 0

Understanding assignment A

(1,4) (2,4) (3,4) (4,4) (1,3) (2,3) (3,3) (4,3) (1,2) (2,2) (3,2) (4,2) (1,1) (2,1) (3,1) (4,1)

  • A 4x4 park with the butterfly in

position (1,1), a flower and a

cliff.

  • The same park! The map

“wraps” as if the park lives

(3,2) (4,2) (1,2) (2,2) on a torus!

(3,1) (4,1) (1,1) (2,1) (3,4) (4,4) (1,4) (2,4) (3,3) (4,3) (1,3) (2,3)

Mapping Park coordinates to Java

  • In the Park we use a column,row notation to identify cells, and have HEIGHT columns and WIDTH rows.
  • Inside Java, we use 2-D arrays that index from 0
    • TileCell[][] myMap = new TileCell[Height][Width]
    • But one issue is that a (column,row) coordinate in the Park has to be

“swapped” and adjusted to access the corresponding cell of myMap

  • Rule:
    • Save the Park Cell from Park location (r,c) at myMap[HEIGHT-c][r-1]
    • myMap[x][y] tells you about Park location (y+1, HEIGHT-x)

Mapping Park coordinates to Java

Park (Height=3, Width=3) [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2] myMap (Height=3, Width=3)

Mapping Park coordinates to Java

Park uses (column, row) notation [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2] myMap uses [row][column] indexing Example: Park (2,1) => myMap[HEIGHT-r][c-1]

... myMap[3-1][2-1]: myMap[2][1] HEIGHT=3, WIDTH=

A Simple List Interface

  • Note that Java has a more complete interface and we do expect you to

be proficient with it!

public interface List {

public void insert(T element);

public void delete(T element);

public boolean contains(T element);

public int size();

List Data Structures

  • Array
    • Must specify array size at creation
    • Insert, delete require moving elements
    • Must copy array to a larger array when it gets full

 Linked list

 uses a sequence of linked cells  we will define a class ListCell from which we build lists 24 - 7 87 78 empty 24

  • 7 87 78 -

Class ListCell

class ListCell { private T datum; private ListCell next; public ListCell(T datum, ListCell next){ this.datum = datum; this.next = next; } public T getDatum() { return datum; } public ListCell getNext() { return next; } public void setDatum(T obj) { datum = obj; } public void setNext(ListCell c) { next = c; } } Each list element “points” to the next one! End of list: next== null

Ways of building a Linked List

ListCell c = new ListCell(new Integer(24),null);

Integer t = new Integer(24); Integer s = new Integer(-7); Integer e = new Integer(87); ListCell p = new ListCell(t, new ListCell(s, new ListCell(e, null))); p ListCell: c ListCell:

Accessing List Elements

  • Linked Lists are sequential-

access data structures.

  • To access contents of cell n in sequence, you must access cells 0 ... n- 1
  • Accessing data in first cell:

p.getDatum()

  • Accessing data in second cell: p.getNext().getDatum()
  • Accessing next field in second

cell: p.getNext().getNext()

 Writing to fields

in cells can be

done the same way

 Update data in first cell: p.setDatum(new Integer(53));  Update data in second cell: p.getNext().setDatum(new Integer(53));  Chop off third cell: p.getNext().setNext(null);

p ListCell:

Access Example: Linear Search

// Here is another version. Why does this work? public static boolean search(T x, ListCell c) { while(c != null) { if (c.getDatum().equals(x)) return true; c = c.getNext(); } return false; } // Scan list looking for x, return true if found public static boolean search(T x, ListCell c) { for (ListCell lc = c; lc != null; lc = lc.getNext()) { if (lc.getDatum().equals(x)) return true; } return false; }

Recursion on Lists

  • Recursion can be done on lists
    • Similar to recursion on integers
  • Almost always
    • Base case: empty list
    • Recursive case: Assume you can solve problem on the tail, use that in the solution for the whole list
  • Many list operations can be implemented very simply by using this

idea

  • Although some are easier to implement using iteration

Recursive Search

  • Base case: empty list
    • return false
  • Recursive case: non-empty list
    • if data in first cell equals object x, return true
    • else return the result of doing linear search on the tail