Java Object Oriented Programming 2 D Arrays Objectives

Java Object Oriented Programming 2 D Arrays

Objectives: § Understand two-dimensional arrays Lab 05 -2

Two-Dimensional Arrays § A one-dimensional array stores a list of elements § A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions Lab 05 -3

Two-Dimensional Arrays § To be precise, in Java a two-dimensional array is an array of arrays § A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; § A array element is referenced using two index values: value = scores[3][6] § The array stored in one row can be specified using one index Lab 05 -4

2 -D Arrays: Declaration // 2 -D array of char with 5 rows, 7 cols: char letter. Grid [ ] = new char [5][7]; // 2 -D array of Color with 1024 rows, 768 cols: Color image [ ] = new Color [1024][768]; // 2 -D array of double with 2 rows and 3 cols: double sample [ ] = { { 0. 0, 0. 1, 0. 2 }, { 1. 0, 1. 1, 1. 2 } }; Lab 05 -5

2 -D Arrays in Java § In Java, a 2 -D array is basically a 1 -D array of 1 -D arrays, its rows. Each row is stored in a separate block of consecutive memory locations. § If m is a 2 -D array, then m[k] is a 1 -D array, the k-th row. § m. length is the number of rows. § m[k]. length is the length of the k-th row. Lab 05 -6

2 -D Arrays: Dimensions § Java allows “rugged” arrays, in which different rows have different lengths. § In a rectangular array, m[0]. length can be used to represent the number of columns. “Rugged” array: Rectangular array: m. length m[3]. length m[0]. length Lab 05 -7

Java Object Oriented Programming Top Down Design

Top-Down Programming (cont’d) § Start JCreator. § Open the file “Lab 05 A. java”. § “Lab 05 A. java” is in the Lab 05Lab 05 A folder. Lab 05 -9

Top-Down Programming WAP that creates a 2 D array of chars of unknown size. Fill the grid with characters read from a data file. Display the contents of the matrix on the console screen. . Lab 05 -10
![Declare The Class/main method public class Lab 05 A { public static void main(String[] Declare The Class/main method public class Lab 05 A { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/b6a1dc72b9597287c2d00ec74a6951f7/image-11.jpg)
Declare The Class/main method public class Lab 05 A { public static void main(String[] args) { Lab 05 A lab = new Lab 05 A( ); while (lab. input()) // fill the matrix from a data file { lab. regular( ); // display the matrix // lab. upside. Down(); // Upside down // lab. right 90(); // Rotate the matrix right 90° // lab. left 90(); // Rotate the matrix left 90° // lab. mirror(); // flip the image (mirror) Lab 05 -11

Write The input( ) Method public void pause() { System. out. print(“n. Press ENTER to continue. . . ”); (new java. util. Scanner(System. in)). next. Line(); } Lab 05 -12
![Declare The Class/Instance Fields private char[ ][ ] grid; Lab 05 -13 Declare The Class/Instance Fields private char[ ][ ] grid; Lab 05 -13](http://slidetodoc.com/presentation_image_h2/b6a1dc72b9597287c2d00ec74a6951f7/image-13.jpg)
Declare The Class/Instance Fields private char[ ][ ] grid; Lab 05 -13

Write The input( ) Method public boolean input() { } Lab 05 -14

Write The input( ) Method public boolean input() { try { } catch (IOException e) { } } Lab 05 -15

Write The input( ) Method public boolean input() { try { } catch (IOException e) { System. out. println(e); return false; } } Lab 05 -16

Write The input( ) Method try { } Lab 05 -17

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); } Lab 05 -18

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); } Lab 05 -19

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); } Lab 05 -20

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; } Lab 05 -21

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); } Lab 05 -22

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); int rows = reader. next. Int(); } Lab 05 -23

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); int rows = reader. next. Int(); reader. next. Line(); } Lab 05 -24

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); int rows = reader. next. Int(); reader. next. Line(); grid = new char[rows][]; } Lab 05 -25

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); int rows = reader. next. Int(); reader. next. Line(); grid = new char[rows][]; for (int y = 0; y < rows; y++) grid[y] = reader. next. Line(). to. Char. Array(); } Lab 05 -26

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); int rows = reader. next. Int(); reader. next. Line(); grid = new char[rows][]; for (int y = 0; y < rows; y++) grid[y] = reader. next. Line(). to. Char. Array(); reader. close(); } Lab 05 -27

Write The input( ) Method try { Scanner kybd = new Scanner(System. in); System. out. print("Enter the file name: "); String file. Name = kybd. next. Line(); if (file. Name. equals. Ignore. Case("quit")) return false; Scanner reader = new Scanner(new File(file. Name + ". dat")); int rows = reader. next. Int(); reader. next. Line(); grid = new char[rows][width]; for (int y = 0; y < rows; y++) grid[y] = reader. next. Line(). to. Char. Array(); reader. close(); return true; } Lab 05 -28

Display The 2 D Array Lab 05 -29

Row First Traversal column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. X. . . O . . @ Lab 05 -30

Row First Traversal column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ X. . . O. O. X. Lab 05 -31

Row First Traversal column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ X. . . O. O. X. . . _. . Lab 05 -32

Row First Traversal column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ X. . # . O. + |. . . _. . X. + |. O. . @ Lab 05 -33

Write The regular( ) Method public void regular() { } Lab 05 -34

Write The regular( ) Method public void regular() { for (int y = 0; y < grid. length; y++) { } } Lab 05 -35

Write The regular( ) Method public void regular() { for (int y = 0; y < grid. length; y++) { for (int x = 0; x < grid[y]. length; x++) { } } } Lab 05 -36

Write The regular( ) Method public void regular() { for (int y = 0; y < grid. length; y++) { for (int x = 0; x < grid[y]. length; x++) { System. out. print(grid[y][x]); } } } Lab 05 -37

Write The regular( ) Method public void regular() { for (int y = 0; y < grid. length; y++) { for (int x = 0; x < grid[y]. length; x++) { System. out. print(grid[y][x]); } System. out. println(); } } Lab 05 -38

Write The regular( ) Method public void regular() { for (int y = 0; y < grid. length; y++) { for (int x = 0; x < grid[y]. length; x++) { System. out. print(grid[y][x]); } System. out. println(); } pause(); } Lab 05 -39

Compile and Execute Lab 05. java Lab 05 -40

Upside Down Lab 05 -41

Upside Down column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. #. . . @ Lab 05 -42

Upside Down column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ #. . . @. |. |. Lab 05 -43

Upside Down column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ #. . . @. |. |. . +. +. Lab 05 -44


Write The upside. Down( ) Method public void upside. Down() { } Lab 05 -46

Write The upside. Down( ) Method public void upside. Down() { for (int y = grid. length - 1; y >= 0; y--) { } } Lab 05 -47

Write The upside. Down( ) Method public void upside. Down() { for (int y = grid. length - 1; y >= 0; y--) { for (int x = 0; x < grid[y]. length; x++) { } } } Lab 05 -48

Write The upside. Down( ) Method public void upside. Down() { for (int y = grid. length - 1; y >= 0; y--) { for (int x = 0; x < grid[y]. length; x++) { System. out. print(grid[y][x]); } } } Lab 05 -49

Write The upside. Down( ) Method public void upside. Down() { for (int y = grid. length - 1; y >= 0; y--) { for (int x = 0; x < grid[y]. length; x++) { System. out. print(grid[y][x]); } System. out. println(); } } Lab 05 -50

Write The upside. Down( ) Method public void upside. Down() { for (int y = grid. length - 1; y >= 0; y--) { for (int x = 0; x < grid[y]. length; x++) { System. out. print(grid[y][x]); } System. out. println(); } pause(); } Lab 05 -51
![Modify The main( ) Method public static void main(String[] args) { Lab 05 A Modify The main( ) Method public static void main(String[] args) { Lab 05 A](http://slidetodoc.com/presentation_image_h2/b6a1dc72b9597287c2d00ec74a6951f7/image-52.jpg)
Modify The main( ) Method public static void main(String[] args) { Lab 05 A lab = new Lab 05 A( ); while (lab. input()) // fill the matrix from a data file { lab. regular( ); // display the matrix lab. upside. Down(); // Display upside down // lab. right 90(); // Rotate the matrix right 90° // lab. left 90(); // Rotate the matrix left 90° // lab. mirror(); // flip the image (mirror) // lab. double. Inverted(); // Upside down & mirrored } } Lab 05 -52

Rotate 90° Right Lab 05 -53

Rotate 90° Right column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. #. . X . . @ Lab 05 -54

Rotate 90° Right column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ #. . X. _ +. O. Lab 05 -55

Rotate 90° Right column (x) 0 1 2 3 row (y) 4 0 X. . . O 1 . . # 2 3 4 5 O. + |. . _. . . X. + |. . . @ #. . X. _ +. O. . _. . Lab 05 -56


Write The right 90( ) Method public void right 90() { } Lab 05 -58

Write The right 90( ) Method public void right 90() { for (int x = 0; x < grid[0]. length; x++) { } } Lab 05 -59

Write The right 90( ) Method public void right 90() { for (int x = 0; x < grid[0]. length; x++) { for (int y = grid. length - 1; y >= 0; y--) { } } } Lab 05 -60

Write The right 90( ) Method public void right 90() { for (int x = 0; x < grid[0]. length; x++) { for (int y = 0; y = grid. length - 1; y >= 0; y--) { System. out. print(grid[y][x]); } } Lab 05 -61

Write The right 90( ) Method public void right 90() { for (int x = 0; x < grid[0]. length; x++) { for (int y = grid. length - 1; y >= 0; y--) { System. out. print(grid[y][x]); } System. out. println(); } } Lab 05 -62

Write The right 90( ) Method public void right 90() { for (int x = 0; x < grid[0]. length; x++) { for (int y = grid. length - 1; y >= 0; y--) { System. out. print(grid[y][x]); } System. out. println(); } pause(); } Lab 05 -63
![Modify The main( ) Method public static void main(String[] args) { Lab 05 A Modify The main( ) Method public static void main(String[] args) { Lab 05 A](http://slidetodoc.com/presentation_image_h2/b6a1dc72b9597287c2d00ec74a6951f7/image-64.jpg)
Modify The main( ) Method public static void main(String[] args) { Lab 05 A lab = new Lab 05 A( ); while (lab. input()) // fill the matrix from a data file { lab. regular( ); // display the matrix lab. upside. Down(); // Display upside down lab. right 90(); // Rotate the matrix right 90° // lab. left 90(); // Rotate the matrix left 90° // lab. mirror(); // flip the image (mirror) // lab. double. Inverted(); // Upside down & mirrored } } Lab 05 -64

Try The Other Data Files “abe. dat” “george. dat” “rip. dat” “werewolf. dat” “zombie. dat” etc… Lab 05 -65

Java Object Oriented Programming Questions?

Java Object Oriented Programming Begin Lab 05
- Slides: 67